From 96c744037e78143bf27650af2626060a810d8dde Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Tue, 13 Jun 2023 13:43:07 +0300 Subject: [PATCH 1/4] Added the ability to pass the result of a preliminary function to the benchmark call --- src/Benchmark.php | 12 +++++++----- tests/Benchmark/PrepareTest.php | 14 ++++++++++++++ 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/src/Benchmark.php b/src/Benchmark.php index d2d9d6e..07700db 100644 --- a/src/Benchmark.php +++ b/src/Benchmark.php @@ -30,7 +30,7 @@ class Benchmark ]; public function __construct( - protected Runner $runner = new Runner(), + protected Runner $runner = new Runner(), protected Transformer $transformer = new Transformer() ) { $this->view = new View(new SymfonyStyle( @@ -109,9 +109,9 @@ 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); @@ -119,11 +119,13 @@ protected function run(mixed $name, callable $callback, ProgressBarService $prog } } - 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 diff --git a/tests/Benchmark/PrepareTest.php b/tests/Benchmark/PrepareTest.php index 9a640e7..9aa504f 100644 --- a/tests/Benchmark/PrepareTest.php +++ b/tests/Benchmark/PrepareTest.php @@ -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), + ]); + } } From 51d43cca6465e2e2cb97cecab12d3469df19744c Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Tue, 13 Jun 2023 13:44:16 +0300 Subject: [PATCH 2/4] Fixed code-style --- src/Benchmark.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Benchmark.php b/src/Benchmark.php index 07700db..34ddb84 100644 --- a/src/Benchmark.php +++ b/src/Benchmark.php @@ -30,7 +30,7 @@ class Benchmark ]; public function __construct( - protected Runner $runner = new Runner(), + protected Runner $runner = new Runner(), protected Transformer $transformer = new Transformer() ) { $this->view = new View(new SymfonyStyle( From 3e49d6cde5809cfec46166e7feb8cb6552c0a5c7 Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Tue, 13 Jun 2023 14:00:27 +0300 Subject: [PATCH 3/4] Fixed code-style --- .editorconfig | 4 ++-- src/Benchmark.php | 10 ++++++---- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/.editorconfig b/.editorconfig index 27f2533..a020f12 100644 --- a/.editorconfig +++ b/.editorconfig @@ -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 @@ -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 diff --git a/src/Benchmark.php b/src/Benchmark.php index 34ddb84..7fba1d7 100644 --- a/src/Benchmark.php +++ b/src/Benchmark.php @@ -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 From ebf153064179dce04a2153bb3f6bbce3f95db4f2 Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Tue, 13 Jun 2023 14:03:03 +0300 Subject: [PATCH 4/4] Update README.md --- README.md | 63 ++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 51 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 2dc7b1d..42b8073 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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; @@ -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 @@ -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: @@ -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 @@ -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; @@ -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 ``` @@ -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.