Skip to content

Commit aa93921

Browse files
Merge pull request #96
Added new assertions for deviation: `toBeDeviationTime` and `toBeDeviationMemory` methods
2 parents cc6d5d1 + 6349140 commit aa93921

16 files changed

Lines changed: 307 additions & 114 deletions
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace DragonCode\Benchmark\Exceptions;
6+
7+
use ValueError;
8+
9+
use function implode;
10+
11+
class DeviationsNotCalculatedException extends ValueError
12+
{
13+
public function __construct(int|string $name)
14+
{
15+
parent::__construct(
16+
implode(' ', [
17+
"No information is available for the deviation values for \"$name\".",
18+
'You must call the "deviations" method before this check.',
19+
])
20+
);
21+
}
22+
}

src/Services/AssertService.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace DragonCode\Benchmark\Services;
66

77
use AssertionError;
8+
use DragonCode\Benchmark\Exceptions\DeviationsNotCalculatedException;
89

910
class AssertService
1011
{
@@ -111,6 +112,38 @@ public function toBeTotalMemory(?float $from = null, ?float $till = null): stati
111112
return $this;
112113
}
113114

115+
public function toBeDeviationTime(?float $from = null, ?float $till = null): static
116+
{
117+
$from = $this->resolveFrom($from, $till);
118+
119+
foreach ($this->result as $key => $item) {
120+
if (! $item->deviation) {
121+
throw new DeviationsNotCalculatedException($key);
122+
}
123+
124+
$this->assertGreaterThan($item->deviation->percent->time, $from, 'deviation time');
125+
$this->assertLessThan($item->deviation->percent->time, $till, 'deviation time');
126+
}
127+
128+
return $this;
129+
}
130+
131+
public function toBeDeviationMemory(?float $from = null, ?float $till = null): static
132+
{
133+
$from = $this->resolveFrom($from, $till);
134+
135+
foreach ($this->result as $name => $item) {
136+
if (! $item->deviation) {
137+
throw new DeviationsNotCalculatedException($name);
138+
}
139+
140+
$this->assertGreaterThan($item->deviation->percent->memory, $from, 'deviation memory');
141+
$this->assertLessThan($item->deviation->percent->memory, $till, 'deviation memory');
142+
}
143+
144+
return $this;
145+
}
146+
114147
protected function assertGreaterThan(float $actual, ?float $expected, string $name): void
115148
{
116149
if ($expected === null) {

src/View/TableView.php

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public function show(array $data): void
1919
$headers = $this->headers($data);
2020
$widths = $this->columnWidths($headers, $data);
2121

22-
$this->writeLine($this->separator($widths));
22+
$this->writeHeaderLine($widths);
2323
$this->writeLine($this->formatRow($headers, $widths));
2424
$this->writeLine($this->separator($widths));
2525

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

36-
$this->writeLine($this->separator($widths));
36+
$this->writeFooterLine($widths);
3737
}
3838

3939
protected function headers(array $data): array
@@ -54,11 +54,25 @@ protected function columnWidths(array $headers, array $data): array
5454
return $widths;
5555
}
5656

57-
protected function separator(array $widths): string
57+
protected function writeHeaderLine(array $widths): void
58+
{
59+
$this->writeLine(
60+
$this->separator($widths, '', '', '')
61+
);
62+
}
63+
64+
protected function writeFooterLine(array $widths): void
65+
{
66+
$this->writeLine(
67+
$this->separator($widths, '', '', '')
68+
);
69+
}
70+
71+
protected function separator(array $widths, string $start = '', string $divider = '', string $end = ''): string
5872
{
59-
$parts = array_map(fn (int $w) => str_repeat('-', $w + 2), $widths);
73+
$parts = array_map(static fn (int $w) => str_repeat('', $w + 2), $widths);
6074

61-
return '+' . implode('+', $parts) . '+';
75+
return $start . implode($divider, $parts) . $end;
6276
}
6377

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

72-
return '|' . implode('|', $cells) . '|';
86+
return '' . implode('', $cells) . '';
7387
}
7488
}

tests/.pest/snapshots/Unit/Result/ToConsoleTest/compare.snap

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
0/1 [░░░░░░░░░░░░░░░░░░░░░░░░░░░░] 0% 1/1 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100%
33

44

5-
+-------+-------------------------------+------------------------------+
6-
| # | 0 | 1 |
7-
+-------+-------------------------------+------------------------------+
8-
| min | 15.67890123 ms - 202 bytes | 2.3456789 ms - 102 bytes |
9-
| max | 112.78901234 ms - 209 bytes | 9.75678901 ms - 109 bytes |
10-
| avg | 53.02524845125 ms - 205 bytes | 5.94290024625 ms - 105 bytes |
11-
| total | 424.20198761 ms - 1.61 KB | 47.54320197 ms - 844 bytes |
12-
+-------+-------------------------------+------------------------------+
13-
| order | 2 | 1 |
14-
+-------+-------------------------------+------------------------------+
5+
┌───────┬───────────────────────────────┬──────────────────────────────┐
6+
# 0 1
7+
├───────┼───────────────────────────────┼──────────────────────────────┤
8+
min 15.67890123 ms - 202 bytes 2.3456789 ms - 102 bytes
9+
max 112.78901234 ms - 209 bytes 9.75678901 ms - 109 bytes
10+
avg 53.02524845125 ms - 205 bytes 5.94290024625 ms - 105 bytes
11+
total 424.20198761 ms - 1.61 KB 47.54320197 ms - 844 bytes
12+
├───────┼───────────────────────────────┼──────────────────────────────┤
13+
order 2 1
14+
└───────┴───────────────────────────────┴──────────────────────────────┘

