Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Filter reports in addition to the run #940

Merged
merged 5 commits into from Nov 6, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 1 addition & 14 deletions lib/Benchmark/Metadata/BenchmarkMetadata.php
Expand Up @@ -98,20 +98,7 @@ public function getSubjects(): array
public function filterSubjectNames(array $filters): void
{
foreach (array_keys($this->subjects) as $subjectName) {
$unset = true;

foreach ($filters as $filter) {
if (preg_match(
sprintf('{^.*?%s.*?$}', $filter),
sprintf('%s::%s', $this->getClass(), $subjectName)
)) {
$unset = false;

break;
}
}

if (true === $unset) {
if (false === Benchmark::matchesPatterns($this->class, $subjectName, $filters)) {
unset($this->subjects[$subjectName]);
}
}
Expand Down
5 changes: 4 additions & 1 deletion lib/Console/Command/Handler/SuiteCollectionHandler.php
Expand Up @@ -60,6 +60,9 @@ public function suiteCollectionFromInput(InputInterface $input): SuiteCollection
assert(is_array($files));
assert(is_array($refs));

$subjectPatterns = $input->hasOption('filter') ? $input->getOption('filter') : [];
$variantPatterns = $input->hasOption('variant') ? $input->getOption('variant') : [];
dantleech marked this conversation as resolved.
Show resolved Hide resolved

if (!$files && !$refs) {
throw new \InvalidArgumentException(
'You must specify at least one of `--file` and/or `--ref`'
Expand All @@ -78,7 +81,7 @@ public function suiteCollectionFromInput(InputInterface $input): SuiteCollection
foreach ($refs as $ref) {
$collection->mergeCollection($this->storage->getService()->fetch(
$this->refResolver->resolve($ref)
));
)->filter($subjectPatterns, $variantPatterns));
}
}

Expand Down
52 changes: 52 additions & 0 deletions lib/Model/Benchmark.php
Expand Up @@ -14,6 +14,7 @@

use ArrayIterator;
use PhpBench\Benchmark\Metadata\SubjectMetadata;
use RuntimeException;

/**
* Benchmark metadata class.
Expand Down Expand Up @@ -45,6 +46,27 @@ public function __construct(Suite $suite, string $class)
$this->class = $class;
}

/**
* @param string[] $patterns
*/
public static function matchesPatterns(string $benchmark, string $subject, array $patterns): bool
dantleech marked this conversation as resolved.
Show resolved Hide resolved
{
if (empty($patterns)) {
return true;
}

foreach ($patterns as $pattern) {
if (preg_match(
sprintf('{^.*?%s.*?$}', $pattern),
sprintf('%s::%s', $benchmark, $subject)
)) {
return true;
}
}

return false;
}

public function createSubjectFromMetadataAndExecutor(SubjectMetadata $metadata, ResolvedExecutor $executor): Subject
{
$subject = new Subject($this, $metadata->getName());
Expand Down Expand Up @@ -74,6 +96,17 @@ public function createSubject(string $name): Subject
return $subject;
}

public function addSubject(Subject $subject): void
{
if ($subject->getBenchmark() !== $this) {
throw new RuntimeException(
'Adding subject to benchmark to which it does not belong'
);
}

$this->subjects[$subject->getName()] = $subject;
}

/**
* Get the subject metadata instances for this benchmark metadata.
*
Expand Down Expand Up @@ -120,4 +153,23 @@ public function getSubject(string $subjectName): ?Subject
{
return $this->subjects[$subjectName] ?? null;
}

/**
* @param string[] $subjectPatterns
* @param string[] $variantPatterns
*/
public function filter(array $subjectPatterns, array $variantPatterns): self
{
$subjects = array_filter($this->subjects, function (Subject $subject) use ($subjectPatterns) {
return Benchmark::matchesPatterns($this->class, $subject->getName(), $subjectPatterns);
});
$subjects = array_map(function (Subject $subject) use ($variantPatterns) {
return $subject->filterVariants($variantPatterns);
}, $subjects);

$new = clone $this;
$new->subjects = $subjects;

return $new;
}
}
24 changes: 24 additions & 0 deletions lib/Model/Subject.php
Expand Up @@ -13,6 +13,7 @@
namespace PhpBench\Model;

use PhpBench\Util\TimeUnit;
use RuntimeException;

/**
* Subject representation.
Expand Down Expand Up @@ -118,6 +119,16 @@ public function createVariant(ParameterSet $parameterSet, int $revolutions, int
return $variant;
}

public function setVariant(Variant $variant): void
{
if ($variant->getSubject() !== $this) {
throw new RuntimeException(
'Adding variant to subject to which it does not belong'
);
}
$this->variants[$variant->getParameterSet()->getName()] = $variant;
}

/**
* @return Variant[]
*/
Expand Down Expand Up @@ -235,4 +246,17 @@ public function getFormat(): ?string
{
return $this->format;
}

/**
* @param string[] $variantPatterns
*/
public function filterVariants(array $variantPatterns): self
{
$new = clone $this;
$new->variants = array_filter($this->variants, function (Variant $variant) use ($variantPatterns) {
return $variant->getParameterSet()->nameMatches($variantPatterns);
});

return $new;
}
}
31 changes: 31 additions & 0 deletions lib/Model/Suite.php
Expand Up @@ -17,6 +17,7 @@
use IteratorAggregate;
use PhpBench\Assertion\VariantAssertionResults;
use PhpBench\Environment\Information;
use RuntimeException;

/**
* Represents a Suite.
Expand All @@ -31,6 +32,10 @@ class Suite implements IteratorAggregate
private $date;
private $configPath;
private $envInformations = [];

/**
* @var Benchmark[]
*/
private $benchmarks = [];
private $uuid;

