Skip to content

Commit

Permalink
Add benchmark.
Browse files Browse the repository at this point in the history
  • Loading branch information
serge-kvashnin committed Oct 3, 2023
1 parent 6326c79 commit 0868eb4
Show file tree
Hide file tree
Showing 10 changed files with 465 additions and 13 deletions.
35 changes: 35 additions & 0 deletions .github/workflows/benchmark.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
on: [ pull_request, push ]

name: "Benchmark"

permissions:
contents: read

jobs:
tests:
name: Benchmark

runs-on: ubuntu-latest

strategy:
fail-fast: false
matrix:
php-version:
- 8.1
- 8.2
- 8.3

steps:
- name: Checkout
uses: actions/checkout@v3

- name: Install PHP
uses: shivammathur/setup-php@v2
with:
php-version: "${{ matrix.php-version }}"

- name: Install dependencies
uses: php-actions/composer@v6

- name: Run benchmark
run: vendor/bin/phpbench run tests/bench --report=aggregate
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"php": "^8.1"
},
"require-dev": {
"phpunit/phpunit": "^10.3"
"phpunit/phpunit": "^10.3",
"phpbench/phpbench": "^1.2"
}
}
5 changes: 5 additions & 0 deletions phpbench.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"$schema": "./vendor/phpbench/phpbench/phpbench.schema.json",
"runner.bootstrap": "vendor/autoload.php",
"runner.file_pattern": "*Bench.php"
}
20 changes: 8 additions & 12 deletions src/Pathfinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,13 @@ public function delete(string $pattern, string $handler): void

public function match(string $method, string $path): array
{
$method = strtoupper($method);
if (isset($this->static[$path])) {
if (!isset($this->static[$path][self::HANDLERS][$method])) {
return self::result(self::METHOD_NOT_ALLOWED);
return [self::METHOD_NOT_ALLOWED, []];
}

return self::result($this->static[$path][self::HANDLERS][$method]);
return [$this->static[$path][self::HANDLERS][$method], []];
}

$params = [];
Expand Down Expand Up @@ -176,22 +177,22 @@ public function match(string $method, string $path): array
}

if (!$found) {
return self::result(self::NOT_FOUND);
return [self::NOT_FOUND, []];
}
}

if ($node[self::HANDLERS]) {
if (!isset($node[self::HANDLERS][$method])) {
return self::result(self::METHOD_NOT_ALLOWED);
return [self::METHOD_NOT_ALLOWED, []];
}

return self::result(
return [
$node[self::HANDLERS][$method],
$params,
);
];
}

return self::result(self::NOT_FOUND);
return [self::NOT_FOUND, []];
}

public function tree(): array
Expand All @@ -202,11 +203,6 @@ public function tree(): array
];
}

private static function result(string|int $handler, array $params = []): array
{
return [$handler, $params];
}

