Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/Exceptions/DeviationsNotCalculatedException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace DragonCode\Benchmark\Exceptions;

use ValueError;

use function implode;

class DeviationsNotCalculatedException extends ValueError
{
public function __construct(int|string $name)
{
parent::__construct(
implode(' ', [
"No information is available for the deviation values for \"$name\".",
'You must call the "deviations" method before this check.',
])
);
}
}
33 changes: 33 additions & 0 deletions src/Services/AssertService.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace DragonCode\Benchmark\Services;

use AssertionError;
use DragonCode\Benchmark\Exceptions\DeviationsNotCalculatedException;

class AssertService
{
Expand Down Expand Up @@ -111,6 +112,38 @@ public function toBeTotalMemory(?float $from = null, ?float $till = null): stati
return $this;
}

public function toBeDeviationTime(?float $from = null, ?float $till = null): static
{
$from = $this->resolveFrom($from, $till);

foreach ($this->result as $key => $item) {
if (! $item->deviation) {
throw new DeviationsNotCalculatedException($key);
}

$this->assertGreaterThan($item->deviation->percent->time, $from, 'deviation time');
$this->assertLessThan($item->deviation->percent->time, $till, 'deviation time');
}

return $this;
}

public function toBeDeviationMemory(?float $from = null, ?float $till = null): static
{
$from = $this->resolveFrom($from, $till);

foreach ($this->result as $name => $item) {
if (! $item->deviation) {
throw new DeviationsNotCalculatedException($name);
}

$this->assertGreaterThan($item->deviation->percent->memory, $from, 'deviation memory');
$this->assertLessThan($item->deviation->percent->memory, $till, 'deviation memory');
}

return $this;
}

protected function assertGreaterThan(float $actual, ?float $expected, string $name): void
{
if ($expected === null) {
Expand Down
26 changes: 20 additions & 6 deletions src/View/TableView.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function show(array $data): void
$headers = $this->headers($data);
$widths = $this->columnWidths($headers, $data);

$this->writeLine($this->separator($widths));
$this->writeHeaderLine($widths);
$this->writeLine($this->formatRow($headers, $widths));
$this->writeLine($this->separator($widths));

Expand All @@ -33,7 +33,7 @@ public function show(array $data): void
$this->writeLine($this->formatRow(array_values($row), $widths));
}

$this->writeLine($this->separator($widths));
$this->writeFooterLine($widths);
}

protected function headers(array $data): array
Expand All @@ -54,11 +54,25 @@ protected function columnWidths(array $headers, array $data): array
return $widths;
}

protected function separator(array $widths): string
protected function writeHeaderLine(array $widths): void
{
$this->writeLine(
$this->separator($widths, '┌', '┬', '┐')
);
}

protected function writeFooterLine(array $widths): void
{
$this->writeLine(
$this->separator($widths, '└', '┴', '┘')
);
}

protected function separator(array $widths, string $start = '├', string $divider = '┼', string $end = '┤'): string
{
$parts = array_map(fn (int $w) => str_repeat('-', $w + 2), $widths);
$parts = array_map(static fn (int $w) => str_repeat('', $w + 2), $widths);

return '+' . implode('+', $parts) . '+';
return $start . implode($divider, $parts) . $end;
}

protected function formatRow(array $values, array $widths): string
Expand All @@ -69,6 +83,6 @@ protected function formatRow(array $values, array $widths): string
$cells[] = ' ' . mb_str_pad((string) $value, $widths[$i]) . ' ';
}

return '|' . implode('|', $cells) . '|';
return '' . implode('', $cells) . '';
}
}
20 changes: 10 additions & 10 deletions tests/.pest/snapshots/Unit/Result/ToConsoleTest/compare.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
0/1 [░░░░░░░░░░░░░░░░░░░░░░░░░░░░] 0% 1/1 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100%


