Skip to content

Commit 5a96e27

Browse files
Merge pull request #11 from TheDragonCode/2.x
Added the ability to pass the result of a preliminary function to the benchmark call
2 parents cad3801 + ebf1530 commit 5a96e27

File tree

4 files changed

+79
-22
lines changed

4 files changed

+79
-22
lines changed

.editorconfig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,7 @@ ij_php_align_multiline_binary_operation = false
510510
ij_php_align_multiline_chained_methods = false
511511
ij_php_align_multiline_extends_list = true
512512
ij_php_align_multiline_for = false
513-
ij_php_align_multiline_parameters = true
513+
ij_php_align_multiline_parameters = false
514514
ij_php_align_multiline_parameters_in_calls = false
515515
ij_php_align_multiline_ternary_operation = false
516516
ij_php_align_named_arguments = true
@@ -562,7 +562,7 @@ ij_php_else_if_style = combine
562562
ij_php_else_on_new_line = true
563563
ij_php_example_weight = 28
564564
ij_php_extends_keyword_wrap = normal
565-
ij_php_extends_list_wrap = off
565+
ij_php_extends_list_wrap = on_every_item
566566
ij_php_fields_default_visibility = protected
567567
ij_php_filesource_weight = 28
568568
ij_php_finally_on_new_line = true

README.md

Lines changed: 51 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010

1111
## Installation
1212