Expand Down Expand Up @@ -70,6 +75,21 @@ public function getBenchmark(string $class): ?Benchmark
return $this->benchmarks[$class] ?? null;
}

/**
* @param string[] $subjectPatterns
* @param string[] $variantPatterns
*/
public function filter(array $subjectPatterns, array $variantPatterns): self
{
$new = clone $this;
$benchmarks = array_map(function (Benchmark $benchmark) use ($subjectPatterns, $variantPatterns) {
return $benchmark->filter($subjectPatterns, $variantPatterns);
}, $this->benchmarks);
$new->benchmarks = $benchmarks;

return $new;
}

/**
* Create and add a benchmark.
*
Expand All @@ -82,6 +102,17 @@ public function createBenchmark(string $class): Benchmark
return $benchmark;
}

public function addBenchmark(Benchmark $benchmark): void
{
if ($benchmark->getSuite() !== $this) {
throw new RuntimeException(
'Adding benchmark to suite to which it does not belong'
);
}

$this->benchmarks[$benchmark->getClass()] = $benchmark;
}

/**
* @return ArrayIterator<Benchmark>
*/
Expand Down
14 changes: 14 additions & 0 deletions lib/Model/SuiteCollection.php
Expand Up @@ -99,4 +99,18 @@ public function firstOnly(): self
$this->suites[0]
]);
}

/**
* @param string[] $subjectPatterns
* @param string[] $variantPatterns
*/
public function filter(array $subjectPatterns, array $variantPatterns): self
{
$new = clone $this;
$new->suites = array_map(function (Suite $suite) use ($subjectPatterns, $variantPatterns) {
return $suite->filter($subjectPatterns, $variantPatterns);
}, $this->suites);

return $new;
}
}
63 changes: 63 additions & 0 deletions tests/Unit/Model/SuiteTest.php
Expand Up @@ -22,6 +22,7 @@
use PhpBench\Model\Suite;
use PhpBench\Model\Variant;
use PhpBench\Tests\TestCase;
use PhpBench\Tests\Util\SuiteBuilder;

class SuiteTest extends TestCase
{
Expand Down Expand Up @@ -138,6 +139,68 @@ public function testFindVariant(): void
));
}

public function testFilterBySubjectNames(): void
{
$suite = SuiteBuilder::create('test')
->benchmark('Foobar')
->subject('subject_one')->end()
->subject('subject_two')->end()
->end()
->build();

self::assertCount(2, $suite->getSubjects());
$suite = $suite->filter(['subject_one'], []);
self::assertCount(1, $suite->getSubjects());
}

public function testFilterByBenchmarkNames(): void
{
$suite = SuiteBuilder::create('test')
->benchmark('Foobar')
->subject('subject_one')->end()
->subject('subject_two')->end()
->end()
->benchmark('Bazboo')
->subject('subject_one')->end()
->subject('subject_two')->end()
->end()
->build();

self::assertCount(4, $suite->getSubjects(), 'Pre filter');
$suite = $suite->filter(['Foobar'], []);
self::assertCount(2, $suite->getSubjects(), 'Post filter');
}

public function testFilterByVariants(): void
{
$suite = SuiteBuilder::create('test')
->benchmark('Foobar')
->subject('subject_one')
->variant('variant one')->end()
->variant('variant two')->end()
->end()
->subject('subject_two')
->variant('variant one')->end()
->variant('variant two')->end()
->end()
->end()
->benchmark('Bazboo')
->subject('subject_one')
->variant('variant one')->end()
->variant('variant two')->end()
->end()
->subject('subject_two')
->variant('variant one')->end()
->variant('variant two')->end()
->end()
->end()
->build();

self::assertCount(8, $suite->getVariants(), 'Pre filter');
$suite = $suite->filter(['Bazboo'], ['variant one']);
self::assertCount(2, $suite->getVariants(), 'Post filter');
}

private function createSuite(array $benchmarks = [], array $informations = []): Suite
{
return new Suite(
Expand Down
78 changes: 78 additions & 0 deletions tests/Util/BenchmarkBuilder.php
@@ -0,0 +1,78 @@
<?php

namespace PhpBench\Tests\Util;

use DateTime;
use PhpBench\Model\Benchmark;
use PhpBench\Model\Suite;
use RuntimeException;

final class BenchmarkBuilder
{
/**
* @var string
*/
private $name;

/**
* @var SubjectBuilder[]
*/
private $subjectBuilders = [];

/**
* @var SuiteBuilder|null
*/
private $suiteBuilder = null;

public function __construct(?SuiteBuilder $suiteBuilder, string $name)
{
$this->name = $name;
$this->suiteBuilder = $suiteBuilder;
}

public static function create(string $name): self
{
return new self(null, $name);
}

public function subject(string $name): SubjectBuilder
{
$builder = SubjectBuilder::forBenchmarkBuilder($this, $name);
$this->subjectBuilders[] = $builder;

return $builder;
}

public function build(?Suite $suite = null): Benchmark
{
if (null === $suite) {
$suite = new Suite(
'testSuite',
new DateTime()
);
}
$benchmark = new Benchmark($suite, $this->name);

foreach ($this->subjectBuilders as $builder) {
$benchmark->addSubject($builder->build($benchmark));
}

return $benchmark;
}

public function end(): SuiteBuilder
{
if (null === $this->suiteBuilder) {
throw new RuntimeException(
'This benchmark builder was not created by a suite builder, end() cannot return anything'
);
}

return $this->suiteBuilder;
}

public static function forSuiteBuilder(SuiteBuilder $builder, string $name): self
{
return new self($builder, $name);
}
}