Last active
August 29, 2015 14:10
-
-
Save harikt/0584058add3fa23cef91 to your computer and use it in GitHub Desktop.
Real life routes and how fast it is
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"require": { | |
"aura/router":"~2.0", | |
"symfony/routing": "~2.5", | |
"nikic/fast-route": "1.*", | |
"phroute/phroute": "1.*" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
error_reporting(E_ALL); | |
require __DIR__ . '/vendor/autoload.php'; | |
use Symfony\Component\Routing\Matcher\UrlMatcher; | |
use Symfony\Component\Routing\RequestContext; | |
use Symfony\Component\Routing\RouteCollection; | |
use Symfony\Component\Routing\Route; | |
use Phroute\Exception\HttpRouteNotFoundException; | |
function fastroutebench($nMatches) | |
{ | |
$options = []; | |
$router = FastRoute\simpleDispatcher(function($router) { | |
$router->addRoute('GET', '/blog/{page:[0-9]+}', 'blog'); | |
$router->addRoute('GET', '/blog/{slug}', 'blog_post'); | |
}, $options); | |
// first route | |
$startTime = microtime(true); | |
for ($i = 0; $i < $nMatches; $i++) { | |
$res = $router->dispatch('GET', '/blog'); | |
} | |
printf("FastRoute first route: %f\n", microtime(true) - $startTime); | |
if ($res[1] != 'blog') { | |
echo "Wrong route matched \n"; | |
} | |
// last route | |
$startTime = microtime(true); | |
for ($i = 0; $i < $nMatches; $i++) { | |
$res = $router->dispatch('GET', '/blog/helloworld'); | |
} | |
printf("FastRoute last route: %f\n", microtime(true) - $startTime); | |
if ($res[1] != 'blog_post') { | |
echo "Wrong route matched \n"; | |
} | |
//var_dump($res); | |
// unknown route | |
$startTime = microtime(true); | |
for ($i = 0; $i < $nMatches; $i++) { | |
$res = $router->dispatch('GET', '/foobar/bar'); | |
} | |
printf("FastRoute unknown route: %f\n", microtime(true) - $startTime); | |
//var_dump($res); | |
echo "\n-----\n\n"; | |
} | |
function phrouteroutebench($nMatches) | |
{ | |
echo "\n-- Phroute ---\n\n"; | |
$router = new Phroute\RouteCollector(); | |
$router->addRoute('GET', '/blog/{page:i}?', function () { | |
// echo " blog "; | |
}); | |
$router->addRoute('GET', '/blog/{slug:a}?', function () { | |
// echo " blog post "; | |
}); | |
$dispatcher = new Phroute\Dispatcher($router); | |
// first route | |
$startTime = microtime(true); | |
for ($i = 0; $i < $nMatches; $i++) { | |
$res = $dispatcher->dispatch('GET', '/blog'); | |
} | |
printf("Phroute first route: %f\n", microtime(true) - $startTime); | |
// last route | |
$startTime = microtime(true); | |
for ($i = 0; $i < $nMatches; $i++) { | |
$res = $dispatcher->dispatch('GET', '/blog/helloworld'); | |
} | |
printf("Phroute last route: %f\n", microtime(true) - $startTime); | |
// unknown route | |
$startTime = microtime(true); | |
for ($i = 0; $i < $nMatches; $i++) { | |
try { | |
$res = $dispatcher->dispatch('GET', '/foobar/bar'); | |
} catch (HttpRouteNotFoundException $e) { | |
} | |
} | |
printf("Phroute unknown route: %f\n", microtime(true) - $startTime); | |
//var_dump($res); | |
echo "\n-----\n\n"; | |
} | |
function symfonybench($nMatches) | |
{ | |
echo "\n-- Symfony ---\n\n"; | |
$routes = new RouteCollection(); | |
$routes->add('blog', new Route('/blog/{page}', array( | |
'_controller' => 'AppBundle:Blog:index', | |
'page' => 1, | |
), array( | |
'page' => '\d+', | |
))); | |
$routes->add('blog_post', new Route('/blog/{slug}', array( | |
'_controller' => 'AppBundle:Blog:show', | |
))); | |
$context = new RequestContext('/blog'); | |
$matcher = new UrlMatcher($routes, $context); | |
// first route | |
$startTime = microtime(true); | |
for ($i = 0; $i < $nMatches; $i++) { | |
$parameters = $matcher->match('/blog'); | |
} | |
printf("Symfony router first route: %f\n", microtime(true) - $startTime); | |
if ($parameters['_route'] != 'blog') { | |
echo "Wrong route matched \n"; | |
} | |
$context = new RequestContext('/blog/helloworld'); | |
$matcher = new UrlMatcher($routes, $context); | |
// last route | |
$startTime = microtime(true); | |
for ($i = 0; $i < $nMatches; $i++) { | |
$parameters = $matcher->match('/blog/helloworld'); | |
} | |
printf("Symfony last route: %f\n", microtime(true) - $startTime); | |
if ($parameters['_route'] != 'blog_post') { | |
echo "Wrong route matched \n"; | |
} | |
//var_dump($res); | |
// unknown route | |
$startTime = microtime(true); | |
for ($i = 0; $i < $nMatches; $i++) { | |
try { | |
$parameters = $matcher->match('/foobar/bar'); | |
} catch (Exception $e) { | |
} | |
} | |
printf("Symfony router unknown route: %f\n", microtime(true) - $startTime); | |
//var_dump($res); | |
echo "\n-----\n\n"; | |
} | |
function aurabench($nMatches) | |
{ | |
echo "\n-- Aura ---\n\n"; | |
$router = new Aura\Router\Router( | |
new Aura\Router\RouteCollection(new \Aura\Router\RouteFactory), | |
new Aura\Router\Generator | |
); | |
$router->add('blog', '/blog{/page}') | |
->addTokens(array( | |
'page' => '\d+', | |
)); | |
$router->add('blog_post', '/blog/{slug}') | |
->addTokens(array( | |
// 'slug' => '\s+', | |
)); | |
// first route | |
$startTime = microtime(true); | |
for ($i = 0; $i < $nMatches; $i++) { | |
$route = $router->match('/blog', $_SERVER); | |
} | |
printf("Aura router first route: %f\n", microtime(true) - $startTime); | |
if ($route->name != 'blog') { | |
echo "Wrong route matched \n"; | |
} | |
// last route | |
$startTime = microtime(true); | |
for ($i = 0; $i < $nMatches; $i++) { | |
$route = $router->match('/blog/helloworld', $_SERVER); | |
} | |
if ($route->name != 'blog_post') { | |
echo "Wrong route matched \n"; | |
} | |
printf("Aura router last route: %f\n", microtime(true) - $startTime); | |
// unknown route | |
$startTime = microtime(true); | |
for ($i = 0; $i < $nMatches; $i++) { | |
$res = $router->match('/foobar/bar', $_SERVER); | |
} | |
printf("Aura router unknown route: %f\n", microtime(true) - $startTime); | |
//var_dump($res); | |
echo "\n-----\n\n"; | |
} | |
$nMatches = 1; | |
echo "No of matches {$nMatches} \n\n"; | |
aurabench($nMatches); | |
symfonybench($nMatches); | |
fastroutebench($nMatches); | |
phrouteroutebench($nMatches); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@nikic