+-------+-------------------------------+------------------------------+
| # | 0 | 1 |
+-------+-------------------------------+------------------------------+
| min | 15.67890123 ms - 202 bytes | 2.3456789 ms - 102 bytes |
| max | 112.78901234 ms - 209 bytes | 9.75678901 ms - 109 bytes |
| avg | 53.02524845125 ms - 205 bytes | 5.94290024625 ms - 105 bytes |
| total | 424.20198761 ms - 1.61 KB | 47.54320197 ms - 844 bytes |
+-------+-------------------------------+------------------------------+
| order | 2 | 1 |
+-------+-------------------------------+------------------------------+
┌───────┬───────────────────────────────┬──────────────────────────────┐
# 0 1
├───────┼───────────────────────────────┼──────────────────────────────┤
min 15.67890123 ms - 202 bytes 2.3456789 ms - 102 bytes
max 112.78901234 ms - 209 bytes 9.75678901 ms - 109 bytes
avg 53.02524845125 ms - 205 bytes 5.94290024625 ms - 105 bytes
total 424.20198761 ms - 1.61 KB 47.54320197 ms - 844 bytes
├───────┼───────────────────────────────┼──────────────────────────────┤
order 2 1
└───────┴───────────────────────────────┴──────────────────────────────┘
Expand Down
26 changes: 13 additions & 13 deletions tests/.pest/snapshots/Unit/Result/ToConsoleTest/deviations.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@
0/1 [░░░░░░░░░░░░░░░░░░░░░░░░░░░░] 0% 1/1 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100%


+------------------+-------------------------------+------------------------------+
| # | 0 | 1 |
+------------------+-------------------------------+------------------------------+
| min | 15.67890123 ms - 202 bytes | 2.3456789 ms - 102 bytes |
| max | 112.78901234 ms - 209 bytes | 9.75678901 ms - 109 bytes |
| avg | 53.02524845125 ms - 205 bytes | 5.94290024625 ms - 105 bytes |
| total | 848.40397522 ms - 3.21 KB | 95.08640394 ms - 1.65 KB |
+------------------+-------------------------------+------------------------------+
| order | 2 | 1 |
+------------------+-------------------------------+------------------------------+
| deviation time | 0% | 0% |
| deviation memory | 0% | 0% |
+------------------+-------------------------------+------------------------------+
┌──────────────────┬───────────────────────────────┬──────────────────────────────┐
# 0 1
├──────────────────┼───────────────────────────────┼──────────────────────────────┤
min 15.67890123 ms - 202 bytes 2.3456789 ms - 102 bytes
max 112.78901234 ms - 209 bytes 9.75678901 ms - 109 bytes
avg 53.02524845125 ms - 205 bytes 5.94290024625 ms - 105 bytes
total 848.40397522 ms - 3.21 KB 95.08640394 ms - 1.65 KB
├──────────────────┼───────────────────────────────┼──────────────────────────────┤
order 2 1
├──────────────────┼───────────────────────────────┼──────────────────────────────┤
deviation time 0% 0%
deviation memory 0% 0%
└──────────────────┴───────────────────────────────┴──────────────────────────────┘
Expand Down
20 changes: 10 additions & 10 deletions tests/.pest/snapshots/Unit/Round/RoundTest/default.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
0/1 [░░░░░░░░░░░░░░░░░░░░░░░░░░░░] 0% 1/1 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100%


+-------+-------------------------------+------------------------------+
| # | 0 | 1 |
+-------+-------------------------------+------------------------------+
| min | 15.67890123 ms - 202 bytes | 2.3456789 ms - 102 bytes |
| max | 112.78901234 ms - 209 bytes | 9.75678901 ms - 109 bytes |
| avg | 53.02524845125 ms - 205 bytes | 5.94290024625 ms - 105 bytes |
| total | 424.20198761 ms - 1.61 KB | 47.54320197 ms - 844 bytes |
+-------+-------------------------------+------------------------------+
| order | 2 | 1 |
+-------+-------------------------------+------------------------------+
┌───────┬───────────────────────────────┬──────────────────────────────┐
# 0 1
├───────┼───────────────────────────────┼──────────────────────────────┤
min 15.67890123 ms - 202 bytes 2.3456789 ms - 102 bytes
max 112.78901234 ms - 209 bytes 9.75678901 ms - 109 bytes
avg 53.02524845125 ms - 205 bytes 5.94290024625 ms - 105 bytes
total 424.20198761 ms - 1.61 KB 47.54320197 ms - 844 bytes
├───────┼───────────────────────────────┼──────────────────────────────┤
order 2 1
└───────┴───────────────────────────────┴──────────────────────────────┘
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
0/1 [░░░░░░░░░░░░░░░░░░░░░░░░░░░░] 0% 1/1 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100%