private static function static(): array
{
return [
Expand Down
42 changes: 42 additions & 0 deletions tests/Bench/ColdBench.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

declare(strict_types=1);

namespace Tests\Norvica\Pathfinder\Bench;

use Norvica\Pathfinder\Pathfinder;
use PhpBench\Attributes\Iterations;
use PhpBench\Attributes\OutputTimeUnit;
use PhpBench\Attributes\Revs;
use PhpBench\Attributes\Warmup;

#[Revs(100)]
#[Iterations(5)]
#[Warmup(2)]
#[OutputTimeUnit("milliseconds", 3)]
final class ColdBench
{
public function benchFirstRoute(): void
{
$router = new Pathfinder();
$routing = require __DIR__ . '/../routes/routes.php';
$routing($router);
$router->match('GET', '/users');
}

public function benchLastRoute(): void
{
$router = new Pathfinder();
$routing = require __DIR__ . '/../routes/routes.php';
$routing($router);
$router->match('POST', '/api/workflow/9999/stages/6666/tasks/3333/subtasks/1111/complete');
}

public function bench404(): void
{
$router = new Pathfinder();
$routing = require __DIR__ . '/../routes/routes.php';
$routing($router);
$router->match('GET', '/api/workflow/9999/stages/6666/tasks/3333/subtasks/1111/complete!');
}
}
49 changes: 49 additions & 0 deletions tests/Bench/InstantiatedBench.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

namespace Tests\Norvica\Pathfinder\Bench;

use Norvica\Pathfinder\Cache;
use Norvica\Pathfinder\Pathfinder;
use PhpBench\Attributes\Iterations;
use PhpBench\Attributes\OutputTimeUnit;
use PhpBench\Attributes\Revs;
use PhpBench\Attributes\Warmup;

#[Revs(10000)]
#[Iterations(5)]
#[Warmup(2)]
#[OutputTimeUnit("milliseconds", 3)]
final class InstantiatedBench
{
private Pathfinder $pathfinder;

public function __construct()
{
$this->pathfinder = new Pathfinder();
$routing = require __DIR__ . '/../routes/routes.php';
$routing($this->pathfinder);

$tree = $this->pathfinder->tree();
(new Cache(__DIR__ . '/../../var/pathfinder_cache'))->write($tree);

$tree = (new Cache(__DIR__ . '/../../var/pathfinder_cache'))->read();
$this->pathfinder = new Pathfinder($tree);
}

public function benchFirstRoute(): void
{
$this->pathfinder->match('GET', '/users');
}

public function benchLastRoute(): void
{
$this->pathfinder->match('POST', '/api/workflow/9999/stages/6666/tasks/3333/subtasks/1111/complete');
}

public function bench404(): void
{
$this->pathfinder->match('GET', '/api/workflow/9999/stages/6666/tasks/3333/subtasks/1111/complete!');
}
}
53 changes: 53 additions & 0 deletions tests/Bench/WarmBench.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

declare(strict_types=1);

namespace Tests\Norvica\Pathfinder\Bench;

use Norvica\Pathfinder\Cache;
use Norvica\Pathfinder\Pathfinder;
use PhpBench\Attributes\Iterations;
use PhpBench\Attributes\OutputTimeUnit;
use PhpBench\Attributes\Revs;
use PhpBench\Attributes\Warmup;

#[Revs(100)]
#[Iterations(5)]
#[Warmup(2)]
#[OutputTimeUnit("milliseconds", 3)]
final class WarmBench
{
public function __construct()
{
$router = new Pathfinder();
$routing = require __DIR__ . '/../routes/routes.php';
$routing($router);

$tree = $router->tree();
(new Cache(__DIR__ . '/../../var/pathfinder_cache'))->write($tree);
}

public function benchFirstRoute(): void
{
$tree = (new Cache(__DIR__ . '/../../var/pathfinder_cache'))->read();
$router = new Pathfinder($tree);

$router->match('GET', '/users');
}

public function benchLastRoute(): void
{
$tree = (new Cache(__DIR__ . '/../../var/pathfinder_cache'))->read();
$router = new Pathfinder($tree);

$router->match('POST', '/api/workflow/9999/stages/6666/tasks/3333/subtasks/1111/complete');
}

public function bench404(): void
{
$tree = (new Cache(__DIR__ . '/../../var/pathfinder_cache'))->read();
$router = new Pathfinder($tree);

$router->match('GET', '/api/workflow/9999/stages/6666/tasks/3333/subtasks/1111/complete!');
}
}
27 changes: 27 additions & 0 deletions tests/profile.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

use Norvica\Pathfinder\Cache;
use Norvica\Pathfinder\Pathfinder;

require __DIR__ . '/../src/Cache.php';
require __DIR__ . '/../src/Pathfinder.php';

$pathfinder = setup();
for($i = 0; $i < 10000; ++$i) {
$pathfinder->match('POST', '/api/workflow/9999/stages/6666/tasks/3333/subtasks/1111/complete');
}

function setup(): Pathfinder
{
$pathfinder = new Pathfinder();
$routing = require __DIR__ . '/routes/routes.php';
$routing($pathfinder);

$tree = $pathfinder->tree();
(new Cache(__DIR__ . '/../var/pathfinder_cache'))->write($tree);

$tree = (new Cache(__DIR__ . '/../var/pathfinder_cache'))->read();
return new Pathfinder($tree);
}
Loading

0 comments on commit 0868eb4

Please sign in to comment.