Skip to content

Instantly share code, notes, and snippets.

@harikt
Last active August 29, 2015 14:10
Show Gist options
  • Save harikt/0584058add3fa23cef91 to your computer and use it in GitHub Desktop.
Save harikt/0584058add3fa23cef91 to your computer and use it in GitHub Desktop.
Real life routes and how fast it is
{
"require": {
"aura/router":"~2.0",
"symfony/routing": "~2.5",
"nikic/fast-route": "1.*",
"phroute/phroute": "1.*"
}
}
<?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);
@harikt
Copy link
Author

harikt commented Nov 21, 2014

@nikic

FastRoute first route: 0.000021
PHP Notice:  Undefined offset: 1 in /var/www/github.com/harikt/aurav2composer/router/routing_real_bench.php on line 26
PHP Stack trace:
PHP   1. {main}() /var/www/github.com/harikt/aurav2composer/router/routing_real_bench.php:0
PHP   2. fastroutebench() /var/www/github.com/harikt/aurav2composer/router/routing_real_bench.php:200
Wrong route matched 
FastRoute last route: 0.000017
FastRoute unknown route: 0.000008

@harikt
Copy link
Author

harikt commented Nov 21, 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment