Skip to content

Commit

Permalink
tests: Add more benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed Jan 5, 2022
1 parent c96fb0c commit f7c8446
Show file tree
Hide file tree
Showing 5 changed files with 353 additions and 0 deletions.
70 changes: 70 additions & 0 deletions tests/benchmarks/ClosureIteratorAggregateBench.php
@@ -0,0 +1,70 @@
<?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 Closure;
use Generator;
use loophp\iterators\ClosureIteratorAggregate;
use PhpBench\Benchmark\Metadata\Annotations\Groups;
use PhpBench\Benchmark\Metadata\Annotations\Iterations;
use PhpBench\Benchmark\Metadata\Annotations\Revs;
use PhpBench\Benchmark\Metadata\Annotations\Warmup;
use Traversable;

/**
* @Groups({"internal"})
* @Iterations(10)
* @Warmup(5)
* @Revs(100)
*/
final class ClosureIteratorAggregateBench
{
/**
* @ParamProviders("provideGenerators")
*/
public function benchIterator(array $params): void
{
$callable = Closure::fromCallable([ClosureIteratorAggregateBench::class, 'loop']);

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

public function provideGenerators(): Generator
{
$items = 5000;

yield ClosureIteratorAggregate::class => [
'class' => ClosureIteratorAggregate::class,
'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(ClosureIteratorAggregate $input): void
{
iterator_to_array($this->loop($input));
}
}
70 changes: 70 additions & 0 deletions tests/benchmarks/ClosureIteratorBench.php
@@ -0,0 +1,70 @@
<?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 Closure;
use Generator;
use loophp\iterators\ClosureIterator;
use PhpBench\Benchmark\Metadata\Annotations\Groups;
use PhpBench\Benchmark\Metadata\Annotations\Iterations;
use PhpBench\Benchmark\Metadata\Annotations\Revs;
use PhpBench\Benchmark\Metadata\Annotations\Warmup;
use Traversable;

/**
* @Groups({"internal"})
* @Iterations(10)
* @Warmup(5)
* @Revs(100)
*/
final class ClosureIteratorBench
{
/**
* @ParamProviders("provideGenerators")
*/
public function benchIterator(array $params): void
{
$callable = Closure::fromCallable([ClosureIteratorBench::class, 'loop']);

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

public function provideGenerators(): Generator
{
$items = 5000;

yield ClosureIterator::class => [
'class' => ClosureIterator::class,
'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(ClosureIterator $input): void
{
iterator_to_array($this->loop($input));
}
}
67 changes: 67 additions & 0 deletions tests/benchmarks/IterableIteratorAggregateBench.php
@@ -0,0 +1,67 @@
<?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 loophp\iterators\IterableIteratorAggregate;
use PhpBench\Benchmark\Metadata\Annotations\Groups;
use PhpBench\Benchmark\Metadata\Annotations\Iterations;
use PhpBench\Benchmark\Metadata\Annotations\Revs;
use PhpBench\Benchmark\Metadata\Annotations\Warmup;
use Traversable;

/**
* @Groups({"internal"})
* @Iterations(10)
* @Warmup(5)
* @Revs(100)
*/
final class IterableIteratorAggregateBench
{
/**
* @ParamProviders("provideGenerators")
*/
public function benchIterator(array $params): void
{
$this->test(
new $params['class']($this->getGenerator($params)),
$params['size']
);
}

public function provideGenerators(): Generator
{
$items = 5000;

yield IterableIteratorAggregate::class => [
'class' => IterableIteratorAggregate::class,
'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));
}
}
67 changes: 67 additions & 0 deletions tests/benchmarks/IterableIteratorBench.php
@@ -0,0 +1,67 @@
<?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 loophp\iterators\IterableIterator;
use PhpBench\Benchmark\Metadata\Annotations\Groups;
use PhpBench\Benchmark\Metadata\Annotations\Iterations;
use PhpBench\Benchmark\Metadata\Annotations\Revs;
use PhpBench\Benchmark\Metadata\Annotations\Warmup;
use Traversable;

/**
* @Groups({"internal"})
* @Iterations(10)
* @Warmup(5)
* @Revs(100)
*/
final class IterableIteratorBench
{
/**
* @ParamProviders("provideGenerators")
*/
public function benchIterator(array $params): void
{
$this->test(
new $params['class']($this->getGenerator($params)),
$params['size']
);
}

public function provideGenerators(): Generator
{
$items = 5000;

yield IterableIterator::class => [
'class' => IterableIterator::class,
'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));
}
}
79 changes: 79 additions & 0 deletions tests/benchmarks/PausableIteratorAggregateBench.php
@@ -0,0 +1,79 @@
<?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 loophp\iterators\PausableIteratorAggregate;
use PhpBench\Benchmark\Metadata\Annotations\Groups;
use PhpBench\Benchmark\Metadata\Annotations\Iterations;
use PhpBench\Benchmark\Metadata\Annotations\Revs;
use PhpBench\Benchmark\Metadata\Annotations\Warmup;
use Traversable;

/**
* @Groups({"internal"})
* @Iterations(10)
* @Warmup(5)
* @Revs(100)
*/
final class PausableIteratorAggregateBench
{
/**
* @ParamProviders("provideGenerators")
*/
public function benchIterator(array $params): void
{
$this->test(
new $params['class']($this->getGenerator($params)),
$params['size']
);
}

public function provideGenerators(): Generator
{
$items = 5000;

yield PausableIteratorAggregate::class => [
'class' => PausableIteratorAggregate::class,
'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 loopUntil(Traversable $input, int $size): Generator
{
foreach ($input as $key => $value) {
yield [$key, $value];

if (0 === $size--) {
break;
}
}
}

private function test(PausableIteratorAggregate $input, int $size): void
{
iterator_to_array($this->loopUntil($input, $size / 2));
iterator_to_array($this->loop($input->rest()));
}
}

0 comments on commit f7c8446

Please sign in to comment.