From 2072fe10dc4b7a3ed724646130ed1ac913e7f3b0 Mon Sep 17 00:00:00 2001 From: Nuno Maduro Date: Mon, 6 Nov 2023 09:19:10 +0000 Subject: [PATCH] feat: improves output --- src/Plugin.php | 2 - src/Printers/Detail.php | 174 ++++++++++++++---------------------- src/Result/DnsLookup.php | 8 ++ src/Result/Download.php | 8 ++ src/Result/Server.php | 8 ++ src/Result/TlsHandshake.php | 8 ++ src/Result/Upload.php | 8 ++ 7 files changed, 108 insertions(+), 108 deletions(-) diff --git a/src/Plugin.php b/src/Plugin.php index 6f879d2..11524b0 100644 --- a/src/Plugin.php +++ b/src/Plugin.php @@ -53,8 +53,6 @@ public function handleArguments(array $arguments): array } } - $concurrency = 1; - foreach ($arguments as $argument) { if (str_starts_with($argument, '--concurrency=')) { $run->concurrently((int) str_replace('--concurrency=', '', $argument)); diff --git a/src/Printers/Detail.php b/src/Printers/Detail.php index 51cde69..273f5de 100644 --- a/src/Printers/Detail.php +++ b/src/Printers/Detail.php @@ -26,8 +26,51 @@ public function print(Result $result): void HTML); $this->overview($result); - $this->server($result); - $this->network($result); + + $color = $this->color($result->requests->dnsLookup->duration->avg, 20.0, 50.0, 100.0); + $value = $this->ms($result->requests->dnsLookup->duration->avg); + $this->twoColumnDetail('DNS Lookup Duration', "$value"); + + $color = $this->color($result->requests->tlsHandshake->duration->avg, 20.0, 50.0, 100.0); + $value = $this->ms($result->requests->tlsHandshake->duration->avg); + $this->twoColumnDetail('TLS Handshake Duration', "$value"); + + $color = $this->color($result->requests->duration->avg, 100.0, 300.0, 1000.0); + $this->twoColumnDetail( + 'Response Duration', + ''.$this->ms($total = $result->requests->duration->avg).'' + ); + + $color = $this->color($result->requests->upload->duration->avg, 50.0, 150.0, 250.0); + $value = $result->requests->upload->duration->avg; + $percentage = $value * 100.0 / $total; + $percentage = sprintf('%4.1f', $percentage); + $value = $this->ms($value); + $this->twoColumnDetail(<<— Upload + $percentage % + HTML, "$value"); + + $color = $this->color($result->requests->server->duration->avg, 50.0, 150.0, 400.0); + $value = $result->requests->server->duration->avg; + $percentage = $value * 100.0 / $total; + $percentage = sprintf('%4.1f', $percentage); + $value = $this->ms($value); + + $this->twoColumnDetail(<<— TTFB + $percentage % + HTML, "$value"); + + $color = $this->color($result->requests->download->duration->avg, 100.0, 300.0, 1000.0); + $value = $result->requests->download->duration->avg; + $percentage = $value * 100.0 / $total; + $percentage = sprintf('%4.1f', $percentage); + $value = $this->ms($value); + $this->twoColumnDetail(<<— Download + $percentage % + HTML, "$value"); render(<<<'HTML'
@@ -63,7 +106,7 @@ private function overview(Result $result): void $requestsRate = round($metrics['http_reqs']['values']['rate'], 2); $result->testRun()->concurrency(); - $this->twoColumnDetail('Total Requests', <<twoColumnDetail('Requests Count', <<$requestsRate reqs/second $requestsTotal requests HTML); @@ -77,109 +120,6 @@ private function overview(Result $result): void $this->twoColumnDetail('Success Rate', <<$successRate % HTML); - - $responseDuration = $metrics['http_req_connecting']['values']['avg'] - + $metrics['http_req_tls_handshaking']['values']['avg'] - + $metrics['http_req_duration']['values']['avg']; - - $responseDurationColor = match (true) { - $responseDuration === 0.0 => '', - $responseDuration < 200.0 => 'text-green', - $responseDuration < 400.0 => 'text-yellow', - $responseDuration < 800.0 => 'text-orange', - default => 'text-red', - }; - - $responseDuration = sprintf('%4.2f', $responseDuration); - - $this->twoColumnDetail('Response Duration', <<$responseDuration ms - HTML); - } - - /** - * Prints the network's detail. - */ - private function network(Result $result): void - { - $metrics = $result->toArray()['metrics']; - - $responseDuration = $metrics['http_req_connecting']['values']['avg'] - + $metrics['http_req_tls_handshaking']['values']['avg'] - + $metrics['http_req_duration']['values']['avg']; - - $responseNetworkDuration = $metrics['http_req_connecting']['values']['avg'] - + $metrics['http_req_tls_handshaking']['values']['avg'] - + $metrics['http_req_duration']['values']['avg'] - - $metrics['http_req_waiting']['values']['avg']; - $responseNetworkDurationPercentage = $responseDuration > 0.00 ? round($responseNetworkDuration * 100 / $responseDuration, 2) : 0.00; - $responseNetworkDurationColor = match (true) { - $responseNetworkDuration === 0.0 => '', - $responseNetworkDuration < 100.0 => 'text-green', - $responseNetworkDuration < 200.0 => 'text-yellow', - $responseNetworkDuration < 400.0 => 'text-orange', - default => 'text-red', - }; - - $responseNetworkDuration = sprintf('%4.2f', $responseNetworkDuration); - - $this->twoColumnDetail(<<―Network - $responseNetworkDurationPercentage % - HTML, <<$responseNetworkDuration ms - HTML); - - $tlsHandshakingTime = $metrics['http_req_tls_handshaking']['values']['avg']; - $tlsHandshakingTime = sprintf('%4.2f', $tlsHandshakingTime); - - $dnsLookupTime = $metrics['http_req_connecting']['values']['avg']; - $dnsLookupTime = sprintf('%4.2f', $dnsLookupTime); - - $uploadTime = $metrics['http_req_sending']['values']['avg']; - $uploadTime = sprintf('%4.2f', $uploadTime); - - $downloadTime = $metrics['http_req_receiving']['values']['avg']; - $downloadTime = sprintf('%4.2f', $downloadTime); - - foreach ([ - 'TLS Handshaking' => "$tlsHandshakingTime ms", - 'DNS Lookup' => "$dnsLookupTime ms", - 'Upload' => "$uploadTime ms", - 'Download' => "$downloadTime ms", - ] as $title => $value) { - $this->twoColumnDetail(''.$title, $value); - } - } - - /** - * Prints the server's detail. - */ - private function server(Result $result): void - { - $metrics = $result->toArray()['metrics']; - - $responseDuration = $metrics['http_req_connecting']['values']['avg'] - + $metrics['http_req_tls_handshaking']['values']['avg'] - + $metrics['http_req_duration']['values']['avg']; - $responseServerDuration = $metrics['http_req_waiting']['values']['avg']; - $responseServerDurationColor = match (true) { - $responseServerDuration === 0.0 => '', - $responseServerDuration < 100.0 => 'text-green', - $responseServerDuration < 200.0 => 'text-yellow', - $responseServerDuration < 400.0 => 'text-orange', - default => 'text-red', - }; - - $responseServerDurationPercentage = $responseDuration > 0.00 ? round($responseServerDuration * 100 / $responseDuration, 2) : 0.00; - $responseServerDuration = sprintf('%4.2f', $responseServerDuration); - - $this->twoColumnDetail(<<―Server - $responseServerDurationPercentage % - HTML, <<$responseServerDuration ms - HTML); } /** @@ -199,4 +139,26 @@ private function twoColumnDetail(string $left, string $right): void
HTML); } + + /** + * Returns the formatted duration in milliseconds. + */ + private function ms(float $duration): string + { + return sprintf('%4.2f ms', $duration); + } + + /** + * Returns the color for the given duration. + */ + private function color(float $duration, float $excellent, float $ok, float $poor): string + { + return match (true) { + $duration === 0.0 => '', + $duration <= $excellent => 'text-green font-bold', + $duration <= $ok => 'text-yellow font-bold', + $duration <= $poor => 'text-orange font-bold', + default => 'text-red font-bold', + }; + } } diff --git a/src/Result/DnsLookup.php b/src/Result/DnsLookup.php index c7030c7..d2ff57d 100644 --- a/src/Result/DnsLookup.php +++ b/src/Result/DnsLookup.php @@ -28,4 +28,12 @@ public function duration(): Duration { return new Duration($this->result->toArray()['metrics']['http_req_connecting']['values']); } + + /** + * Proxies the properties to methods. + */ + public function __get(string $name): mixed + { + return $this->{$name}(); // @phpstan-ignore-line + } } diff --git a/src/Result/Download.php b/src/Result/Download.php index 019d324..d3622d2 100644 --- a/src/Result/Download.php +++ b/src/Result/Download.php @@ -37,4 +37,12 @@ public function duration(): Duration { return new Duration($this->result->toArray()['metrics']['http_req_receiving']['values']); } + + /** + * Proxies the properties to methods. + */ + public function __get(string $name): mixed + { + return $this->{$name}(); // @phpstan-ignore-line + } } diff --git a/src/Result/Server.php b/src/Result/Server.php index 28e02b0..f882867 100644 --- a/src/Result/Server.php +++ b/src/Result/Server.php @@ -28,4 +28,12 @@ public function duration(): Duration { return new Duration($this->result->toArray()['metrics']['http_req_waiting']['values']); } + + /** + * Proxies the properties to methods. + */ + public function __get(string $name): mixed + { + return $this->{$name}(); // @phpstan-ignore-line + } } diff --git a/src/Result/TlsHandshake.php b/src/Result/TlsHandshake.php index 621aca3..5d840b0 100644 --- a/src/Result/TlsHandshake.php +++ b/src/Result/TlsHandshake.php @@ -28,4 +28,12 @@ public function duration(): Duration { return new Duration($this->result->toArray()['metrics']['http_req_tls_handshaking']['values']); } + + /** + * Proxies the properties to methods. + */ + public function __get(string $name): mixed + { + return $this->{$name}(); // @phpstan-ignore-line + } } diff --git a/src/Result/Upload.php b/src/Result/Upload.php index e1f45d0..2b75774 100644 --- a/src/Result/Upload.php +++ b/src/Result/Upload.php @@ -37,4 +37,12 @@ public function duration(): Duration { return new Duration($this->result->toArray()['metrics']['http_req_sending']['values']); } + + /** + * Proxies the properties to methods. + */ + public function __get(string $name): mixed + { + return $this->{$name}(); // @phpstan-ignore-line + } }