Skip to content

Commit 5de7f58

Browse files
Callback calls have been moved to a separate method.
1 parent 855bd6d commit 5de7f58

14 files changed

Lines changed: 158 additions & 128 deletions

src/Benchmark.php

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

77
use Closure;
8-
use DragonCode\Benchmark\Exceptions\ValueIsNotCallableException;
98
use DragonCode\Benchmark\Services\AssertService;
109
use DragonCode\Benchmark\Services\CallbacksService;
1110
use DragonCode\Benchmark\Services\CollectorService;
@@ -16,10 +15,7 @@
1615
use DragonCode\Benchmark\View\ProgressBarView;
1716

1817
use function abs;
19-
use function array_first;
2018
use function count;
21-
use function is_array;
22-
use function is_callable;
2319
use function max;
2420

2521
class Benchmark
@@ -102,11 +98,7 @@ public function round(?int $precision): static
10298

10399
public function compare(array|Closure ...$callbacks): static
104100
{
105-
$this->clear();
106-
107-
$values = $this->resolveCallbacks($callbacks);
108-
109-
$this->withProgress($values, $this->steps($values));
101+
$this->callbacks->compare(...$callbacks);
110102

111103
return $this;
112104
}
@@ -116,6 +108,8 @@ public function compare(array|Closure ...$callbacks): static
116108
*/
117109
public function toData(): array
118110
{
111+
$this->performCallbacks();
112+
119113
return $this->result->get(
120114
$this->collector->all()
121115
);
@@ -139,13 +133,25 @@ public function toAssert(): AssertService
139133
);
140134
}
141135

