Skip to content

Commit

Permalink
Improve visibility of benchmarks
Browse files Browse the repository at this point in the history
This reorganises the benchmark structure, allowing us to have better
visibility and more easily filter on groups/variants.

Signed-off-by: Luís Cobucci <[email protected]>
  • Loading branch information
lcobucci committed Jul 27, 2023
1 parent 8680dc7 commit d7f1a0d
Show file tree
Hide file tree
Showing 7 changed files with 279 additions and 277 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ phpstan:

.PHONY: phpbench
phpbench:
@vendor/bin/phpbench run -l dots --report aggregate
@vendor/bin/phpbench run -l dots --report aggregate $(PHPBENCH_OPTIONS)
133 changes: 0 additions & 133 deletions benchmark/Benchmark.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,21 @@

namespace FastRoute\Benchmark;

use FastRoute\DataGenerator;
use FastRoute\Dispatcher;
use FastRoute\RouteCollector;

use RuntimeException;

Check failure on line 10 in benchmark/DispatcherForBenchmark.php

View workflow job for this annotation

GitHub Actions / Check Coding Standards (locked, 8.1, ubuntu-latest)

Expected 0 lines between same types of use statement, found 1.

use function FastRoute\simpleDispatcher;

final class RealLifeExampleBench extends Benchmark
/**

Check failure on line 14 in benchmark/DispatcherForBenchmark.php

View workflow job for this annotation

GitHub Actions / Check Coding Standards (locked, 8.1, ubuntu-latest)

Found multi-line doc comment with single line content, use one-line doc comment instead.
* @phpstan-type DispatcherOptions array{dataGenerator: class-string<DataGenerator>, dispatcher: class-string<Dispatcher>}
*/
final class DispatcherForBenchmark
{
/** @inheritdoc */
protected function createDispatcher(array $options): Dispatcher
/** @param class-string<Dispatcher> $dispatcher */
public static function realLifeExample(string $dispatcher): Dispatcher
{
return simpleDispatcher(
static function (RouteCollector $routes): void {
Expand Down Expand Up @@ -57,71 +63,39 @@ static function (RouteCollector $routes): void {
$routes->addRoute(['PUT', 'PATCH'], '/admin/category/{category_id:\d+}', ['name' => 'admin.category.update']);
$routes->addRoute('DELETE', '/admin/category/{category_id:\d+}', ['name' => 'admin.category.destroy']);
},
$options,
self::resolveOptions($dispatcher),
);
}

/** @inheritdoc */
public function provideStaticRoutes(): iterable
{
yield 'first' => [
'method' => 'GET',
'route' => '/',
'result' => [Dispatcher::FOUND, ['name' => 'home'], []],
];

yield 'last' => [
'method' => 'GET',
'route' => '/admin/category',
'result' => [Dispatcher::FOUND, ['name' => 'admin.category.index'], []],
];

yield 'invalid-method' => [
'method' => 'PUT',
'route' => '/about-us',
'result' => [Dispatcher::METHOD_NOT_ALLOWED, ['GET']],
];
}

/** @inheritdoc */
public function provideDynamicRoutes(): iterable
/** @param class-string<Dispatcher> $dispatcher */
public static function manyRoutes(string $dispatcher, int $routeCount = 400): Dispatcher
{
yield 'first' => [
'method' => 'GET',
'route' => '/page/hello-word',
'result' => [Dispatcher::FOUND, ['name' => 'page.show'], ['page_slug' => 'hello-word']],
];

yield 'last' => [
'method' => 'GET',
'route' => '/admin/category/123',
'result' => [Dispatcher::FOUND, ['name' => 'admin.category.show'], ['category_id' => '123']],
];

yield 'invalid-method' => [
'method' => 'PATCH',
'route' => '/shop/category/123',
'result' => [Dispatcher::METHOD_NOT_ALLOWED, ['GET']],
];
return simpleDispatcher(
static function (RouteCollector $routes) use ($routeCount): void {
for ($i = 0; $i < $routeCount; ++$i) {
$routes->addRoute('GET', '/abc' . $i, ['name' => 'static-' . $i]);
$routes->addRoute('GET', '/abc{foo}/' . $i, ['name' => 'not-static-' . $i]);
}
},
self::resolveOptions($dispatcher),
);
}

/** @inheritdoc */
public function provideOtherScenarios(): iterable
/**
* @param class-string<Dispatcher> $dispatcher
*
* @return DispatcherOptions
*/
private static function resolveOptions(string $dispatcher): array
{
yield 'non-existent' => [
'method' => 'GET',
'route' => '/shop/product/awesome',
'result' => [Dispatcher::NOT_FOUND],
];
$generator = match ($dispatcher) {
Dispatcher\GroupCountBased::class => DataGenerator\GroupCountBased::class,
Dispatcher\CharCountBased::class => DataGenerator\CharCountBased::class,
Dispatcher\GroupPosBased::class => DataGenerator\GroupPosBased::class,
Dispatcher\MarkBased::class => DataGenerator\MarkBased::class,
default => throw new RuntimeException('Unsupported dispatcher'),
};

yield 'longest-route' => [
'method' => 'GET',
'route' => '/shop/category/123/product/search/status:sale',
'result' => [
Dispatcher::FOUND,
['name' => 'shop.category.product.search'],
['category_id' => '123', 'filter_by' => 'status', 'filter_value' => 'sale'],
],
];
return ['dataGenerator' => $generator, 'dispatcher' => $dispatcher];
}
}
82 changes: 0 additions & 82 deletions benchmark/ManyRoutesBench.php

This file was deleted.

41 changes: 41 additions & 0 deletions benchmark/RouteRegistrationBench.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php
declare(strict_types=1);

namespace FastRoute\Benchmark;

use FastRoute\Dispatcher;
use PhpBench\Attributes as Bench;

#[Bench\Iterations(5)]
#[Bench\Revs(100)]
#[Bench\Warmup(3)]
final class RouteRegistrationBench
{
#[Bench\Subject]
#[Bench\Groups(['group_count'])]
public function groupCount(): void
{
DispatcherForBenchmark::realLifeExample(Dispatcher\GroupCountBased::class);
}

#[Bench\Subject]
#[Bench\Groups(['char_count'])]
public function charCount(): void
{
DispatcherForBenchmark::realLifeExample(Dispatcher\CharCountBased::class);
}

#[Bench\Subject]
#[Bench\Groups(['group_pos'])]
public function groupPos(): void
{
DispatcherForBenchmark::realLifeExample(Dispatcher\GroupPosBased::class);
}

#[Bench\Subject]
#[Bench\Groups(['mark'])]
public function mark(): void
{
DispatcherForBenchmark::realLifeExample(Dispatcher\MarkBased::class);
}
}
Loading

0 comments on commit d7f1a0d

Please sign in to comment.