tests/.pest/snapshots/Unit/Result/ToConsoleTest/deviations.snap

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@
22
0/1 [░░░░░░░░░░░░░░░░░░░░░░░░░░░░] 0% 1/1 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100%
33

44

5-
+------------------+-------------------------------+------------------------------+
6-
| # | 0 | 1 |
7-
+------------------+-------------------------------+------------------------------+
8-
| min | 15.67890123 ms - 202 bytes | 2.3456789 ms - 102 bytes |
9-
| max | 112.78901234 ms - 209 bytes | 9.75678901 ms - 109 bytes |
10-
| avg | 53.02524845125 ms - 205 bytes | 5.94290024625 ms - 105 bytes |
11-
| total | 848.40397522 ms - 3.21 KB | 95.08640394 ms - 1.65 KB |
12-
+------------------+-------------------------------+------------------------------+
13-
| order | 2 | 1 |
14-
+------------------+-------------------------------+------------------------------+
15-
| deviation time | 0% | 0% |
16-
| deviation memory | 0% | 0% |
17-
+------------------+-------------------------------+------------------------------+
5+
┌──────────────────┬───────────────────────────────┬──────────────────────────────┐
6+
# 0 1
7+
├──────────────────┼───────────────────────────────┼──────────────────────────────┤
8+
min 15.67890123 ms - 202 bytes 2.3456789 ms - 102 bytes
9+
max 112.78901234 ms - 209 bytes 9.75678901 ms - 109 bytes
10+
avg 53.02524845125 ms - 205 bytes 5.94290024625 ms - 105 bytes
11+
total 848.40397522 ms - 3.21 KB 95.08640394 ms - 1.65 KB
12+
├──────────────────┼───────────────────────────────┼──────────────────────────────┤
13+
order 2 1
14+
├──────────────────┼───────────────────────────────┼──────────────────────────────┤
15+
deviation time 0% 0%
16+
deviation memory 0% 0%
17+
└──────────────────┴───────────────────────────────┴──────────────────────────────┘

tests/.pest/snapshots/Unit/Round/RoundTest/default.snap

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
0/1 [░░░░░░░░░░░░░░░░░░░░░░░░░░░░] 0% 1/1 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100%
33

44

5-
+-------+-------------------------------+------------------------------+
6-
| # | 0 | 1 |
7-
+-------+-------------------------------+------------------------------+
8-
| min | 15.67890123 ms - 202 bytes | 2.3456789 ms - 102 bytes |
9-
| max | 112.78901234 ms - 209 bytes | 9.75678901 ms - 109 bytes |
10-
| avg | 53.02524845125 ms - 205 bytes | 5.94290024625 ms - 105 bytes |
11-
| total | 424.20198761 ms - 1.61 KB | 47.54320197 ms - 844 bytes |
12-
+-------+-------------------------------+------------------------------+
13-
| order | 2 | 1 |
14-
+-------+-------------------------------+------------------------------+
5+
┌───────┬───────────────────────────────┬──────────────────────────────┐
6+
# 0 1
7+
├───────┼───────────────────────────────┼──────────────────────────────┤
8+
min 15.67890123 ms - 202 bytes 2.3456789 ms - 102 bytes
9+
max 112.78901234 ms - 209 bytes 9.75678901 ms - 109 bytes
10+
avg 53.02524845125 ms - 205 bytes 5.94290024625 ms - 105 bytes
11+
total 424.20198761 ms - 1.61 KB 47.54320197 ms - 844 bytes
12+
├───────┼───────────────────────────────┼──────────────────────────────┤
13+
order 2 1
14+
└───────┴───────────────────────────────┴──────────────────────────────┘

tests/.pest/snapshots/Unit/Round/RoundTest/round_with_data_set___0__.snap

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
0/1 [░░░░░░░░░░░░░░░░░░░░░░░░░░░░] 0% 1/1 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100%
33

44

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

tests/.pest/snapshots/Unit/Round/RoundTest/round_with_data_set___0____0_.snap

Lines changed: 0 additions & 14 deletions
This file was deleted.

tests/.pest/snapshots/Unit/Round/RoundTest/round_with_data_set___2__.snap

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
0/1 [░░░░░░░░░░░░░░░░░░░░░░░░░░░░] 0% 1/1 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100%
33

44

5-
+-------+-----------------------+----------------------+
6-
| # | 0 | 1 |
7-
+-------+-----------------------+----------------------+
8-
| min | 15.68 ms - 202 bytes | 2.35 ms - 102 bytes |
9-
| max | 112.79 ms - 209 bytes | 9.76 ms - 109 bytes |
10-
| avg | 53.03 ms - 205 bytes | 5.94 ms - 105 bytes |
11-
| total | 424.2 ms - 1.61 KB | 47.54 ms - 844 bytes |
12-
+-------+-----------------------+----------------------+
13-
| order | 2 | 1 |
14-
+-------+-----------------------+----------------------+
5+
┌───────┬───────────────────────┬──────────────────────┐
6+
# 0 1
7+
├───────┼───────────────────────┼──────────────────────┤
8+
min 15.68 ms - 202 bytes 2.35 ms - 102 bytes
9+
max 112.79 ms - 209 bytes 9.76 ms - 109 bytes
10+
avg 53.03 ms - 205 bytes 5.94 ms - 105 bytes
11+
total 424.2 ms - 1.61 KB 47.54 ms - 844 bytes
12+
├───────┼───────────────────────┼──────────────────────┤
13+
order 2 1
14+
└───────┴───────────────────────┴──────────────────────┘

tests/.pest/snapshots/Unit/Round/RoundTest/round_with_data_set___2____2_.snap

Lines changed: 0 additions & 14 deletions
This file was deleted.

0 commit comments

Comments
 (0)