Skip to content

Commit

Permalink
feat: uses med instead of avg
Browse files Browse the repository at this point in the history
  • Loading branch information
nunomaduro committed Nov 9, 2023
1 parent 5e89414 commit ab5d143
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 23 deletions.
26 changes: 14 additions & 12 deletions src/Printers/Detail.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ public function print(Result $result): void

$this->overview($result);

$color = $this->color($result->requests->dnsLookup->duration->avg, 20.0, 50.0, 100.0);
$value = $this->ms($result->requests->dnsLookup->duration->avg);
$color = $this->color($result->requests->dnsLookup->duration->med, 20.0, 50.0, 100.0);
$value = $this->ms($result->requests->dnsLookup->duration->med);

$domain = $result->url();
$domain = (string) parse_url($domain, PHP_URL_HOST);
Expand All @@ -49,20 +49,22 @@ public function print(Result $result): void
<span class="$color">$value</span>
HTML);

$color = $this->color($result->requests->tlsHandshake->duration->avg, 20.0, 50.0, 100.0);
$value = $this->ms($result->requests->tlsHandshake->duration->avg);
$color = $this->color($result->requests->tlsHandshake->duration->med, 20.0, 50.0, 100.0);
$value = $this->ms($result->requests->tlsHandshake->duration->med);
$this->twoColumnDetail('TLS Handshake Duration', <<<HTML
<span class="$color">$value</span>
HTML);

$color = $this->color($result->requests->duration->avg, 100.0, 300.0, 1000.0);
$total = $result->requests->duration->med;
$color = $this->color($total, 100.0, 300.0, 1000.0);

$this->twoColumnDetail(
'Request Duration',
'<span class="'.$color.'">'.$this->ms($total = $result->requests->duration->avg).'</span>'
'<span class="'.$color.'">'.$this->ms($total).'</span>'
);

$color = $this->color($result->requests->upload->duration->avg, 50.0, 150.0, 250.0);
$value = $result->requests->upload->duration->avg;
$color = $this->color($result->requests->upload->duration->med, 50.0, 150.0, 250.0);
$value = $result->requests->upload->duration->med;
$percentage = $total === 0.0 ? 0.0 : ($value * 100.0 / $total);
$percentage = sprintf('%4.1f', $percentage);
$value = $this->ms($value);
Expand All @@ -82,8 +84,8 @@ public function print(Result $result): void
<span class="$color">$value</span>
HTML);

$color = $this->color($result->requests->ttfb->duration->avg, 50.0, 150.0, 400.0);
$value = $result->requests->ttfb->duration->avg;
$color = $this->color($result->requests->ttfb->duration->med, 50.0, 150.0, 400.0);
$value = $result->requests->ttfb->duration->med;
$percentage = $total === 0.0 ? 0.0 : ($value * 100.0 / $total);
$percentage = sprintf('%4.1f', $percentage);
$value = $this->ms($value);
Expand All @@ -93,8 +95,8 @@ public function print(Result $result): void
<span class="ml-1 text-gray">$percentage % — including server processing time</span>
HTML, "<span class=\"$color\">$value</span>");

$color = $this->color($result->requests->download->duration->avg, 100.0, 300.0, 1000.0);
$value = $result->requests->download->duration->avg;
$color = $this->color($result->requests->download->duration->med, 100.0, 300.0, 1000.0);
$value = $result->requests->download->duration->med;
$percentage = $total === 0.0 ? 0.0 : ($value * 100.0 / $total);
$percentage = sprintf('%4.1f', $percentage);
$value = $this->ms($value);
Expand Down
47 changes: 36 additions & 11 deletions src/Printers/Progress.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,21 +65,22 @@ public function tail(): void
/** @var array<int, array{data: array{time: string, value: float}}> $points */
$points = [];
$buffer = '';
$currentTime = '';
$lastTime = '';

while ($this->process->isRunning()) {
$this->fetch($tail, $points, $buffer, $lastTime);
$this->fetch($tail, $points, $buffer, $currentTime, $lastTime);
}

$this->fetch($tail, $points, $buffer, $lastTime);
$this->fetch($tail, $points, $buffer, $currentTime, $lastTime);
}

/**
* Fetches the tail output.
*
* @param array<int, array{data: array{time: string, value: float}}> $points
*/
private function fetch(Process $tail, array &$points, string &$buffer, string &$lastTime): void
private function fetch(Process $tail, array &$points, string &$buffer, string &$currentTime, string &$lastTime): void
{
$output = trim($tail->getIncrementalOutput());

Expand All @@ -102,7 +103,6 @@ private function fetch(Process $tail, array &$points, string &$buffer, string &$
$currentTime = substr($point['data']['time'], 0, 19);
if ($lastTime !== $currentTime) {
$this->printCurrentPoints($points);
$points = [];

$lastTime = $currentTime;
}
Expand All @@ -128,33 +128,58 @@ private function printCurrentPoints(array $points): void
static $maxResponseTime;

if ($points !== []) {
$average = array_sum(array_map(fn ($point): float => $point['data']['value'], $points)) / count($points);
$values = array_map(fn ($point): float => $point['data']['value'], $points);
$median = $this->median($values);

$time = substr($points[0]['data']['time'], 11, 8);
$time = substr($points[count($points) - 1]['data']['time'], 11, 8);

$width = max(0, terminal()->width());
$width = $width - 4 - strlen($time);

if ($maxResponseTime === null) {
$maxResponseTime = max($average * 3, 1000);
$maxResponseTime = max($median * 3, 1000);
}

$greenDots = (int) (($average * $width) / $maxResponseTime);
$greenDots = (int) (($median * $width) / $maxResponseTime);

$greenDots = min($greenDots, 150 - 23);
$greenDots = min($greenDots, min(150, terminal()->width()) - 23);
$greenDots = str_repeat('', $greenDots);

$average = sprintf('%4.2f', $average);
$median = sprintf('%4.2f', $median);

render(<<<HTML
<div class="flex justify-between mx-2 max-w-150 text-gray">
<span>
<span>{$time}</span>
<span class="ml-1">{$greenDots}</span>
</span>
<span class="ml-1">{$average}ms</span>
<span class="ml-1">{$median}ms</span>
</div>
HTML);
}
}

/**
* Calculates the median.
*
* @param array<int, float> $values
*/
private function median(array $values): float
{
$count = count($values);

if ($count === 0) {
return 0.0;
}

sort($values);

$middle = (int) floor(($count - 1) / 2);

if ($count % 2 === 1) {
return $values[$middle];
}

return ($values[$middle] + $values[$middle + 1]) / 2.0;
}
}

0 comments on commit ab5d143

Please sign in to comment.