Skip to content

Commit

Permalink
tests: Refactor benchmarks.
Browse files Browse the repository at this point in the history
(cherry picked from commit e42a5af45e90f861ce279bc6cd25511fc65e2a84)
  • Loading branch information
drupol committed Jan 6, 2022
1 parent a069835 commit d80150d
Show file tree
Hide file tree
Showing 12 changed files with 133 additions and 258 deletions.
1 change: 1 addition & 0 deletions phpbench.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"opcache.enable_cli": true,
"xdebug.mode": "off"
},
"runner.file_pattern": "*Bench.php",
"runner.time_unit": "microseconds",
"core.profiles": {
"local": {
Expand Down
36 changes: 10 additions & 26 deletions tests/benchmarks/CachingIteratorsAggregateBench.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,24 @@
use loophp\iterators\CachingIteratorAggregate;
use loophp\iterators\SimpleCachingIteratorAggregate;
use PhpBench\Benchmark\Metadata\Annotations\Groups;
use PhpBench\Benchmark\Metadata\Annotations\ParamProviders;
use Psl\Iter\Iterator as IterIterator;
use Traversable;

/**
* @Groups({"CachingIteratorsAggregateBench"})
*/
final class CachingIteratorsAggregateBench
final class CachingIteratorsAggregateBench extends IteratorBenchmark
{
/**
* @ParamProviders("provideGenerators")
*/
public function benchIterator(array $params): void
public function bench(array $params): void
{
$generator = static function (int $from, int $to): Generator {
for ($i = $from; $i < $to; ++$i) {
yield $i;
}
};

$iterator = new $params['class']($generator(0, $params['size']));

$this->test($iterator, $params['size']);
$this->doBench(
$this->getSubject($params),
$params
);
}

public function provideGenerators(): Generator
Expand All @@ -57,21 +53,9 @@ public function provideGenerators(): Generator
];
}

private function loop(Traversable $input, int $breakAt): Generator
{
foreach ($input as $key => $value) {
if (0 === $breakAt--) {
break;
}
}

foreach ($input as $key => $value) {
yield [$key, $value];
}
}

private function test(Traversable $input, int $size): void
protected function doBench(Traversable $input, array $params): void
{
iterator_to_array($this->loop($input, $size / 2));
iterator_to_array($this->loopUntil($input, $params));
iterator_to_array($this->loop($input));
}
}
31 changes: 8 additions & 23 deletions tests/benchmarks/ClosureIteratorAggregateBench.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,22 @@
use Generator;
use loophp\iterators\ClosureIteratorAggregate;
use PhpBench\Benchmark\Metadata\Annotations\Groups;
use PhpBench\Benchmark\Metadata\Annotations\ParamProviders;
use Traversable;

/**
* @Groups({"ci", "local"})
*/
final class ClosureIteratorAggregateBench
final class ClosureIteratorAggregateBench extends IteratorBenchmark
{
/**
* @ParamProviders("provideGenerators")
*/
public function benchIterator(array $params): void
public function bench(array $params): void
{
$callable = Closure::fromCallable([ClosureIteratorAggregateBench::class, 'loop']);

$this->test(
new $params['class']($callable, [$this->getGenerator($params)]),
$params['size']
$this->doBench(
$this->getSubject($params),
$params
);
}

Expand All @@ -43,22 +42,8 @@ public function provideGenerators(): Generator
];
}

private function getGenerator(array $params): Generator
{
for ($i = 0; $i < $params['size']; ++$i) {
yield [$i, sprintf('*%s*', $i)];
}
}

private function loop(Traversable $input): Generator
{
foreach ($input as $key => $value) {
yield [$key, $value];
}
}

private function test(ClosureIteratorAggregate $input): void
protected function getSubject(array $params): Traversable
{
iterator_to_array($this->loop($input));
return new $params['class'](Closure::fromCallable([__CLASS__, 'loop']), [$this->getGenerator($params)]);
}
}
30 changes: 8 additions & 22 deletions tests/benchmarks/ClosureIteratorBench.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,22 @@
use Generator;
use loophp\iterators\ClosureIterator;
use PhpBench\Benchmark\Metadata\Annotations\Groups;
use PhpBench\Benchmark\Metadata\Annotations\ParamProviders;
use Traversable;

/**
* @Groups({"ci", "local"})
*/
final class ClosureIteratorBench
final class ClosureIteratorBench extends IteratorBenchmark
{
/**
* @ParamProviders("provideGenerators")
*/
public function benchIterator(array $params): void
public function bench(array $params): void
{
$callable = Closure::fromCallable([ClosureIteratorBench::class, 'loop']);

$this->test(
new $params['class']($callable, [$this->getGenerator($params)])
$this->doBench(
$this->getSubject($params),
$params
);
}

Expand All @@ -42,22 +42,8 @@ public function provideGenerators(): Generator
];
}

private function getGenerator(array $params): Generator
{
for ($i = 0; $i < $params['size']; ++$i) {
yield [$i, sprintf('*%s*', $i)];
}
}

private function loop(Traversable $input): Generator
{
foreach ($input as $key => $value) {
yield [$key, $value];
}
}

