Skip to content

Commit 67a4388

Browse files
Add bench helper function
1 parent 0c532ab commit 67a4388

File tree

7 files changed

+103
-4
lines changed

7 files changed

+103
-4
lines changed

README.md

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ Or manually update `require-dev` block of `composer.json` and run `composer upda
3737
### Quick Start
3838

3939
```php
40-
use DragonCode\Benchmark\Benchmark;
40+
use function DragonCode\Benchmark\bench;
4141

42-
new Benchmark()
42+
bench()
4343
->compare(
4444
static fn () => true,
4545
static fn () => true
@@ -49,6 +49,56 @@ new Benchmark()
4949

5050
### How To Use
5151

52+
You can use both the `bench()` function and the `Benchmark` class.
53+
54+
```php
55+
use DragonCode\Benchmark\Benchmark;
56+
57+
use function DragonCode\Benchmark\bench;
58+
59+
bench()->compare([
60+
fn () => /* some code */,
61+
fn () => /* some code */,
62+
])->toConsole();
63+
64+
new Benchmark()->compare([
65+
fn () => /* some code */,
66+
fn () => /* some code */,
67+
])->toConsole();
68+
```
69+
70+
#### As a function
71+
72+
```php
73+
use function DragonCode\Benchmark\bench;
74+
75+
// Array without named keys
76+
bench()->compare([
77+
fn () => /* some code */,
78+
fn () => /* some code */,
79+
])->toConsole();
80+
81+
// Array with named keys
82+
bench()->compare([
83+
'foo' => fn () => /* some code */,
84+
'bar' => fn () => /* some code */,
85+
])->toConsole();
86+
87+
// Callbacks without named parameters
88+
bench()->compare(
89+
fn () => /* some code */,
90+
fn () => /* some code */,
91+
)->toConsole();
92+
93+
// Callbacks with named parameters
94+
bench()->compare(
95+
foo: fn () => /* some code */,
96+
bar: fn () => /* some code */,
97+
)->toConsole();
98+
```
99+
100+
#### As a class
101+
52102
```php
53103
use DragonCode\Benchmark\Benchmark;
54104

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@
4949
"autoload": {
5050
"psr-4": {
5151
"DragonCode\\Benchmark\\": "src/"
52-
}
52+
},
53+
"files": ["src/helpers.php"]
5354
},
5455
"autoload-dev": {
5556
"psr-4": {

src/helpers.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace DragonCode\Benchmark;
6+
7+
if (! function_exists('\DragonCode\Benchmark\bench')) {
8+
function bench(): Benchmark
9+
{
10+
return new Benchmark;
11+
}
12+
}

tests/.pest/snapshots/Unit/Result/HelperTest/compare.snap

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
3+
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%
4+
5+
6+

tests/.pest/snapshots/Unit/Result/HelperTest/deviations.snap

Lines changed: 6 additions & 0 deletions
Large diffs are not rendered by default.

tests/Unit/Architecture/InstanceTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
use DragonCode\Benchmark\Benchmark;
88

9+
use function DragonCode\Benchmark\bench;
10+
911
test('dynamic', function () {
1012
expect(new Benchmark)->toBeInstanceOf(Benchmark::class);
1113
});
@@ -15,5 +17,5 @@
1517
});
1618

1719
test('helper', function () {
18-
expect(benchmark())->toBeInstanceOf(Benchmark::class);
20+
expect(bench())->toBeInstanceOf(Benchmark::class);
1921
});

tests/Unit/Result/HelperTest.php

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+
use function DragonCode\Benchmark\bench;
6+
7+
test('compare', function () {
8+
bench()
9+
->compare(fn () => true)
10+
->toData();
11+
12+
expectOutputToMatchSnapshot();
13+
});
14+
15+
test('deviations', function () {
16+
bench()
17+
->deviations()
18+
->compare(fn () => true)
19+
->toData();
20+
21+
expectOutputToMatchSnapshot();
22+
});

0 commit comments

Comments
 (0)