+-------+--------------------+-------------------+
| # | 0 | 1 |
+-------+--------------------+-------------------+
| min | 16 ms - 202 bytes | 2 ms - 102 bytes |
| max | 113 ms - 209 bytes | 10 ms - 109 bytes |
| avg | 53 ms - 205 bytes | 6 ms - 105 bytes |
| total | 424 ms - 1.61 KB | 48 ms - 844 bytes |
+-------+--------------------+-------------------+
| order | 2 | 1 |
+-------+--------------------+-------------------+
┌───────┬────────────────────┬───────────────────┐
# 0 1
├───────┼────────────────────┼───────────────────┤
min 16 ms - 202 bytes 2 ms - 102 bytes
max 113 ms - 209 bytes 10 ms - 109 bytes
avg 53 ms - 205 bytes 6 ms - 105 bytes
total 424 ms - 1.61 KB 48 ms - 844 bytes
├───────┼────────────────────┼───────────────────┤
order 2 1
└───────┴────────────────────┴───────────────────┘
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
0/1 [░░░░░░░░░░░░░░░░░░░░░░░░░░░░] 0% 1/1 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100%


+-------+-----------------------+----------------------+
| # | 0 | 1 |
+-------+-----------------------+----------------------+
| min | 15.68 ms - 202 bytes | 2.35 ms - 102 bytes |
| max | 112.79 ms - 209 bytes | 9.76 ms - 109 bytes |
| avg | 53.03 ms - 205 bytes | 5.94 ms - 105 bytes |
| total | 424.2 ms - 1.61 KB | 47.54 ms - 844 bytes |
+-------+-----------------------+----------------------+
| order | 2 | 1 |
+-------+-----------------------+----------------------+
┌───────┬───────────────────────┬──────────────────────┐
# 0 1
├───────┼───────────────────────┼──────────────────────┤
min 15.68 ms - 202 bytes 2.35 ms - 102 bytes
max 112.79 ms - 209 bytes 9.76 ms - 109 bytes
avg 53.03 ms - 205 bytes 5.94 ms - 105 bytes
total 424.2 ms - 1.61 KB 47.54 ms - 844 bytes
├───────┼───────────────────────┼──────────────────────┤
order 2 1
└───────┴───────────────────────┴──────────────────────┘
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
0/1 [░░░░░░░░░░░░░░░░░░░░░░░░░░░░] 0% 1/1 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100%


+-------+--------------------------+------------------------+
| # | 0 | 1 |
+-------+--------------------------+------------------------+
| min | 15.6789 ms - 202 bytes | 2.34568 ms - 102 bytes |
| max | 112.78901 ms - 209 bytes | 9.75679 ms - 109 bytes |
| avg | 53.02525 ms - 205 bytes | 5.9429 ms - 105 bytes |
| total | 424.20199 ms - 1.61 KB | 47.5432 ms - 844 bytes |
+-------+--------------------------+------------------------+
| order | 2 | 1 |
+-------+--------------------------+------------------------+
┌───────┬──────────────────────────┬────────────────────────┐
# 0 1
├───────┼──────────────────────────┼────────────────────────┤
min 15.6789 ms - 202 bytes 2.34568 ms - 102 bytes
max 112.78901 ms - 209 bytes 9.75679 ms - 109 bytes
avg 53.02525 ms - 205 bytes 5.9429 ms - 105 bytes
total 424.20199 ms - 1.61 KB 47.5432 ms - 844 bytes
├───────┼──────────────────────────┼────────────────────────┤
order 2 1
└───────┴──────────────────────────┴────────────────────────┘
Expand Down

This file was deleted.

Loading