private function test(ClosureIterator $input): void
protected function getSubject(array $params): Traversable
{
iterator_to_array($this->loop($input));
return new $params['class'](Closure::fromCallable([__CLASS__, 'loop']), [$this->getGenerator($params)]);
}
}
31 changes: 6 additions & 25 deletions tests/benchmarks/IterableIteratorAggregateBench.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,21 @@
use Generator;
use loophp\iterators\IterableIteratorAggregate;
use PhpBench\Benchmark\Metadata\Annotations\Groups;
use Traversable;
use PhpBench\Benchmark\Metadata\Annotations\ParamProviders;

/**
* @Groups({"ci", "local"})
*/
final class IterableIteratorAggregateBench
final class IterableIteratorAggregateBench extends IteratorBenchmark
{
/**
* @ParamProviders("provideGenerators")
*/
public function benchIterator(array $params): void
public function bench(array $params): void
{
$this->test(
new $params['class']($this->getGenerator($params)),
$params['size']
$this->doBench(
$this->getSubject($params),
$params
);
}

Expand All @@ -39,23 +39,4 @@ public function provideGenerators(): Generator
'size' => $items,
];
}

private function getGenerator(array $params): Generator
{
for ($i = 0; $i < $params['size']; ++$i) {
yield [$i, sprintf('*%s*', $i)];
}
}

private function loop(Traversable $input): Generator
{
foreach ($input as $key => $value) {
yield [$key, $value];
}
}

private function test(IterableIteratorAggregate $input): void
{
iterator_to_array($this->loop($input));
}
}
31 changes: 6 additions & 25 deletions tests/benchmarks/IterableIteratorBench.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,21 @@
use Generator;
use loophp\iterators\IterableIterator;
use PhpBench\Benchmark\Metadata\Annotations\Groups;
use Traversable;
use PhpBench\Benchmark\Metadata\Annotations\ParamProviders;

/**
* @Groups({"ci", "local"})
*/
final class IterableIteratorBench
final class IterableIteratorBench extends IteratorBenchmark
{
/**
* @ParamProviders("provideGenerators")
*/
public function benchIterator(array $params): void
public function bench(array $params): void
{
$this->test(
new $params['class']($this->getGenerator($params)),
$params['size']
$this->doBench(
$this->getSubject($params),
$params
);
}

Expand All @@ -39,23 +39,4 @@ public function provideGenerators(): Generator
'size' => $items,
];
}

private function getGenerator(array $params): Generator
{
for ($i = 0; $i < $params['size']; ++$i) {
yield [$i, sprintf('*%s*', $i)];
}
}

private function loop(Traversable $input): Generator
{
foreach ($input as $key => $value) {
yield [$key, $value];
}
}

private function test(IterableIterator $input): void
{
iterator_to_array($this->loop($input));
}
}
61 changes: 61 additions & 0 deletions tests/benchmarks/IteratorBenchmark.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

/**
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/

declare(strict_types=1);

namespace benchmarks\loophp\iterators;

use Generator;
use PhpBench\Benchmark\Metadata\Annotations\ParamProviders;
use Traversable;

abstract class IteratorBenchmark
{
/**
* @ParamProviders("provideGenerators")
*/
abstract public function bench(array $params): void;

abstract public function provideGenerators(): Generator;

protected function doBench(Traversable $input, array $params): void
{
iterator_to_array($this->loop($input));
}

protected function getGenerator(array $params): Generator
{
for ($i = 0; $i < $params['size']; ++$i) {
yield [$i, sprintf('*%s*', $i)];
}
}

protected function getSubject(array $params): Traversable
{
return new $params['class']($this->getGenerator($params));
}

protected function loop(Traversable $input): Generator
{
foreach ($input as $key => $value) {
yield [$key, $value];
}
}

protected function loopUntil(Traversable $input, array $params): Generator
{
$breakAt = $params['size'] / 2;

foreach ($input as $key => $value) {
yield $key => $value;

if (0 === $breakAt--) {
break;
}
}
}
}
31 changes: 6 additions & 25 deletions tests/benchmarks/PackIterableAggregateBench.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,21 @@
use Generator;
use loophp\iterators\PackIterableAggregate;
use PhpBench\Benchmark\Metadata\Annotations\Groups;
use Traversable;
use PhpBench\Benchmark\Metadata\Annotations\ParamProviders;

/**
* @Groups({"ci", "local"})
*/
final class PackIterableAggregateBench
final class PackIterableAggregateBench extends IteratorBenchmark
{
/**
* @ParamProviders("provideGenerators")
*/
public function benchIterator(array $params): void
public function bench(array $params): void
{
$this->test(
new $params['class']($this->getGenerator($params)),
$params['size']
$this->doBench(
$this->getSubject($params),
$params
);
}

Expand All @@ -39,23 +39,4 @@ public function provideGenerators(): Generator
'size' => $items,
];
}

private function getGenerator(array $params): Generator
{
for ($i = 0; $i < $params['size']; ++$i) {
yield [$i, sprintf('*%s*', $i)];
}
}

private function loop(Traversable $input): Generator
{
foreach ($input as $key => $value) {
yield [$key, $value];
}
}

private function test(Traversable $input, int $size): void
{
iterator_to_array($this->loop($input));
}
}
Loading

0 comments on commit d80150d

Please sign in to comment.