Skip to content

Instantly share code, notes, and snippets.

@harikt
Forked from nikic/routing_bench.php
Last active April 27, 2016 11:38
Show Gist options
  • Save harikt/7bbcd76105da40431713 to your computer and use it in GitHub Desktop.
Save harikt/7bbcd76105da40431713 to your computer and use it in GitHub Desktop.
Fast router benchmark with aura and symfony
{
"require": {
"aura/router":"2.0.*@dev",
"symfony/routing": "3.0.*@dev",
"nikic/fast-route": "dev-master"
}
}
No of routes is 1000 and matches 1
FastRoute first route: 0.000060
FastRoute last route: 0.001729
FastRoute unknown route: 0.000218
-----
---- aura ----
Aura router first route: 0.000275
Aura router last route: 0.043275
Aura router unknown route: 0.034146
-----
-- Symfony ---
Symfony router first route: 0.000869
Symfony last route: 0.051301
Symfony router unknown route: 0.003998
-----
No of routes is 10000 and matches 1
FastRoute first route: 0.000067
FastRoute last route: 0.017755
FastRoute unknown route: 0.002557
-----
---- aura ----
Aura router first route: 0.014479
Aura router last route: 0.508813
Aura router unknown route: 0.541192
-----
-- Symfony ---
Symfony router first route: 0.020570
Symfony last route: 0.589215
Symfony router unknown route: 0.116708
-----
No of routes is 100000 and matches 1
FastRoute first route: 0.000066
FastRoute last route: 0.257746
FastRoute unknown route: 0.257522
-----
---- aura ----
Aura router first route: 0.178608
Aura router last route: 7.966677
Aura router unknown route: 8.636277
-----
-- Symfony ---
Symfony router first route: 0.215096
Symfony last route: 6.167352
Symfony router unknown route: 1.189065
-----
No of routes is 10 and matches 1
FastRoute first route: 0.000033
FastRoute last route: 0.000014
FastRoute unknown route: 0.000007
-----
---- aura ----
Aura router first route: 0.000128
Aura router last route: 0.000457
Aura router unknown route: 0.000337
-----
-- Symfony ---
Symfony router first route: 0.000645
Symfony last route: 0.000512
Symfony router unknown route: 0.000221
-----
No of routes is 100 and matches 1
FastRoute first route: 0.000041
FastRoute last route: 0.000176
FastRoute unknown route: 0.000025
-----
---- aura ----
Aura router first route: 0.000136
Aura router last route: 0.004347
Aura router unknown route: 0.003289
-----
-- Symfony ---
Symfony router first route: 0.000752
Symfony last route: 0.005072
Symfony router unknown route: 0.000590
-----
No of routes is 10 and matches 10
FastRoute first route: 0.000108
FastRoute last route: 0.000096
FastRoute unknown route: 0.000050
-----
---- aura ----
Aura router first route: 0.000644
Aura router last route: 0.003582
Aura router unknown route: 0.003340
-----
-- Symfony ---
Symfony router first route: 0.000947
Symfony last route: 0.001068
Symfony router unknown route: 0.000960
-----
No of routes is 10 and matches 100
FastRoute first route: 0.000774
FastRoute last route: 0.000939
FastRoute unknown route: 0.000443
-----
---- aura ----
Aura router first route: 0.005397
Aura router last route: 0.034647
Aura router unknown route: 0.032655
-----
-- Symfony ---
Symfony router first route: 0.003468
Symfony last route: 0.006021
Symfony router unknown route: 0.006985
-----
No of routes is 100 and matches 100
FastRoute first route: 0.000764
FastRoute last route: 0.002446
FastRoute unknown route: 0.001894
-----
---- aura ----
Aura router first route: 0.005706
Aura router last route: 0.315672
Aura router unknown route: 0.314364
-----
-- Symfony ---
Symfony router first route: 0.003788
Symfony last route: 0.037519
Symfony router unknown route: 0.034351
-----
<?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 Aura\Router\Map;
use Aura\Router\DefinitionFactory;
use Aura\Router\RouteFactory;
function fastroutebench($nRoutes, $nMatches)
{
$options = [];
$router = FastRoute\simpleDispatcher(function($router) use ($nRoutes, &$lastStr) {
for ($i = 0, $str = 'a'; $i < $nRoutes; $i++, $str++) {
$router->addRoute('GET', '/' . $str . '/{arg}', 'handler' . $i);
$lastStr = $str;
}
}, $options);
// first route
$startTime = microtime(true);
for ($i = 0; $i < $nMatches; $i++) {
$res = $router->dispatch('GET', '/a/foo');
}
printf("FastRoute first route: %f\n", microtime(true) - $startTime);
// last route
$startTime = microtime(true);
for ($i = 0; $i < $nMatches; $i++) {
$res = $router->dispatch('GET', '/' . $lastStr . '/foo');
}
printf("FastRoute last route: %f\n", microtime(true) - $startTime);
//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 symfonybench($nRoutes, $nMatches)
{
echo "\n-- Symfony ---\n\n";
$routes = new RouteCollection();
for ($i = 0, $str = 'a'; $i < $nRoutes; $i++, $str++) {
$route = new Route('/' . $str . '/{arg}', array('controller' => 'MyController'));
$routes->add('handler' . $i, $route);
$lastStr = $str;
}
$context = new RequestContext('/a/foo');
$matcher = new UrlMatcher($routes, $context);
// first route
$startTime = microtime(true);
for ($i = 0; $i < $nMatches; $i++) {
$parameters = $matcher->match('/a/foo');
}
printf("Symfony router first route: %f\n", microtime(true) - $startTime);
$context = new RequestContext('/' . $lastStr . '/foo');
$matcher = new UrlMatcher($routes, $context);
// last route
$startTime = microtime(true);
for ($i = 0; $i < $nMatches; $i++) {
$parameters = $matcher->match('/' . $lastStr . '/foo');
}
printf("Symfony last route: %f\n", microtime(true) - $startTime);
//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($nRoutes, $nMatches)
{
echo "---- aura ---- \n";
// v1
// $router = new Map(new DefinitionFactory, new RouteFactory);
// v2
$router = new Aura\Router\Router(
new Aura\Router\RouteCollection(new \Aura\Router\RouteFactory),
new Aura\Router\Generator
);
for ($i = 0, $str = 'a'; $i < $nRoutes; $i++, $str++) {
// v1
// $router->add('handler' . $i, '/' . $str . '/{:arg}');
// v2
$router->add('handler' . $i, '/' . $str . '/{arg}');
$lastStr = $str;
}
// first route
$startTime = microtime(true);
for ($i = 0; $i < $nMatches; $i++) {
$res = $router->match('/a/foo', $_SERVER);
}
printf("Aura router first route: %f\n", microtime(true) - $startTime);
// last route
$startTime = microtime(true);
for ($i = 0; $i < $nMatches; $i++) {
$res = $router->match('/' . $lastStr . '/foo', $_SERVER);
}
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";
}
$nRoutes = 100;
$nMatches = 100;
echo "No of routes is {$nRoutes} and matches {$nMatches} \n\n";
fastroutebench($nRoutes, $nMatches);
aurabench($nRoutes, $nMatches);
symfonybench($nRoutes, $nMatches);
@weaverryan
Copy link

@harikt I found this because this benchmark was something I have been curious about. But, I don't see any caching, which doesn't make this real-world. Both FastRoute and Symfony (maybe also Aura) cache their routing configuration. Symfony's is particularly optimized for it's cached routing system. I'd be more curious to see the benchmarks when each library is setup for it's real-world configuration. I may fork your benchmark when I have some time to add this :)

@weaverryan
Copy link

Actually, I hadn't seen this yet: veonik/php-router-benchmark#8 - which is more extensive and shows the "dumped" version with Symfony :)

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