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
46 changes: 18 additions & 28 deletions src/Benchmark.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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;
}
Expand All @@ -116,6 +108,8 @@ public function compare(array|Closure ...$callbacks): static
*/
public function toData(): array
{
$this->performCallbacks();

return $this->result->get(
$this->collector->all()
);
Expand 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);
Expand All @@ -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);
Expand Down Expand Up @@ -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();
Expand Down
30 changes: 30 additions & 0 deletions src/Services/CallbacksService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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);
Expand Down Expand Up @@ -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);
}
}
}
24 changes: 14 additions & 10 deletions tests/.pest/snapshots/Unit/Result/ToConsoleTest/output.snap
Original file line number Diff line number Diff line change
@@ -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 |
+-------+-------------------------------+------------------------------+
Expand Down
24 changes: 14 additions & 10 deletions tests/.pest/snapshots/Unit/Round/RoundTest/default.snap
Original file line number Diff line number Diff line change
@@ -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 |
+-------+-------------------------------+------------------------------+
Expand Down
Original file line number Diff line number Diff line change
@@ -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 |
+-------+--------------------+-------------------+
Expand Down
Original file line number Diff line number Diff line change
@@ -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 |
+-------+--------------------+-------------------+
Expand Down
Original file line number Diff line number Diff line change
@@ -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 |
+-------+-----------------------+----------------------+
Expand Down
Original file line number Diff line number Diff line change
@@ -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 |
+-------+-----------------------+----------------------+
Expand Down
Original file line number Diff line number Diff line change
@@ -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 |
+-------+--------------------------+------------------------+
Expand Down
Original file line number Diff line number Diff line change
@@ -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 |
+-------+--------------------------+------------------------+
Expand Down
15 changes: 10 additions & 5 deletions tests/Unit/Callbacks/AfterEachTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
->compare([
fn () => true,
fn () => true,
]);
])
->toData();

expect($result)->toHaveCount(6);
});
Expand All @@ -29,7 +30,8 @@
->compare([
'foo' => fn () => true,
'bar' => fn () => true,
]);
])
->toData();

expect($result)->toBe([
'foo',
Expand All @@ -51,7 +53,8 @@
->compare([
'foo' => fn () => true,
'bar' => fn () => true,
]);
])
->toData();

expect($result)->toBe([
'foo:1',
Expand All @@ -73,7 +76,8 @@
->compare([
fn () => true,
fn () => true,
]);
])
->toData();

expect($result)->toBe([
'0:1',
Expand All @@ -95,7 +99,8 @@
->compare(
fn () => true,
fn () => true,
);
)
->toData();

expect($result)->toBe([
'0:1',
Expand Down
Loading