Skip to content

Commit

Permalink
tests: Update benchmarks.
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed Dec 21, 2021
1 parent dd965ff commit 61ebd70
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 33 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ help:
@awk 'BEGIN {FS = ":.*?## "} /^[a-zA-Z_\-\.]+:.*?## / {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}' $(MAKEFILE_LIST)

install-benchmarks: ## install all required dependencies to run benchmarks
composer require phpbench/phpbench azjezz/psl:2.0.x-dev
composer require phpbench/phpbench azjezz/psl:2.0.x-dev --ignore-platform-reqs

benchmarks: install-benchmarks ## run benchmarks
./vendor/bin/phpbench run
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Exception;
use Generator;
use loophp\iterators\CachingIteratorAggregate;
use loophp\iterators\SimpleCachingIteratorAggregate;
use PhpBench\Benchmark\Metadata\Annotations\Groups;
use PhpBench\Benchmark\Metadata\Annotations\Iterations;
use PhpBench\Benchmark\Metadata\Annotations\Revs;
Expand All @@ -20,27 +21,19 @@
use Traversable;

/**
* @Groups({"GeneratorCache"})
* @Groups({"CachingIteratorsBench"})
* @Iterations(10)
* @Warmup(1)
* @Revs(100)
*/
class GeneratorCacheBench
class CachingIteratorsBench
{
/**
* @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);
};
$generator = static fn (): Generator => yield from range(0, $params['items']);

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

Expand All @@ -49,39 +42,27 @@ public function benchIterators(array $params): void

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

yield 'loophp\iterators\SimpleCachingIteratorAggregate' => ['class' => SimpleCachingIteratorAggregate::class, 'items' => $items];

yield 'azjezz/psl' => ['class' => IterIterator::class];
yield 'loophp\iterators\CachingIteratorAggregate' => ['class' => CachingIteratorAggregate::class, 'items' => $items];

yield 'Psl\Iter\Iterator' => ['class' => IterIterator::class, 'items' => $items];
}

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

$i = 0;

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

if (2 === $i++) {
break;
}
$a[] = [$key, $value];
}

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

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

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

if ($a !== $b[2]) {
throw new Exception('$a !== $b[2]');
if ($a !== $b) {
throw new Exception('$a !== $b => Invalid benchmark.');
}
}
}

0 comments on commit 61ebd70

Please sign in to comment.