Skip to content

Commit

Permalink
feat: Add benchmarks.
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed Dec 19, 2021
1 parent d96dada commit f9b7102
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 1 deletion.
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@
"php": ">= 7.4"
},
"require-dev": {
"azjezz/psl": "^1.9",
"drupol/php-conventions": "^5",
"infection/infection": ">= 0.24.0",
"phpbench/phpbench": "^1.2",
"phpstan/phpstan-strict-rules": "^1.0",
"phpunit/php-code-coverage": "^9.2",
"phpunit/phpunit": "^9.5"
Expand All @@ -43,7 +45,8 @@
},
"autoload-dev": {
"psr-4": {
"tests\\loophp\\iterators\\": "./tests/unit/"
"tests\\loophp\\iterators\\": "./tests/unit/",
"benchmarks\\loophp\\iterators\\": "./tests/benchmarks/"
}
},
"config": {
Expand Down
10 changes: 10 additions & 0 deletions phpbench.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"$schema":"./vendor/phpbench/phpbench/phpbench.schema.json",
"runner.bootstrap": "vendor/autoload.php",
"runner.path": "tests/benchmarks",
"report.generators": {
"test": {
"extends": "aggregate"
}
}
}
73 changes: 73 additions & 0 deletions tests/benchmarks/GeneratorCacheBench.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?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 Exception;
use Generator;
use loophp\iterators\CachingIteratorAggregate;
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 Psl\Iter\Iterator as IterIterator;
use Traversable;

/**
* @Groups({"GeneratorCache"})
* @Iterations(10)
* @Warmup(1)
* @Revs(100)
*/
class GeneratorCacheBench
{
/**
* @ParamProviders("provideGenerators")
*/
public function benchIterators(array $params): void
{
$generator = static function (): Generator {
yield true => true;

yield false => false;

yield ['a'] => ['a'];

yield from range(0, 250);
};

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

$this->test($iterator);
}

public function provideGenerators(): Generator
{
yield 'loophp/iterators' => ['class' => CachingIteratorAggregate::class];

yield 'azjezz/psl' => ['class' => IterIterator::class];
}

private function test(Traversable $input): void
{
$a = $b = [];

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

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

if ($a !== $b) {
throw new Exception('$a !== $b');
}
}
}

0 comments on commit f9b7102

Please sign in to comment.