Skip to content

Commit 6349140

Browse files
Added new assertions for deviation: toBeDeviationTime and toBeDeviationMemory methods
1 parent eace68f commit 6349140

15 files changed

Lines changed: 287 additions & 108 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) {

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.

tests/.pest/snapshots/Unit/Round/RoundTest/round_with_data_set___5__.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.6789 ms - 202 bytes | 2.34568 ms - 102 bytes |
9-
| max | 112.78901 ms - 209 bytes | 9.75679 ms - 109 bytes |
10-
| avg | 53.02525 ms - 205 bytes | 5.9429 ms - 105 bytes |
11-
| total | 424.20199 ms - 1.61 KB | 47.5432 ms - 844 bytes |
12-
+-------+--------------------------+------------------------+
13-
| order | 2 | 1 |
14-
+-------+--------------------------+------------------------+
5+
┌───────┬──────────────────────────┬────────────────────────┐
6+
# 0 1
7+
├───────┼──────────────────────────┼────────────────────────┤
8+
min 15.6789 ms - 202 bytes 2.34568 ms - 102 bytes
9+
max 112.78901 ms - 209 bytes 9.75679 ms - 109 bytes
10+
avg 53.02525 ms - 205 bytes 5.9429 ms - 105 bytes
11+
total 424.20199 ms - 1.61 KB 47.5432 ms - 844 bytes
12+
├───────┼──────────────────────────┼────────────────────────┤
13+
order 2 1
14+
└───────┴──────────────────────────┴────────────────────────┘

0 commit comments

Comments
 (0)