Skip to content

Commit

Permalink
Merge pull request #11 from TheDragonCode/2.x
Browse files Browse the repository at this point in the history
Added the ability to pass the result of a preliminary function to the benchmark call
  • Loading branch information
andrey-helldar authored Jun 13, 2023
2 parents cad3801 + ebf1530 commit 5a96e27
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 22 deletions.
4 changes: 2 additions & 2 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,7 @@ ij_php_align_multiline_binary_operation = false
ij_php_align_multiline_chained_methods = false
ij_php_align_multiline_extends_list = true
ij_php_align_multiline_for = false
ij_php_align_multiline_parameters = true
ij_php_align_multiline_parameters = false
ij_php_align_multiline_parameters_in_calls = false
ij_php_align_multiline_ternary_operation = false
ij_php_align_named_arguments = true
Expand Down Expand Up @@ -562,7 +562,7 @@ ij_php_else_if_style = combine
ij_php_else_on_new_line = true
ij_php_example_weight = 28
ij_php_extends_keyword_wrap = normal
ij_php_extends_list_wrap = off
ij_php_extends_list_wrap = on_every_item
ij_php_fields_default_visibility = protected
ij_php_filesource_weight = 28
ij_php_finally_on_new_line = true
Expand Down
63 changes: 51 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@

## Installation

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

```bash
composer require dragon-code/benchmark --dev
Expand Down Expand Up @@ -78,12 +79,15 @@ Result example:
------- --------------------- --------------------
```

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
10% of the highest values is excluded from the total result, thus the final data becomes cleaner and less dependent on any external factors.
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
10% of the highest values is excluded from the total result, thus the final data becomes cleaner and less dependent on
any external factors.

### Iterations Count

By default, the benchmark performs 100 iterations per callback, but you can change this number by calling the `iterations` method:
By default, the benchmark performs 100 iterations per callback, but you can change this number by calling
the `iterations` method:

```php
use DragonCode\Benchmark\Benchmark;
Expand Down Expand Up @@ -117,9 +121,23 @@ If the passed value is less than 1, then one iteration will be performed for eac
------- --------------------- ---------------------
```

You can also get the number of the current execution iteration from the input parameter:

```php
use DragonCode\Benchmark\Benchmark;

(new Benchmark())
->iterations(5)
->compare(
fn (int $iteration) => /* some code */,
fn (int $iteration) => /* some code */,
);
```

### Without Data

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
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
summary information:

```php
Expand Down Expand Up @@ -150,12 +168,15 @@ Result example:

> Note
>
> 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
> information will be forcibly disabled, since there will be absolutely no point in it with a significantly increasing load on the computer.
> 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
> information will be forcibly disabled, since there will be absolutely no point in it with a significantly increasing
> load on the computer.
### Round Precision

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

For example:

Expand Down Expand Up @@ -194,7 +215,8 @@ Result example:

### Prepare Data

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.
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.
There is a `prepare` method for this:

```php
Expand All @@ -208,7 +230,8 @@ use DragonCode\Benchmark\Benchmark;
);
```

When calling a callback, the name and iteration parameters are passed to it. If necessary, you can use this information inside the callback function.
When calling a callback, the name and iteration parameters are passed to it. If necessary, you can use this information
inside the callback function.

```php
use DragonCode\Benchmark\Benchmark;
Expand All @@ -221,6 +244,20 @@ use DragonCode\Benchmark\Benchmark;
);
```

You can also get the number of the current iteration and the result of the execution of the preliminary function from
the input parameter:

```php
use DragonCode\Benchmark\Benchmark;

(new Benchmark())
->prepare(fn (mixed $name, int $iteration) => /* some code */)
->compare(
fn (int $iteration, mixed $prepareResult) => /* some code */,
fn (int $iteration, mixed $prepareResult) => /* some code */,
);
```

## Information

```
Expand All @@ -242,13 +279,15 @@ use DragonCode\Benchmark\Benchmark;
------- ------------------ ------------------
```

* `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`.
* `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`.
* `1`, `2`, `3`, ..., `N+1` - Verification iteration sequence number.
* `11.33 ms` - Execution time of the checked code in one iteration.
* `0b`, `6.8Kb`, etc. - The amount of RAM used by the checked code.
* `min` - Minimum code processing time.
* `max` - Maximum code processing time.
* `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
* `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
through the possible intervention of external factors.
* `total` - The total time and RAM spent on checking all iterations of the code.

Expand Down
20 changes: 12 additions & 8 deletions src/Benchmark.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,12 @@ public function __construct(
protected Runner $runner = new Runner(),
protected Transformer $transformer = new Transformer()
) {
$this->view = new View(new SymfonyStyle(
new ArgvInput(),
new ConsoleOutput()
));
$this->view = new View(
new SymfonyStyle(
new ArgvInput(),
new ConsoleOutput()
)
);
}

public function prepare(callable $callback): self
Expand Down Expand Up @@ -109,21 +111,23 @@ protected function each(mixed $name, callable $callback, ProgressBarService $pro
protected function run(mixed $name, callable $callback, ProgressBarService $progressBar): void
{
for ($i = 1; $i <= $this->iterations; ++$i) {
$this->runPrepare($name, $i);
$result = $this->runPrepare($name, $i);

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

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

$progressBar->advance();
}
}

protected function runPrepare(mixed $name, int $iteration): void
protected function runPrepare(mixed $name, int $iteration): mixed
{
if ($callback = $this->prepare) {
$this->call($callback, [$name, $iteration]);
return $callback($name, $iteration);
}

return null;
}

protected function call(callable $callback, array $parameters = []): array
Expand Down
14 changes: 14 additions & 0 deletions tests/Benchmark/PrepareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,18 @@ public function testName(): void
'bar',
], $result);
}

public function testPrepareResult(): void
{
$this->benchmark()
->iterations(3)
->withoutData()
->prepare(
fn (mixed $name, int $iteration) => sprintf('%s:%d', $name, $iteration)
)
->compare([
'foo' => fn (int $iteration, string $result) => $this->assertSame('foo:' . $iteration, $result),
'bar' => fn (int $iteration, string $result) => $this->assertSame('bar:' . $iteration, $result),
]);
}
}

0 comments on commit 5a96e27

Please sign in to comment.