Last active
December 22, 2015 01:39
-
-
Save alexrohleder/c6ba88234e51f301a1ab to your computer and use it in GitHub Desktop.
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 | |
include 'vendor/autoload.php'; | |
$cbmapper = new Codeburner\Router\Mapper; | |
$cbdispatcher = new Codeburner\Router\Dispatcher($cbmapper); | |
$frmapper = new FastRoute\RouteCollector(new FastRoute\RouteParser\Std, new FastRoute\DataGenerator\GroupCountBased); | |
// for triggering the autoload | |
new FastRoute\Dispatcher\GroupCountBased($frmapper->getData()); | |
new Codeburner\Router\Strategies\UriDispatcherStrategy(); | |
function run($name, Closure $do) | |
{ | |
$t1 = microtime(true); | |
$do(); | |
$t2 = microtime(true) - $t1; | |
echo $name, " done in: ", $t2, "<br>"; | |
return $t2; | |
} | |
function routes() | |
{ | |
$routes = []; | |
///** for 100 routes with args variating on 1 to 9 | |
foreach (range(1, 10) as $i) { | |
foreach (range(1, 10) as $j) { | |
$uri = '/' . $i; | |
foreach (range(1, $j) as $k) { | |
$uri .= '/{arg' . $k . '}'; | |
} | |
$routes[] = $uri; | |
} | |
} | |
//**/ | |
/** for 100 routes with 9 args like nikic benchmark | |
$args = implode('/', array_map(function ($i) { | |
return "{arg$i}"; | |
}, range(1, 9))); | |
for ($i = 0, $str = 'a'; $i < 100; $i++, $str++) { | |
$routes[] = '/' . $str . '/' . $args; | |
} | |
**/ | |
return $routes; | |
} | |
// define('ROUTE', array_reverse(routes())[0]); // for last route | |
define('ROUTE', routes()[0]); // for first route | |
$cbmapper_time = run('cbmapper', function () use ($cbmapper) { | |
foreach (routes() as $route) { | |
$cbmapper->get($route, function () { | |
echo '<span style="color:blue">', 'route matched and executed with ', count(func_get_args()), ' arguments!', '</span> <br>'; | |
}); | |
} | |
}); | |
$frmapper_time = run('frmapper', function () use ($frmapper) { | |
foreach (routes() as $route) { | |
$frmapper->addRoute('GET', $route, function () { | |
echo '<span style="color:blue">', 'route matched ad executed with ', count(func_get_args()), ' arguments!', '</span> <br>'; | |
}); | |
} | |
}); | |
echo '<p style="color:red">', 'diference between cbmapper - frmapper: ', $cbmapper_time - $frmapper_time, '</p>'; | |
$cbmatcher_time = run('cbmatcher', function () use ($cbdispatcher) { | |
$cbdispatcher->match('get', ROUTE); | |
}); | |
$frmatcher_time = run('frmatcher', function () use ($frmapper) { | |
$frmatcher = new FastRoute\Dispatcher\GroupCountBased($frmapper->getData()); | |
$frmatcher->dispatch('GET', ROUTE); | |
}); | |
echo '<p style="color:red">', 'diference between cbmatcher - frmatcher: ', $cbmatcher_time - $frmatcher_time, '</p>'; | |
$cbdispatcher_time = run('cbdispatcher', function () use ($cbdispatcher) { | |
$cbdispatcher->dispatch('get', ROUTE); | |
}); | |
$frdispatcher_time = run('frdispatcher', function () use ($frmapper) { | |
$frmatcher = new FastRoute\Dispatcher\GroupCountBased($frmapper->getData()); | |
$route = $frmatcher->dispatch('GET', ROUTE); | |
call_user_func_array($route[1], $route[2]); | |
}); | |
echo '<p style="color:red">', 'diference between cbdispatcher - frdispatcher: ', $cbdispatcher_time - $frdispatcher_time, '</p>'; | |
echo '<p><b>', 'The total time for registering all the routes and execute the matched route callback:', '</b></p>'; | |
echo 'codeburner: ', $codeburner = $cbmapper_time + $cbdispatcher_time, '<br>'; | |
echo 'fastroute: ', $fastroute = $frmapper_time + $frdispatcher_time, '<br>'; | |
echo '<p style="color:red">', 'The diference between codeburner - fastroute: ', $codeburner - $fastroute, '</p>'; | |
if ($codeburner > $fastroute) { | |
echo '<p style="color:green">fastroute gets ', 100 - ($fastroute * 100) / $codeburner,'% faster</p>'; | |
} else { | |
echo '<p style="color:green">codeburner gets ', 100 - ($codeburner * 100) / $fastroute,'% faster</p>'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment