Skip to content

Commit 498aa8b

Browse files
Merge pull request #7 from TheDragonCode/2.x
Added `prepare` method
2 parents cb2b0c8 + 06ce9b1 commit 498aa8b

File tree

4 files changed

+136
-6
lines changed

4 files changed

+136
-6
lines changed

README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,35 @@ Result example:
192192
------- ------------------ ------------------
193193
```
194194

195+
### Prepare Data
196+
197+
In some cases, it becomes necessary to call some action before starting each check cycle so that its time does not fall into the result of the runtime check.
198+
There is a `prepare` method for this:
199+
200+
```php
201+
use DragonCode\Benchmark\Benchmark;
202+
203+
(new Benchmark())
204+
->prepare(fn () => /* some code */)
205+
->compare(
206+
fn () => /* some code */,
207+
fn () => /* some code */,
208+
);
209+
```
210+
211+
When calling a callback, the name and iteration parameters are passed to it. If necessary, you can use this information inside the callback function.
212+
213+
```php
214+
use DragonCode\Benchmark\Benchmark;
215+
216+
(new Benchmark())
217+
->prepare(fn (mixed $name, int $iteration) => /* some code */)
218+
->compare(
219+
fn () => /* some code */,
220+
fn () => /* some code */,
221+
);
222+
```
223+
195224
## Information
196225

197226
```

src/Benchmark.php

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace DragonCode\Benchmark;
66

7+
use Closure;
78
use DragonCode\Benchmark\Exceptions\ValueIsNotCallableException;
89
use DragonCode\Benchmark\Services\Runner;
910
use DragonCode\Benchmark\Services\View;
@@ -21,6 +22,8 @@ class Benchmark
2122

2223
protected bool $withData = true;
2324

25+
protected ?Closure $prepare = null;
26+
2427
protected array $result = [
2528
'each' => [],
2629
'total' => [],
@@ -36,6 +39,13 @@ public function __construct(
3639
));
3740
}
3841

42+
public function prepare(callable $callback): self
43+
{
44+
$this->prepare = $callback;
45+
46+
return $this;
47+
}
48+
3949
public function iterations(int $count): self
4050
{
4151
$this->iterations = max(1, $count);
@@ -99,6 +109,8 @@ protected function each(mixed $name, callable $callback, ProgressBarService $pro
99109
protected function run(mixed $name, callable $callback, ProgressBarService $progressBar): void
100110
{
101111
for ($i = 1; $i <= $this->iterations; ++$i) {
112+
$this->runPrepare($name, $i);
113+
102114
[$time, $ram] = $this->call($callback);
103115

104116
$this->push($name, $i, $time, $ram);
@@ -107,9 +119,16 @@ protected function run(mixed $name, callable $callback, ProgressBarService $prog
107119
}
108120
}
109121

110-
protected function call(callable $callback): array
122+
protected function runPrepare(mixed $name, int $iteration): void
123+
{
124+
if ($callback = $this->prepare) {
125+
$this->call($callback, [$name, $iteration]);
126+
}
127+
}
128+
129+
protected function call(callable $callback, array $parameters = []): array
111130
{
112-
return $this->runner->call($callback);
131+
return $this->runner->call($callback, $parameters);
113132
}
114133

115134
protected function push(mixed $name, int $iteration, float $time, float $ram): void

src/Services/Runner.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,24 @@ public function __construct(
1111
) {
1212
}
1313

14-
public function call(callable $callback): array
14+
public function call(callable $callback, array $parameters = []): array
1515
{
1616
$this->clean();
1717

18-
return $this->run($callback);
18+
return $this->run($callback, $parameters);
1919
}
2020

2121
protected function clean(): void
2222
{
2323
gc_collect_cycles();
2424
}
2525

26-
protected function run(callable $callback): array
26+
protected function run(callable $callback, array $parameters = []): array
2727
{
2828
$ramFrom = $this->memory->now();
2929
$startAt = hrtime(true);
3030

31-
$callback();
31+
$callback(...$parameters);
3232

3333
$time = $this->diff(hrtime(true), $startAt);
3434
$ram = $this->memory->diff($ramFrom);

tests/Benchmark/PrepareTest.php

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests\Benchmark;
6+
7+
use Tests\TestCase;
8+
9+
class PrepareTest extends TestCase
10+
{
11+
public function testCallback(): void
12+
{
13+
$result = [];
14+
15+
$this->benchmark()
16+
->iterations(3)
17+
->withoutData()
18+
->prepare(function () use (&$result) {
19+
$result[] = 1;
20+
})
21+
->compare([
22+
'foo' => fn () => $this->work(),
23+
'bar' => fn () => $this->work(),
24+
]);
25+
26+
$this->assertSame(6, count($result));
27+
}
28+
29+
public function testParameters(): void
30+
{
31+
$result = [];
32+
33+
$this->benchmark()
34+
->iterations(3)
35+
->withoutData()
36+
->prepare(function (mixed $name, int $iteration) use (&$result) {
37+
$result[] = sprintf('%s:%d', $name, $iteration);
38+
})
39+
->compare([
40+
'foo' => fn () => $this->work(),
41+
'bar' => fn () => $this->work(),
42+
]);
43+
44+
$this->assertSame(6, count($result));
45+
46+
$this->assertSame([
47+
'foo:1',
48+
'foo:2',
49+
'foo:3',
50+
'bar:1',
51+
'bar:2',
52+
'bar:3',
53+
], $result);
54+
}
55+
56+
public function testName(): void
57+
{
58+
$result = [];
59+
60+
$this->benchmark()
61+
->iterations(3)
62+
->withoutData()
63+
->prepare(function (mixed $name) use (&$result) {
64+
$result[] = $name;
65+
})
66+
->compare([
67+
'foo' => fn () => $this->work(),
68+
'bar' => fn () => $this->work(),
69+
]);
70+
71+
$this->assertSame(6, count($result));
72+
73+
$this->assertSame([
74+
'foo',
75+
'foo',
76+
'foo',
77+
'bar',
78+
'bar',
79+
'bar',
80+
], $result);
81+
}
82+
}

0 commit comments

Comments
 (0)