|
| 1 | +<?php |
| 2 | +declare(strict_types=1); |
| 3 | + |
| 4 | +namespace FastRoute\Benchmark; |
| 5 | + |
| 6 | +use FastRoute\ConfigureRoutes; |
| 7 | +use FastRoute\FastRoute; |
| 8 | +use FastRoute\GenerateUri; |
| 9 | +use PhpBench\Attributes as Bench; |
| 10 | + |
| 11 | +/** @phpstan-import-type UriSubstitutions from GenerateUri */ |
| 12 | +#[Bench\Iterations(5)] |
| 13 | +#[Bench\Revs(250)] |
| 14 | +#[Bench\Warmup(3)] |
| 15 | +#[Bench\BeforeMethods(['registerGenerator'])] |
| 16 | +final class UriGenerationBench |
| 17 | +{ |
| 18 | + private GenerateUri $generator; |
| 19 | + |
| 20 | + public function registerGenerator(): void |
| 21 | + { |
| 22 | + $loader = static function (ConfigureRoutes $routes): void { |
| 23 | + $routes->addRoute('GET', '/', 'do-something', ['_name' => 'home']); |
| 24 | + $routes->addRoute('GET', '/page/{page_slug:[a-zA-Z0-9\-]+}', 'do-something', ['_name' => 'page.show']); |
| 25 | + $routes->addRoute('GET', '/about-us', 'do-something', ['_name' => 'about-us']); |
| 26 | + $routes->addRoute('GET', '/contact-us', 'do-something', ['_name' => 'contact-us']); |
| 27 | + $routes->addRoute('POST', '/contact-us', 'do-something', ['_name' => 'contact-us.submit']); |
| 28 | + $routes->addRoute('GET', '/blog', 'do-something', ['_name' => 'blog.index']); |
| 29 | + $routes->addRoute('GET', '/blog/recent', 'do-something', ['_name' => 'blog.recent']); |
| 30 | + $routes->addRoute('GET', '/blog/{year}[/{month}[/{day}]]', 'do-something', ['_name' => 'blog.archive']); |
| 31 | + $routes->addRoute('GET', '/blog/post/{post_slug:[a-zA-Z0-9\-]+}', 'do-something', ['_name' => 'blog.post.show']); |
| 32 | + $routes->addRoute('POST', '/blog/post/{post_slug:[a-zA-Z0-9\-]+}/comment', 'do-something', ['_name' => 'blog.post.comment']); |
| 33 | + $routes->addRoute('GET', '/shop', 'do-something', ['_name' => 'shop.index']); |
| 34 | + $routes->addRoute('GET', '/shop/category', 'do-something', ['_name' => 'shop.category.index']); |
| 35 | + $routes->addRoute('GET', '/shop/category/{category_id:\d+}/product/search/{filter_by:[a-zA-Z]+}:{filter_value}', 'do-something', ['_name' => 'shop.category.product.search']); |
| 36 | + }; |
| 37 | + |
| 38 | + $this->generator = FastRoute::recommendedSettings($loader, 'cache') |
| 39 | + ->disableCache() |
| 40 | + ->uriGenerator(); |
| 41 | + } |
| 42 | + |
| 43 | + /** @param array{name: non-empty-string} $params */ |
| 44 | + #[Bench\Subject] |
| 45 | + #[Bench\ParamProviders(['allStaticRoutes'])] |
| 46 | + public function staticRoutes(array $params): void |
| 47 | + { |
| 48 | + $this->generator->forRoute($params['name']); |
| 49 | + } |
| 50 | + |
| 51 | + /** @return iterable<non-empty-string, array{name: non-empty-string}> */ |
| 52 | + public static function allStaticRoutes(): iterable |
| 53 | + { |
| 54 | + $staticRoutes = [ |
| 55 | + 'home', |
| 56 | + 'about-us', |
| 57 | + 'contact-us', |
| 58 | + 'contact-us.submit', |
| 59 | + 'blog.index', |
| 60 | + 'blog.recent', |
| 61 | + 'shop.index', |
| 62 | + 'shop.category.index', |
| 63 | + ]; |
| 64 | + |
| 65 | + foreach ($staticRoutes as $route) { |
| 66 | + yield $route => ['name' => $route]; |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + /** @param array{name: non-empty-string, substitutions: UriSubstitutions} $params */ |
| 71 | + #[Bench\Subject] |
| 72 | + #[Bench\ParamProviders(['allDynamicRoutes'])] |
| 73 | + public function dynamicRoutes(array $params): void |
| 74 | + { |
| 75 | + $this->generator->forRoute($params['name'], $params['substitutions']); |
| 76 | + } |
| 77 | + |
| 78 | + /** @return iterable<non-empty-string, array{name: non-empty-string, substitutions: UriSubstitutions}> */ |
| 79 | + public static function allDynamicRoutes(): iterable |
| 80 | + { |
| 81 | + yield 'page.show' => ['name' => 'page.show', 'substitutions' => ['page_slug' => 'testing-one-two-three']]; |
| 82 | + |
| 83 | + yield 'blog.post.show' => ['name' => 'blog.post.show', 'substitutions' => ['post_slug' => 'testing-one-two-three']]; |
| 84 | + yield 'blog.post.comment' => ['name' => 'blog.post.comment', 'substitutions' => ['post_slug' => 'testing-one-two-three']]; |
| 85 | + yield 'blog.archive-year' => ['name' => 'blog.archive', 'substitutions' => ['year' => '2014']]; |
| 86 | + yield 'blog.archive-year-month' => ['name' => 'blog.archive', 'substitutions' => ['year' => '2014', 'month' => '03']]; |
| 87 | + yield 'blog.archive-year-day' => ['name' => 'blog.archive', 'substitutions' => ['year' => '2014', 'month' => '03', 'day' => '15']]; |
| 88 | + |
| 89 | + yield 'shop.category.product.search' => ['name' => 'shop.category.product.search', 'substitutions' => ['category_id' => '1', 'filter_by' => 'name', 'filter_value' => 'testing']]; |
| 90 | + } |
| 91 | +} |
0 commit comments