142-
protected function withProgress(array $callbacks, int $count): void
136+
protected function performCallbacks(): void
137+
{
138+
$this->clear();
139+
140+
$callbacks = $this->callbacks->compare;
141+
142+
$this->withProgress(
143+
callback: fn (ProgressBarView $bar) => $this->chunks($callbacks, $bar),
144+
total : $this->steps($callbacks)
145+
);
146+
}
147+
148+
protected function withProgress(Closure $callback, int $total): void
143149
{
144150
$this->view->emptyLine();
145151

146-
$bar = $this->view->progressBar()->create($count);
152+
$bar = $this->view->progressBar()->create($total);
147153

148-
$this->chunks($callbacks, $bar);
154+
$callback($bar);
149155

150156
$bar->finish();
151157
$this->view->emptyLine(2);
@@ -159,8 +165,6 @@ protected function steps(array $callbacks): int
159165
protected function chunks(array $callbacks, ProgressBarView $progressBar): void
160166
{
161167
foreach ($callbacks as $name => $callback) {
162-
$this->validate($callback);
163-
164168
$this->callbacks->performBefore($name);
165169

166170
$this->run($name, $callback, $progressBar);
@@ -194,20 +198,6 @@ protected function push(mixed $name, float $time, float $memory): void
194198
$this->collector->push($name, [$time, $memory]);
195199
}
196200

197-
protected function resolveCallbacks(array $callbacks): array
198-
{
199-
$first = array_first($callbacks);
200-
201-
return is_array($first) ? $first : $callbacks;
202-
}
203-
204-
protected function validate(mixed $callback): void
205-
{
206-
if (! is_callable($callback)) {
207-
throw new ValueIsNotCallableException($callback);
208-
}
209-
}
210-
211201
protected function clear(): void
212202
{
213203
$this->result->clear();

src/Services/CallbacksService.php

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

77
use Closure;
8+
use DragonCode\Benchmark\Exceptions\ValueIsNotCallableException;
9+
10+
use function array_first;
11+
use function is_array;
12+
use function is_callable;
813

914
class CallbacksService
1015
{
@@ -16,6 +21,17 @@ class CallbacksService
1621

1722
public ?Closure $afterEach = null;
1823

24+
public array $compare = [];
25+
26+
public function compare(array|Closure ...$callbacks): void
27+
{
28+
foreach ($this->parameters($callbacks) as $key => $callback) {
29+
$this->validate($callback);
30+
31+
$this->compare[$key] = $callback;
32+
}
33+
}
34+
1935
public function performBefore(int|string $name): mixed
2036
{
2137
return $this->perform($this->before, $name);
@@ -44,4 +60,18 @@ protected function perform(?Closure $callback, mixed ...$args): mixed
4460

4561
return $callback(...$args);
4662
}
63+
64+
protected function parameters(array $callbacks): array
65+
{
66+
$first = array_first($callbacks);
67+
68+
return is_array($first) ? $first : $callbacks;
69+
}
70+
71+
protected function validate(mixed $callback): void
72+
{
73+
if (! is_callable($callback)) {
74+
throw new ValueIsNotCallableException($callback);
75+
}
76+
}
4777
}

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

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1-
+-------+-------------------------------+------------------------------+
2-
| # | 0 | 1 |
3-
+-------+-------------------------------+------------------------------+
4-
| min | 15.67890123 ms - 202 bytes | 2.3456789 ms - 102 bytes |
5-
| max | 112.78901234 ms - 209 bytes | 9.75678901 ms - 109 bytes |
6-
| avg | 53.02524845125 ms - 205 bytes | 5.94290024625 ms - 105 bytes |
7-
| total | 424.20198761 ms - 1.61 KB | 47.54320197 ms - 844 bytes |
8-
+-------+-------------------------------+------------------------------+
9-
| order | 2 | 1 |
10-
+-------+-------------------------------+------------------------------+
1+
2+
0/1 [░░░░░░░░░░░░░░░░░░░░░░░░░░░░] 0% 1/1 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100%
3+
4+
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/default.snap

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1-
+-------+-------------------------------+------------------------------+
2-
| # | 0 | 1 |
3-
+-------+-------------------------------+------------------------------+
4-
| min | 15.67890123 ms - 202 bytes | 2.3456789 ms - 102 bytes |
5-
| max | 112.78901234 ms - 209 bytes | 9.75678901 ms - 109 bytes |
6-
| avg | 53.02524845125 ms - 205 bytes | 5.94290024625 ms - 105 bytes |
7-
| total | 424.20198761 ms - 1.61 KB | 47.54320197 ms - 844 bytes |
8-
+-------+-------------------------------+------------------------------+
9-
| order | 2 | 1 |
10-
+-------+-------------------------------+------------------------------+
1+
2+
0/1 [░░░░░░░░░░░░░░░░░░░░░░░░░░░░] 0% 1/1 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100%
3+
4+
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: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1-
+-------+--------------------+-------------------+
2-
| # | 0 | 1 |
3-
+-------+--------------------+-------------------+
4-
| min | 16 ms - 202 bytes | 2 ms - 102 bytes |
5-
| max | 113 ms - 209 bytes | 10 ms - 109 bytes |
6-
| avg | 53 ms - 205 bytes | 6 ms - 105 bytes |
7-
| total | 424 ms - 1.61 KB | 48 ms - 844 bytes |
8-
+-------+--------------------+-------------------+
9-
| order | 2 | 1 |
10-
+-------+--------------------+-------------------+
1+
2+
0/1 [░░░░░░░░░░░░░░░░░░░░░░░░░░░░] 0% 1/1 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100%
3+
4+
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 & 10 deletions
This file was deleted.

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

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1-
+-------+-----------------------+----------------------+
2-
| # | 0 | 1 |
3-
+-------+-----------------------+----------------------+
4-
| min | 15.68 ms - 202 bytes | 2.35 ms - 102 bytes |
5-
| max | 112.79 ms - 209 bytes | 9.76 ms - 109 bytes |
6-
| avg | 53.03 ms - 205 bytes | 5.94 ms - 105 bytes |
7-
| total | 424.2 ms - 1.61 KB | 47.54 ms - 844 bytes |
8-
+-------+-----------------------+----------------------+
9-
| order | 2 | 1 |
10-
+-------+-----------------------+----------------------+
1+
2+
0/1 [░░░░░░░░░░░░░░░░░░░░░░░░░░░░] 0% 1/1 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100%
3+
4+
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 & 10 deletions
This file was deleted.

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

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1-
+-------+--------------------------+------------------------+
2-
| # | 0 | 1 |
3-
+-------+--------------------------+------------------------+
4-
| min | 15.6789 ms - 202 bytes | 2.34568 ms - 102 bytes |
5-
| max | 112.78901 ms - 209 bytes | 9.75679 ms - 109 bytes |
6-
| avg | 53.02525 ms - 205 bytes | 5.9429 ms - 105 bytes |
7-
| total | 424.20199 ms - 1.61 KB | 47.5432 ms - 844 bytes |
8-
+-------+--------------------------+------------------------+
9-
| order | 2 | 1 |
10-
+-------+--------------------------+------------------------+
1+
2+
0/1 [░░░░░░░░░░░░░░░░░░░░░░░░░░░░] 0% 1/1 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100%
3+
4+
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+
+-------+--------------------------+------------------------+

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

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

0 commit comments

Comments
 (0)