|
| 1 | +<?php |
| 2 | + |
| 3 | +require __DIR__ . '/vendor/autoload.php'; |
| 4 | + |
| 5 | +function createParts(&$parts, $j = 0) |
| 6 | +{ |
| 7 | + $j++; |
| 8 | + if ($j < 5) { |
| 9 | + for ($i = 0; $i < 10; $i++) { |
| 10 | + $part = dechex(crc32(mt_rand(1000, 2000))); |
| 11 | + $parts[$part] = []; |
| 12 | + createParts($parts[$part], $j); |
| 13 | + } |
| 14 | + } |
| 15 | +} |
| 16 | + |
| 17 | +function createRoutes(&$routes, $parts, $prefix = '/') |
| 18 | +{ |
| 19 | + foreach ($parts as $part => $subParts) { |
| 20 | + if (!empty($subParts)) { |
| 21 | + createRoutes($routes, $subParts, $prefix . $part . '/'); |
| 22 | + } else { |
| 23 | + $routes[] = $prefix . $part . '/{id:[0-9]+}'; |
| 24 | + } |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +$sections = ['news', 'projects', 'users', 'tasks', 'articles', 'documents', 'photos']; |
| 29 | +$subsections = ['all', 'new', 'popular', 'discussed', 'hot', 'my']; |
| 30 | + |
| 31 | +$routePatterns = [ |
| 32 | +'/%section%/{param1}' => '/%section%/1', |
| 33 | +'/%section%/{param1}/{param2}' => '/%section%/1/2', |
| 34 | +'/%section%/{param1}/{param2}/full' => '/%section%/1/2/full', |
| 35 | +'/%section%/%subsection%/{param1:[0-9]+}' => '/%section%/%subsection%/1', |
| 36 | +'/%section%/%subsection%/{param1:[0-9]+}/{param2:[a-z]+}' => '/%section%/%subsection%/1/hello', |
| 37 | +'/%section%/%subsection%/{param1:[0-9]+}/{param2:[a-z]+}/full' => '/%section%/%subsection%/1/hello/full' |
| 38 | +]; |
| 39 | + |
| 40 | +$routeIndex = []; |
| 41 | +foreach ($sections as $section) { |
| 42 | + foreach ($subsections as $subsection) { |
| 43 | + foreach ($routePatterns as $routePattern => $urlPattern) { |
| 44 | + $route = str_replace(['%section%', '%subsection%'], [$section, $subsection], $routePattern); |
| 45 | + $url = str_replace(['%section%', '%subsection%'], [$section, $subsection], $urlPattern); |
| 46 | + $routeIndex[$route] = $url; |
| 47 | + } |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +var_dump($routeIndex); |
| 52 | + |
| 53 | +$urls = array_values($routeIndex); |
| 54 | + |
| 55 | +function createFastRoute($routeIndex) |
| 56 | +{ |
| 57 | + $dispatcher = FastRoute\simpleDispatcher(function(FastRoute\RouteCollector $r) use ($routeIndex) { |
| 58 | + $i = 0; |
| 59 | + foreach ($routeIndex as $route => $url) { |
| 60 | + $r->addRoute('GET', $route, 'handler' . $i); |
| 61 | + $i++; |
| 62 | + } |
| 63 | + }); |
| 64 | + return $dispatcher; |
| 65 | +} |
| 66 | + |
| 67 | +function createTreeRoute($routeIndex) |
| 68 | +{ |
| 69 | + $router = new \TreeRoute\Router(); |
| 70 | + $i = 0; |
| 71 | + foreach ($routeIndex as $route => $url) { |
| 72 | + $router->addRoute('GET', $route, 'handler' . $i); |
| 73 | + $i++; |
| 74 | + } |
| 75 | + return $router; |
| 76 | +} |
| 77 | + |
| 78 | +$t1 = microtime(true); |
| 79 | +$fastRoute = createFastRoute($routeIndex); |
| 80 | +$t2 = microtime(true); |
| 81 | + |
| 82 | + |
| 83 | +$t3 = microtime(true); |
| 84 | +$treeRoute = createTreeRoute($routeIndex); |
| 85 | +$t4 = microtime(true); |
| 86 | + |
| 87 | +echo 'FastRoute init time: ' . ($t2 - $t1) . PHP_EOL; |
| 88 | +echo 'TreeRoute init time: ' . ($t4 - $t2) . PHP_EOL; |
| 89 | + |
| 90 | +function test($router, $routeIndex, $url) |
| 91 | +{ |
| 92 | + $time = 0; |
| 93 | + for ($i = 0; $i < 10000; $i++) { |
| 94 | + $t1 = microtime(true); |
| 95 | + $router->dispatch('GET', $url); |
| 96 | + $t2 = microtime(true); |
| 97 | + $time += ($t2 - $t1); |
| 98 | + } |
| 99 | + return $time; |
| 100 | +} |
| 101 | + |
| 102 | +$fastRouteResultFirst = test($fastRoute, $routeIndex, $urls[0]); |
| 103 | +$treeRouteResultFirst = test($treeRoute, $routeIndex, $urls[0]); |
| 104 | + |
| 105 | +$fastRouteResultMiddle = test($fastRoute, $routeIndex, $urls[round(sizeof($urls) / 2)]); |
| 106 | +$treeRouteResultMiddle = test($treeRoute, $routeIndex, $urls[round(sizeof($urls) / 2)]); |
| 107 | + |
| 108 | +$fastRouteResultLast = test($fastRoute, $routeIndex, $urls[sizeof($urls) - 1]); |
| 109 | +$treeRouteResultLast = test($treeRoute, $routeIndex, $urls[sizeof($urls) - 1]); |
| 110 | + |
| 111 | +$fastRouteResultNotFound = test($fastRoute, $routeIndex, '/not/found/url'); |
| 112 | +$treeRouteResultNotFound = test($treeRoute, $routeIndex, '/not/found/url'); |
| 113 | + |
| 114 | +echo 'FastRoute first route time: ' . $fastRouteResultFirst . PHP_EOL; |
| 115 | +echo 'TreeRoute first route time: ' . $treeRouteResultFirst . PHP_EOL; |
| 116 | + |
| 117 | +echo 'FastRoute middle route time: ' . $fastRouteResultMiddle . PHP_EOL; |
| 118 | +echo 'TreeRoute middle route time: ' . $treeRouteResultMiddle . PHP_EOL; |
| 119 | + |
| 120 | +echo 'FastRoute last route time: ' . $fastRouteResultLast . PHP_EOL; |
| 121 | +echo 'TreeRoute last route time: ' . $treeRouteResultLast . PHP_EOL; |
| 122 | + |
| 123 | +echo 'FastRoute not found time: ' . $fastRouteResultNotFound . PHP_EOL; |
| 124 | +echo 'TreeRoute not found time: ' . $treeRouteResultNotFound . PHP_EOL; |
0 commit comments