divineniiquaye /
php-routers-benchmark
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | /* |
||
| 6 | * This file is part of DivineNii opensource projects. |
||
| 7 | * |
||
| 8 | * PHP version 7.4 and above required |
||
| 9 | * |
||
| 10 | * @author Divine Niiquaye Ibok <[email protected]> |
||
| 11 | * @copyright 2019 DivineNii (https://divinenii.com/) |
||
| 12 | * @license https://opensource.org/licenses/BSD-3-Clause License |
||
| 13 | * |
||
| 14 | * For the full copyright and license information, please view the LICENSE |
||
| 15 | * file that was distributed with this source code. |
||
| 16 | */ |
||
| 17 | |||
| 18 | namespace App\BenchMark; |
||
| 19 | |||
| 20 | use App\BenchMark\Strategy\CaseInterface; |
||
| 21 | |||
| 22 | require __DIR__ . '/bootstrap.php'; |
||
| 23 | |||
| 24 | // Add Routers For Benchmarking |
||
| 25 | $routers = [ |
||
| 26 | 'Symfony' => Routers\SymfonyRouter::class, |
||
| 27 | 'Flight Routing' => Routers\FlightRouting::class, |
||
| 28 | 'FastRoute' => Routers\FastRoute::class, |
||
| 29 | 'Laravel' => Routers\LaravelRouter::class, |
||
| 30 | 'AltoRouter' => Routers\AltRouter::class, |
||
| 31 | 'AuraRouter' => Routers\AuraRouter::class, |
||
| 32 | 'RareloopRouter' => Routers\RareloopRouter::class, |
||
| 33 | 'NetteRouter' => Routers\NetteRouter::class, |
||
| 34 | 'SunriseRouter' => Routers\SunriseRouter::class, |
||
| 35 | 'SpiralRouter' => Routers\SpiralRouter::class, |
||
| 36 | //'PeceeRouter' => Routers\PeceeRouter::class, |
||
| 37 | //'BramusRouter' => Routers\BramusRouter::class, |
||
| 38 | ]; |
||
| 39 | |||
| 40 | // This generates routes for router and matches them. |
||
| 41 | $routerGenerator = static function (array $config, CaseInterface $strategy, string $router) { |
||
| 42 | [$type, $isolated, $nbRoutes, $nbHosts, $cache] = $config; |
||
| 43 | |||
| 44 | $generator = new RouteGenerator($isolated, $nbRoutes, $nbHosts); |
||
| 45 | |||
| 46 | if ($type === 'SubDomain') { |
||
| 47 | $generator->setHost($router::HOST); |
||
| 48 | $generator->setTemplate($router::PATH, ['world' => '[^/]+']); |
||
| 49 | } elseif ($type === 'Path') { |
||
| 50 | $generator->setTemplate($router::PATH, ['world' => '[^/]+']); |
||
| 51 | } |
||
| 52 | |||
| 53 | [$ids, $routes] = $generator->generate($type === 'Static'); |
||
| 54 | |||
| 55 | /** @var \App\BenchMark\AbstractRouter $router */ |
||
| 56 | $router = new $router($strategy, $generator, $type, $cache); |
||
| 57 | $method = 'test' . $type; |
||
| 58 | |||
| 59 | $router->buildRoutes($routes); |
||
| 60 | $strategy->add($ids); |
||
| 61 | |||
| 62 | return $router->{$method}(); |
||
| 63 | }; |
||
| 64 | |||
| 65 | // Print out system and PHP Info |
||
| 66 | echo Reporter\BenchMark::systemInfo(); |
||
| 67 | |||
| 68 | // Start BenchMarking and produce outcome |
||
| 69 | foreach ($modes as $title => $isolated) { |
||
| 70 | echo Reporter\BenchMark::title($title, '#'); |
||
| 71 | $config = []; |
||
| 72 | |||
| 73 | if ('With Routes Supporting All HTTP Methods And Cache' === $title) { |
||
| 74 | $config['cache'] = true; |
||
| 75 | } |
||
| 76 | |||
| 77 | foreach ($benchmarks as $bench) { |
||
| 78 | static $strategy; |
||
| 79 | |||
| 80 | foreach ($bench['forms'] as $title => $variables) { |
||
|
0 ignored issues
–
show
Comprehensibility
Bug
introduced
by
Loading history...
|
|||
| 81 | foreach ($variables as $key => $value) { |
||
| 82 | if ('strategy' === $key) { |
||
| 83 | $strategy = new $value(); |
||
| 84 | |||
| 85 | continue; |
||
| 86 | } |
||
| 87 | $config[$key] = $value; |
||
| 88 | } |
||
| 89 | |||
| 90 | $benchmark = new Reporter\BenchMark(); |
||
| 91 | echo Reporter\BenchMark::title($title); |
||
| 92 | $benchmark->repeat(100); |
||
| 93 | |||
| 94 | foreach ($routers as $name => $router) { |
||
| 95 | if (isset($config['cache']) && !$router::isCacheable()) { |
||
| 96 | continue; |
||
| 97 | } |
||
| 98 | |||
| 99 | if (!\method_exists($router, 'test' . $bench['type'])) { |
||
| 100 | continue; |
||
| 101 | } |
||
| 102 | |||
| 103 | $data = [$bench['type'], $isolated, $bench['nbRoutes'], $config['nbHosts'] ?? 1, isset($config['cache'])]; |
||
| 104 | |||
| 105 | $benchmark->run($name, $routerGenerator, $data, $strategy, $router); |
||
| 106 | |||
| 107 | \gc_collect_cycles(); |
||
| 108 | } |
||
| 109 | |||
| 110 | echo $benchmark->report(); |
||
| 111 | } |
||
| 112 | } |
||
| 113 | } |
||
| 114 | |||
| 115 | // Remove cache directory after benchmark. |
||
| 116 | if (__DIR__ . '/caches') { |
||
| 117 | \rmdir(__DIR__ . '/caches'); |
||
| 118 | } |
||
| 119 |