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
54 changes: 52 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ Or manually update `require-dev` block of `composer.json` and run `composer upda
### Quick Start

```php
use DragonCode\Benchmark\Benchmark;
use function DragonCode\Benchmark\bench;

new Benchmark()
bench()
->compare(
static fn () => true,
static fn () => true
Expand All @@ -49,6 +49,56 @@ new Benchmark()

### How To Use

You can use both the `bench()` function and the `Benchmark` class.

```php
use DragonCode\Benchmark\Benchmark;

use function DragonCode\Benchmark\bench;

bench()->compare([
fn () => /* some code */,
fn () => /* some code */,
])->toConsole();

new Benchmark()->compare([
fn () => /* some code */,
fn () => /* some code */,
])->toConsole();
```

#### As a function

```php
use function DragonCode\Benchmark\bench;

// Array without named keys
bench()->compare([
fn () => /* some code */,
fn () => /* some code */,
])->toConsole();

// Array with named keys
bench()->compare([
'foo' => fn () => /* some code */,
'bar' => fn () => /* some code */,
])->toConsole();

// Callbacks without named parameters
bench()->compare(
fn () => /* some code */,
fn () => /* some code */,
)->toConsole();

// Callbacks with named parameters
bench()->compare(
foo: fn () => /* some code */,
bar: fn () => /* some code */,
)->toConsole();
```

#### As a class

```php
use DragonCode\Benchmark\Benchmark;

Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@
"autoload": {
"psr-4": {
"DragonCode\\Benchmark\\": "src/"
}
},
"files": ["src/helpers.php"]
},
"autoload-dev": {
"psr-4": {
Expand Down
12 changes: 12 additions & 0 deletions src/helpers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace DragonCode\Benchmark;

if (! function_exists('\DragonCode\Benchmark\bench')) {
function bench(): Benchmark
{
return new Benchmark;
}
}
6 changes: 6 additions & 0 deletions tests/.pest/snapshots/Unit/Result/HelperTest/compare.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@


0/100 [░░░░░░░░░░░░░░░░░░░░░░░░░░░░] 0% 1/100 [░░░░░░░░░░░░░░░░░░░░░░░░░░░░] 1% 2/100 [░░░░░░░░░░░░░░░░░░░░░░░░░░░░] 2% 3/100 [░░░░░░░░░░░░░░░░░░░░░░░░░░░░] 3% 4/100 [▓░░░░░░░░░░░░░░░░░░░░░░░░░░░] 4% 5/100 [▓░░░░░░░░░░░░░░░░░░░░░░░░░░░] 5% 6/100 [▓░░░░░░░░░░░░░░░░░░░░░░░░░░░] 6% 7/100 [▓░░░░░░░░░░░░░░░░░░░░░░░░░░░] 7% 8/100 [▓▓░░░░░░░░░░░░░░░░░░░░░░░░░░] 8% 9/100 [▓▓░░░░░░░░░░░░░░░░░░░░░░░░░░] 9% 10/100 [▓▓░░░░░░░░░░░░░░░░░░░░░░░░░░] 10% 11/100 [▓▓▓░░░░░░░░░░░░░░░░░░░░░░░░░] 11% 12/100 [▓▓▓░░░░░░░░░░░░░░░░░░░░░░░░░] 12% 13/100 [▓▓▓░░░░░░░░░░░░░░░░░░░░░░░░░] 13% 14/100 [▓▓▓░░░░░░░░░░░░░░░░░░░░░░░░░] 14% 15/100 [▓▓▓▓░░░░░░░░░░░░░░░░░░░░░░░░] 15% 16/100 [▓▓▓▓░░░░░░░░░░░░░░░░░░░░░░░░] 16% 17/100 [▓▓▓▓░░░░░░░░░░░░░░░░░░░░░░░░] 17% 18/100 [▓▓▓▓▓░░░░░░░░░░░░░░░░░░░░░░░] 18% 19/100 [▓▓▓▓▓░░░░░░░░░░░░░░░░░░░░░░░] 19% 20/100 [▓▓▓▓▓░░░░░░░░░░░░░░░░░░░░░░░] 20% 21/100 [▓▓▓▓▓░░░░░░░░░░░░░░░░░░░░░░░] 21% 22/100 [▓▓▓▓▓▓░░░░░░░░░░░░░░░░░░░░░░] 22% 23/100 [▓▓▓▓▓▓░░░░░░░░░░░░░░░░░░░░░░] 23% 24/100 [▓▓▓▓▓▓░░░░░░░░░░░░░░░░░░░░░░] 24% 25/100 [▓▓▓▓▓▓▓░░░░░░░░░░░░░░░░░░░░░] 25% 26/100 [▓▓▓▓▓▓▓░░░░░░░░░░░░░░░░░░░░░] 26% 27/100 [▓▓▓▓▓▓▓░░░░░░░░░░░░░░░░░░░░░] 27% 28/100 [▓▓▓▓▓▓▓░░░░░░░░░░░░░░░░░░░░░] 28% 29/100 [▓▓▓▓▓▓▓▓░░░░░░░░░░░░░░░░░░░░] 28% 30/100 [▓▓▓▓▓▓▓▓░░░░░░░░░░░░░░░░░░░░] 30% 31/100 [▓▓▓▓▓▓▓▓░░░░░░░░░░░░░░░░░░░░] 31% 32/100 [▓▓▓▓▓▓▓▓░░░░░░░░░░░░░░░░░░░░] 32% 33/100 [▓▓▓▓▓▓▓▓▓░░░░░░░░░░░░░░░░░░░] 33% 34/100 [▓▓▓▓▓▓▓▓▓░░░░░░░░░░░░░░░░░░░] 34% 35/100 [▓▓▓▓▓▓▓▓▓░░░░░░░░░░░░░░░░░░░] 35% 36/100 [▓▓▓▓▓▓▓▓▓▓░░░░░░░░░░░░░░░░░░] 36% 37/100 [▓▓▓▓▓▓▓▓▓▓░░░░░░░░░░░░░░░░░░] 37% 38/100 [▓▓▓▓▓▓▓▓▓▓░░░░░░░░░░░░░░░░░░] 38% 39/100 [▓▓▓▓▓▓▓▓▓▓░░░░░░░░░░░░░░░░░░] 39% 40/100 [▓▓▓▓▓▓▓▓▓▓▓░░░░░░░░░░░░░░░░░] 40% 41/100 [▓▓▓▓▓▓▓▓▓▓▓░░░░░░░░░░░░░░░░░] 41% 42/100 [▓▓▓▓▓▓▓▓▓▓▓░░░░░░░░░░░░░░░░░] 42% 43/100 [▓▓▓▓▓▓▓▓▓▓▓▓░░░░░░░░░░░░░░░░] 43% 44/100 [▓▓▓▓▓▓▓▓▓▓▓▓░░░░░░░░░░░░░░░░] 44% 45/100 [▓▓▓▓▓▓▓▓▓▓▓▓░░░░░░░░░░░░░░░░] 45% 46/100 [▓▓▓▓▓▓▓▓▓▓▓▓░░░░░░░░░░░░░░░░] 46% 47/100 [▓▓▓▓▓▓▓▓▓▓▓▓▓░░░░░░░░░░░░░░░] 47% 48/100 [▓▓▓▓▓▓▓▓▓▓▓▓▓░░░░░░░░░░░░░░░] 48% 49/100 [▓▓▓▓▓▓▓▓▓▓▓▓▓░░░░░░░░░░░░░░░] 49% 50/100 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓░░░░░░░░░░░░░░] 50% 51/100 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓░░░░░░░░░░░░░░] 51% 52/100 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓░░░░░░░░░░░░░░] 52% 53/100 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓░░░░░░░░░░░░░░] 53% 54/100 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓░░░░░░░░░░░░░] 54% 55/100 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓░░░░░░░░░░░░░] 55% 56/100 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓░░░░░░░░░░░░░] 56% 57/100 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓░░░░░░░░░░░░░] 56% 58/100 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓░░░░░░░░░░░░] 57% 59/100 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓░░░░░░░░░░░░] 59% 60/100 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓░░░░░░░░░░░░] 60% 61/100 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓░░░░░░░░░░░] 61% 62/100 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓░░░░░░░░░░░] 62% 63/100 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓░░░░░░░░░░░] 63% 64/100 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓░░░░░░░░░░░] 64% 65/100 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓░░░░░░░░░░] 65% 66/100 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓░░░░░░░░░░] 66% 67/100 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓░░░░░░░░░░] 67% 68/100 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓░░░░░░░░░] 68% 69/100 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓░░░░░░░░░] 69% 70/100 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓░░░░░░░░░] 70% 71/100 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓░░░░░░░░░] 71% 72/100 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓░░░░░░░░] 72% 73/100 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓░░░░░░░░] 73% 74/100 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓░░░░░░░░] 74% 75/100 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓░░░░░░░] 75% 76/100 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓░░░░░░░] 76% 77/100 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓░░░░░░░] 77% 78/100 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓░░░░░░░] 78% 79/100 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓░░░░░░] 79% 80/100 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓░░░░░░] 80% 81/100 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓░░░░░░] 81% 82/100 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓░░░░░░] 82% 83/100 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓░░░░░] 83% 84/100 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓░░░░░] 84% 85/100 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓░░░░░] 85% 86/100 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓░░░░] 86% 87/100 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓░░░░] 87% 88/100 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓░░░░] 88% 89/100 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓░░░░] 89% 90/100 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓░░░] 90% 91/100 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓░░░] 91% 92/100 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓░░░] 92% 93/100 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓░░] 93% 94/100 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓░░] 94% 95/100 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓░░] 95% 96/100 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓░░] 96% 97/100 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓░] 97% 98/100 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓░] 98% 99/100 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓░] 99% 100/100 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100% 100/100 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100%



Expand Down
6 changes: 6 additions & 0 deletions tests/.pest/snapshots/Unit/Result/HelperTest/deviations.snap

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion tests/Unit/Architecture/InstanceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

use DragonCode\Benchmark\Benchmark;

use function DragonCode\Benchmark\bench;

test('dynamic', function () {
expect(new Benchmark)->toBeInstanceOf(Benchmark::class);
});
Expand All @@ -15,5 +17,5 @@
});

test('helper', function () {
expect(benchmark())->toBeInstanceOf(Benchmark::class);
expect(bench())->toBeInstanceOf(Benchmark::class);
});
22 changes: 22 additions & 0 deletions tests/Unit/Result/HelperTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

use function DragonCode\Benchmark\bench;

test('compare', function () {
bench()
->compare(fn () => true)
->toData();

expectOutputToMatchSnapshot();
});

test('deviations', function () {
bench()
->deviations()
->compare(fn () => true)
->toData();

expectOutputToMatchSnapshot();
});
Loading