Skip to content

Instantly share code, notes, and snippets.

@nikic
Created February 17, 2014 11:46
Show Gist options
  • Save nikic/9049180 to your computer and use it in GitHub Desktop.
Save nikic/9049180 to your computer and use it in GitHub Desktop.
Microbenchmark between Pux and FastRoute
<?php
error_reporting(E_ALL);
require __DIR__ . '/FastRoute/src/bootstrap.php';
spl_autoload_register(function ($class) {
require __DIR__ . '/Pux/src/' . strtr($class, '\\', '/') . '.php';
});
function callback() {}
/*$options = [
'dataGenerator' => 'FastRoute\\OldDataGenerator',
'dispatcher' => 'FastRoute\\OldDispatcher',
];*/
$options = [];
$nRoutes = 100;
$nMatches = 30000;
$mux = new Pux\Mux;
for ($i = 0, $str = 'a'; $i < $nRoutes; $i++, $str++) {
$mux->add('/' . $str . '/:arg', 'callback');
$lastStr = $str;
}
// first route
$startTime = microtime(true);
for ($i = 0; $i < $nMatches; ++$i) {
$route = $mux->dispatch('/a/foo');
}
printf("Pux first route: %f\n", microtime(true) - $startTime);
//var_dump($route);
// last route
$startTime = microtime(true);
for ($i = 0; $i < $nMatches; ++$i) {
$route = $mux->dispatch('/' . $lastStr . '/foo');
}
printf("Pux last route: %f\n", microtime(true) - $startTime);
//var_dump($route);
// unknown route
$startTime = microtime(true);
for ($i = 0; $i < $nMatches; ++$i) {
$route = $mux->dispatch('/foobar/foo');
}
printf("Pux unknown route: %f\n", microtime(true) - $startTime);
//var_dump($route);
$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);
//var_dump($res);
// 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";
$nRoutes = 100;
$nArgs = 9;
$nMatches = 20000;
$args = implode('/', array_map(function($i) { return ':arg' . $i; }, range(1, $nArgs)));
$mux = new Pux\Mux;
for ($i = 0, $str = 'a'; $i < $nRoutes; $i++, $str++) {
$mux->add('/' . $str . '/' . $args, 'callback');
$lastStr = $str;
}
// first route
$startTime = microtime(true);
for ($i = 0; $i < $nMatches; ++$i) {
$route = $mux->dispatch('/a/' . $args);
}
printf("Pux first route: %f\n", microtime(true) - $startTime);
//var_dump($route);
// last route
$startTime = microtime(true);
for ($i = 0; $i < $nMatches; ++$i) {
$route = $mux->dispatch('/' . $lastStr . '/' . $args);
}
printf("Pux last route: %f\n", microtime(true) - $startTime);
//var_dump($route);
// unknown route
$startTime = microtime(true);
for ($i = 0; $i < $nMatches; ++$i) {
$route = $mux->dispatch('/foobar/' . $args);
}
printf("Pux unknown route: %f\n", microtime(true) - $startTime);
//var_dump($route);
$args = implode('/', array_map(function($i) { return "{arg$i}"; }, range(1, $nArgs)));
$router = FastRoute\simpleDispatcher(function($router) use($nRoutes, $args, &$lastStr) {
for ($i = 0, $str = 'a'; $i < $nRoutes; $i++, $str++) {
$router->addRoute('GET', '/' . $str . '/' . $args, 'handler' . $i);
$lastStr = $str;
}
}, $options);
// first route
$startTime = microtime(true);
for ($i = 0; $i < $nMatches; $i++) {
$res = $router->dispatch('GET', '/a/' . $args);
}
printf("FastRoute first route: %f\n", microtime(true) - $startTime);
//var_dump($res);
// last route
$startTime = microtime(true);
for ($i = 0; $i < $nMatches; $i++) {
$res = $router->dispatch('GET', '/' . $lastStr . '/' . $args);
}
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/' . $args);
}
printf("FastRoute unknown route: %f\n", microtime(true) - $startTime);
//var_dump($res);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment