diff --git a/src/Benchmark.php b/src/Benchmark.php index a4a7787..3acd797 100644 --- a/src/Benchmark.php +++ b/src/Benchmark.php @@ -5,7 +5,6 @@ namespace DragonCode\Benchmark; use Closure; -use DragonCode\Benchmark\Exceptions\ValueIsNotCallableException; use DragonCode\Benchmark\Services\AssertService; use DragonCode\Benchmark\Services\CallbacksService; use DragonCode\Benchmark\Services\CollectorService; @@ -16,10 +15,7 @@ use DragonCode\Benchmark\View\ProgressBarView; use function abs; -use function array_first; use function count; -use function is_array; -use function is_callable; use function max; class Benchmark @@ -102,11 +98,7 @@ public function round(?int $precision): static public function compare(array|Closure ...$callbacks): static { - $this->clear(); - - $values = $this->resolveCallbacks($callbacks); - - $this->withProgress($values, $this->steps($values)); + $this->callbacks->compare(...$callbacks); return $this; } @@ -116,6 +108,8 @@ public function compare(array|Closure ...$callbacks): static */ public function toData(): array { + $this->performCallbacks(); + return $this->result->get( $this->collector->all() ); @@ -139,13 +133,25 @@ public function toAssert(): AssertService ); } - protected function withProgress(array $callbacks, int $count): void + protected function performCallbacks(): void + { + $this->clear(); + + $callbacks = $this->callbacks->compare; + + $this->withProgress( + callback: fn (ProgressBarView $bar) => $this->chunks($callbacks, $bar), + total : $this->steps($callbacks) + ); + } + + protected function withProgress(Closure $callback, int $total): void { $this->view->emptyLine(); - $bar = $this->view->progressBar()->create($count); + $bar = $this->view->progressBar()->create($total); - $this->chunks($callbacks, $bar); + $callback($bar); $bar->finish(); $this->view->emptyLine(2); @@ -159,8 +165,6 @@ protected function steps(array $callbacks): int protected function chunks(array $callbacks, ProgressBarView $progressBar): void { foreach ($callbacks as $name => $callback) { - $this->validate($callback); - $this->callbacks->performBefore($name); $this->run($name, $callback, $progressBar); @@ -194,20 +198,6 @@ protected function push(mixed $name, float $time, float $memory): void $this->collector->push($name, [$time, $memory]); } - protected function resolveCallbacks(array $callbacks): array - { - $first = array_first($callbacks); - - return is_array($first) ? $first : $callbacks; - } - - protected function validate(mixed $callback): void - { - if (! is_callable($callback)) { - throw new ValueIsNotCallableException($callback); - } - } - protected function clear(): void { $this->result->clear(); diff --git a/src/Services/CallbacksService.php b/src/Services/CallbacksService.php index 7433eab..9cccbdf 100644 --- a/src/Services/CallbacksService.php +++ b/src/Services/CallbacksService.php @@ -5,6 +5,11 @@ namespace DragonCode\Benchmark\Services; use Closure; +use DragonCode\Benchmark\Exceptions\ValueIsNotCallableException; + +use function array_first; +use function is_array; +use function is_callable; class CallbacksService { @@ -16,6 +21,17 @@ class CallbacksService public ?Closure $afterEach = null; + public array $compare = []; + + public function compare(array|Closure ...$callbacks): void + { + foreach ($this->parameters($callbacks) as $key => $callback) { + $this->validate($callback); + + $this->compare[$key] = $callback; + } + } + public function performBefore(int|string $name): mixed { return $this->perform($this->before, $name); @@ -44,4 +60,18 @@ protected function perform(?Closure $callback, mixed ...$args): mixed return $callback(...$args); } + + protected function parameters(array $callbacks): array + { + $first = array_first($callbacks); + + return is_array($first) ? $first : $callbacks; + } + + protected function validate(mixed $callback): void + { + if (! is_callable($callback)) { + throw new ValueIsNotCallableException($callback); + } + } } diff --git a/tests/.pest/snapshots/Unit/Result/ToConsoleTest/output.snap b/tests/.pest/snapshots/Unit/Result/ToConsoleTest/output.snap index 24cb656..03fd8e9 100644 --- a/tests/.pest/snapshots/Unit/Result/ToConsoleTest/output.snap +++ b/tests/.pest/snapshots/Unit/Result/ToConsoleTest/output.snap @@ -1,10 +1,14 @@ -+-------+-------------------------------+------------------------------+ -| # | 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 [░░░░░░░░░░░░░░░░░░░░░░░░░░░░] 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 | ++-------+-------------------------------+------------------------------+ diff --git a/tests/.pest/snapshots/Unit/Round/RoundTest/default.snap b/tests/.pest/snapshots/Unit/Round/RoundTest/default.snap index 24cb656..03fd8e9 100644 --- a/tests/.pest/snapshots/Unit/Round/RoundTest/default.snap +++ b/tests/.pest/snapshots/Unit/Round/RoundTest/default.snap @@ -1,10 +1,14 @@ -+-------+-------------------------------+------------------------------+ -| # | 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 [░░░░░░░░░░░░░░░░░░░░░░░░░░░░] 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 | ++-------+-------------------------------+------------------------------+ diff --git a/tests/.pest/snapshots/Unit/Round/RoundTest/round_with_data_set___0__.snap b/tests/.pest/snapshots/Unit/Round/RoundTest/round_with_data_set___0__.snap index e5a0e4a..2cc2425 100644 --- a/tests/.pest/snapshots/Unit/Round/RoundTest/round_with_data_set___0__.snap +++ b/tests/.pest/snapshots/Unit/Round/RoundTest/round_with_data_set___0__.snap @@ -1,10 +1,14 @@ -+-------+--------------------+-------------------+ -| # | 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 [░░░░░░░░░░░░░░░░░░░░░░░░░░░░] 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 | ++-------+--------------------+-------------------+ diff --git a/tests/.pest/snapshots/Unit/Round/RoundTest/round_with_data_set___0____0_.snap b/tests/.pest/snapshots/Unit/Round/RoundTest/round_with_data_set___0____0_.snap index e5a0e4a..2cc2425 100644 --- a/tests/.pest/snapshots/Unit/Round/RoundTest/round_with_data_set___0____0_.snap +++ b/tests/.pest/snapshots/Unit/Round/RoundTest/round_with_data_set___0____0_.snap @@ -1,10 +1,14 @@ -+-------+--------------------+-------------------+ -| # | 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 [░░░░░░░░░░░░░░░░░░░░░░░░░░░░] 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 | ++-------+--------------------+-------------------+ diff --git a/tests/.pest/snapshots/Unit/Round/RoundTest/round_with_data_set___2__.snap b/tests/.pest/snapshots/Unit/Round/RoundTest/round_with_data_set___2__.snap index ca9eeba..851185b 100644 --- a/tests/.pest/snapshots/Unit/Round/RoundTest/round_with_data_set___2__.snap +++ b/tests/.pest/snapshots/Unit/Round/RoundTest/round_with_data_set___2__.snap @@ -1,10 +1,14 @@ -+-------+-----------------------+----------------------+ -| # | 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 [░░░░░░░░░░░░░░░░░░░░░░░░░░░░] 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 | ++-------+-----------------------+----------------------+ diff --git a/tests/.pest/snapshots/Unit/Round/RoundTest/round_with_data_set___2____2_.snap b/tests/.pest/snapshots/Unit/Round/RoundTest/round_with_data_set___2____2_.snap index ca9eeba..851185b 100644 --- a/tests/.pest/snapshots/Unit/Round/RoundTest/round_with_data_set___2____2_.snap +++ b/tests/.pest/snapshots/Unit/Round/RoundTest/round_with_data_set___2____2_.snap @@ -1,10 +1,14 @@ -+-------+-----------------------+----------------------+ -| # | 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 [░░░░░░░░░░░░░░░░░░░░░░░░░░░░] 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 | ++-------+-----------------------+----------------------+ diff --git a/tests/.pest/snapshots/Unit/Round/RoundTest/round_with_data_set___5__.snap b/tests/.pest/snapshots/Unit/Round/RoundTest/round_with_data_set___5__.snap index 637cc7d..b539f60 100644 --- a/tests/.pest/snapshots/Unit/Round/RoundTest/round_with_data_set___5__.snap +++ b/tests/.pest/snapshots/Unit/Round/RoundTest/round_with_data_set___5__.snap @@ -1,10 +1,14 @@ -+-------+--------------------------+------------------------+ -| # | 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 [░░░░░░░░░░░░░░░░░░░░░░░░░░░░] 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 | ++-------+--------------------------+------------------------+ diff --git a/tests/.pest/snapshots/Unit/Round/RoundTest/round_with_data_set___5____5_.snap b/tests/.pest/snapshots/Unit/Round/RoundTest/round_with_data_set___5____5_.snap index 637cc7d..b539f60 100644 --- a/tests/.pest/snapshots/Unit/Round/RoundTest/round_with_data_set___5____5_.snap +++ b/tests/.pest/snapshots/Unit/Round/RoundTest/round_with_data_set___5____5_.snap @@ -1,10 +1,14 @@ -+-------+--------------------------+------------------------+ -| # | 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 [░░░░░░░░░░░░░░░░░░░░░░░░░░░░] 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 | ++-------+--------------------------+------------------------+ diff --git a/tests/Unit/Callbacks/AfterEachTest.php b/tests/Unit/Callbacks/AfterEachTest.php index 61265d3..6ae7600 100644 --- a/tests/Unit/Callbacks/AfterEachTest.php +++ b/tests/Unit/Callbacks/AfterEachTest.php @@ -14,7 +14,8 @@ ->compare([ fn () => true, fn () => true, - ]); + ]) + ->toData(); expect($result)->toHaveCount(6); }); @@ -29,7 +30,8 @@ ->compare([ 'foo' => fn () => true, 'bar' => fn () => true, - ]); + ]) + ->toData(); expect($result)->toBe([ 'foo', @@ -51,7 +53,8 @@ ->compare([ 'foo' => fn () => true, 'bar' => fn () => true, - ]); + ]) + ->toData(); expect($result)->toBe([ 'foo:1', @@ -73,7 +76,8 @@ ->compare([ fn () => true, fn () => true, - ]); + ]) + ->toData(); expect($result)->toBe([ '0:1', @@ -95,7 +99,8 @@ ->compare( fn () => true, fn () => true, - ); + ) + ->toData(); expect($result)->toBe([ '0:1', diff --git a/tests/Unit/Callbacks/AfterTest.php b/tests/Unit/Callbacks/AfterTest.php index 216fa6f..7a07d6e 100644 --- a/tests/Unit/Callbacks/AfterTest.php +++ b/tests/Unit/Callbacks/AfterTest.php @@ -14,7 +14,8 @@ ->compare([ fn () => true, fn () => true, - ]); + ]) + ->toData(); expect($result)->toHaveCount(2); }); @@ -29,7 +30,8 @@ ->compare([ 'foo' => fn () => true, 'bar' => fn () => true, - ]); + ]) + ->toData(); expect($result)->toBe([ 'foo', @@ -47,7 +49,8 @@ ->compare([ fn () => true, fn () => true, - ]); + ]) + ->toData(); expect($result)->toBe([ 0, @@ -65,7 +68,8 @@ ->compare( foo: fn () => true, bar: fn () => true, - ); + ) + ->toData(); expect($result)->toBe([ 'foo', @@ -83,7 +87,8 @@ ->compare( fn () => true, fn () => true, - ); + ) + ->toData(); expect($result)->toBe([ 0, diff --git a/tests/Unit/Callbacks/BeforeEachTest.php b/tests/Unit/Callbacks/BeforeEachTest.php index 9e366d4..a7d5899 100644 --- a/tests/Unit/Callbacks/BeforeEachTest.php +++ b/tests/Unit/Callbacks/BeforeEachTest.php @@ -14,7 +14,8 @@ ->compare([ fn () => true, fn () => true, - ]); + ]) + ->toData(); expect($result)->toHaveCount(6); }); @@ -29,7 +30,8 @@ ->compare([ 'foo' => fn () => true, 'bar' => fn () => true, - ]); + ]) + ->toData(); expect($result)->toBe([ 'foo', @@ -51,7 +53,8 @@ ->compare([ 'foo' => fn () => true, 'bar' => fn () => true, - ]); + ]) + ->toData(); expect($result)->toBe([ 'foo:1', @@ -73,7 +76,8 @@ ->compare([ fn () => true, fn () => true, - ]); + ]) + ->toData(); expect($result)->toBe([ '0:1', @@ -95,7 +99,8 @@ ->compare( fn () => true, fn () => true, - ); + ) + ->toData(); expect($result)->toBe([ '0:1', diff --git a/tests/Unit/Callbacks/BeforeTest.php b/tests/Unit/Callbacks/BeforeTest.php index c126e42..927f56c 100644 --- a/tests/Unit/Callbacks/BeforeTest.php +++ b/tests/Unit/Callbacks/BeforeTest.php @@ -14,7 +14,8 @@ ->compare([ fn () => true, fn () => true, - ]); + ]) + ->toData(); expect($result)->toHaveCount(2); }); @@ -29,7 +30,8 @@ ->compare([ 'foo' => fn () => true, 'bar' => fn () => true, - ]); + ]) + ->toData(); expect($result)->toBe([ 'foo', @@ -47,7 +49,8 @@ ->compare([ fn () => true, fn () => true, - ]); + ]) + ->toData(); expect($result)->toBe([ 0, @@ -65,7 +68,8 @@ ->compare( foo: fn () => true, bar: fn () => true, - ); + ) + ->toData(); expect($result)->toBe([ 'foo', @@ -83,7 +87,8 @@ ->compare( fn () => true, fn () => true, - ); + ) + ->toData(); expect($result)->toBe([ 0,