13-
To get the latest version of `The Dragon Code: Benchmark`, simply require the project using [Composer](https://getcomposer.org):
13+
To get the latest version of `The Dragon Code: Benchmark`, simply require the project
14+
using [Composer](https://getcomposer.org):
1415

1516
```bash
1617
composer require dragon-code/benchmark --dev
@@ -78,12 +79,15 @@ Result example:
7879
------- --------------------- --------------------
7980
```
8081

81-
When measuring the average value among the results, when more than 10 iterations are used, the final data is filtered by peak values. The calculation of the 10% of the lowest and
82-
10% of the highest values is excluded from the total result, thus the final data becomes cleaner and less dependent on any external factors.
82+
When measuring the average value among the results, when more than 10 iterations are used, the final data is filtered by
83+
peak values. The calculation of the 10% of the lowest and
84+
10% of the highest values is excluded from the total result, thus the final data becomes cleaner and less dependent on
85+
any external factors.
8386

8487
### Iterations Count
8588

86-
By default, the benchmark performs 100 iterations per callback, but you can change this number by calling the `iterations` method:
89+
By default, the benchmark performs 100 iterations per callback, but you can change this number by calling
90+
the `iterations` method:
8791

8892
```php
8993
use DragonCode\Benchmark\Benchmark;
@@ -117,9 +121,23 @@ If the passed value is less than 1, then one iteration will be performed for eac
117121
------- --------------------- ---------------------
118122
```
119123

124+
You can also get the number of the current execution iteration from the input parameter:
125+
126+
```php
127+
use DragonCode\Benchmark\Benchmark;
128+
129+
(new Benchmark())
130+
->iterations(5)
131+
->compare(
132+
fn (int $iteration) => /* some code */,
133+
fn (int $iteration) => /* some code */,
134+
);
135+
```
136+
120137
### Without Data
121138

122-
If you want to see only the summary result of the run time without detailed information for each iteration, then you can call the `withoutData` method, which will display only the
139+
If you want to see only the summary result of the run time without detailed information for each iteration, then you can
140+
call the `withoutData` method, which will display only the
123141
summary information:
124142

125143
```php
@@ -150,12 +168,15 @@ Result example:
150168

151169
> Note
152170
>
153-
> If the option to display detailed information is enabled (without using the `withoutData` method) and more than 1000 iterations are requested, then the output of detailed
154-
> information will be forcibly disabled, since there will be absolutely no point in it with a significantly increasing load on the computer.
171+
> If the option to display detailed information is enabled (without using the `withoutData` method) and more than 1000
172+
> iterations are requested, then the output of detailed
173+
> information will be forcibly disabled, since there will be absolutely no point in it with a significantly increasing
174+
> load on the computer.
155175
156176
### Round Precision
157177

158-
By default, the script does not round measurement results, but you can specify the number of decimal places to which rounding can be performed.
178+
By default, the script does not round measurement results, but you can specify the number of decimal places to which
179+
rounding can be performed.
159180

160181
For example:
161182

@@ -194,7 +215,8 @@ Result example:
194215

195216
### Prepare Data
196217

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.
218+
In some cases, it becomes necessary to call some action before starting each check cycle so that its time does not fall
219+
into the result of the runtime check.
198220
There is a `prepare` method for this:
199221

200222
```php
@@ -208,7 +230,8 @@ use DragonCode\Benchmark\Benchmark;
208230
);
209231
```
210232

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.
233+
When calling a callback, the name and iteration parameters are passed to it. If necessary, you can use this information
234+
inside the callback function.
212235

213236
```php
214237
use DragonCode\Benchmark\Benchmark;
@@ -221,6 +244,20 @@ use DragonCode\Benchmark\Benchmark;
221244
);
222245
```
223246

247+
You can also get the number of the current iteration and the result of the execution of the preliminary function from
248+
the input parameter:
249+
250+
```php
251+
use DragonCode\Benchmark\Benchmark;
252+
253+
(new Benchmark())
254+
->prepare(fn (mixed $name, int $iteration) => /* some code */)
255+
->compare(
256+
fn (int $iteration, mixed $prepareResult) => /* some code */,
257+
fn (int $iteration, mixed $prepareResult) => /* some code */,
258+
);
259+
```
260+
224261
## Information
225262

226263
```
@@ -242,13 +279,15 @@ use DragonCode\Benchmark\Benchmark;
242279
------- ------------------ ------------------
243280
```
244281

245-
* `foo`, `bar` - The names of the columns in the passed array. Needed for identification. By default, the array index is used, starting from zero. For example, `1, 2, 3,.. N+1`.
282+
* `foo`, `bar` - The names of the columns in the passed array. Needed for identification. By default, the array index is
283+
used, starting from zero. For example, `1, 2, 3,.. N+1`.
246284
* `1`, `2`, `3`, ..., `N+1` - Verification iteration sequence number.
247285
* `11.33 ms` - Execution time of the checked code in one iteration.
248286
* `0b`, `6.8Kb`, etc. - The amount of RAM used by the checked code.
249287
* `min` - Minimum code processing time.
250288
* `max` - Maximum code processing time.
251-
* `avg` - The arithmetic mean value among all iterations, taking into account the elimination of 10% of the smallest and 10% of the largest values to obtain a more accurate value
289+
* `avg` - The arithmetic mean value among all iterations, taking into account the elimination of 10% of the smallest and
290+
10% of the largest values to obtain a more accurate value
252291
through the possible intervention of external factors.
253292
* `total` - The total time and RAM spent on checking all iterations of the code.
254293

src/Benchmark.php

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,12 @@ public function __construct(
3333
protected Runner $runner = new Runner(),
3434
protected Transformer $transformer = new Transformer()
3535
) {
36-
$this->view = new View(new SymfonyStyle(
37-
new ArgvInput(),
38-
new ConsoleOutput()
39-
));
36+
$this->view = new View(
37+
new SymfonyStyle(
38+
new ArgvInput(),
39+
new ConsoleOutput()
40+
)
41+
);
4042
}
4143

4244
public function prepare(callable $callback): self
@@ -109,21 +111,23 @@ protected function each(mixed $name, callable $callback, ProgressBarService $pro
109111
protected function run(mixed $name, callable $callback, ProgressBarService $progressBar): void
110112
{
111113
for ($i = 1; $i <= $this->iterations; ++$i) {
112-
$this->runPrepare($name, $i);
114+
$result = $this->runPrepare($name, $i);
113115

114-
[$time, $ram] = $this->call($callback);
116+
[$time, $ram] = $this->call($callback, [$i, $result]);
115117

116118
$this->push($name, $i, $time, $ram);
117119

118120
$progressBar->advance();
119121
}
120122
}
121123

122-
protected function runPrepare(mixed $name, int $iteration): void
124+
protected function runPrepare(mixed $name, int $iteration): mixed
123125
{
124126
if ($callback = $this->prepare) {
125-
$this->call($callback, [$name, $iteration]);
127+
return $callback($name, $iteration);
126128
}
129+
130+
return null;
127131
}
128132

129133
protected function call(callable $callback, array $parameters = []): array

tests/Benchmark/PrepareTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,18 @@ public function testName(): void
7979
'bar',
8080
], $result);
8181
}
82+
83+
public function testPrepareResult(): void
84+
{
85+
$this->benchmark()
86+
->iterations(3)
87+
->withoutData()
88+
->prepare(
89+
fn (mixed $name, int $iteration) => sprintf('%s:%d', $name, $iteration)
90+
)
91+
->compare([
92+
'foo' => fn (int $iteration, string $result) => $this->assertSame('foo:' . $iteration, $result),
93+
'bar' => fn (int $iteration, string $result) => $this->assertSame('bar:' . $iteration, $result),
94+
]);
95+
}
8296
}

0 commit comments

Comments
 (0)