Skip to content

Instantly share code, notes, and snippets.

@dmaicher
Created March 15, 2017 10:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dmaicher/5e85c23145e84a4400354224da85bd08 to your computer and use it in GitHub Desktop.
Save dmaicher/5e85c23145e84a4400354224da85bd08 to your computer and use it in GitHub Desktop.
Symfony routing benchmark
<?php
require_once __DIR__.'/app/bootstrap.php.cache';
require_once __DIR__.'/app/AppKernel.php';
$kernel = new AppKernel('prod', false);
$kernel->boot();
/** @var \Symfony\Component\Routing\Router $router */
$router = $kernel->getContainer()->get('router');
$router->setContext(new \Symfony\Component\Routing\RequestContext());
$routeCollection = $router->getRouteCollection();
for ($i = 0; $i < 100000; $i++) {
$matched = 0;
$notFound = 0;
$methodNotAllowed = 0;
foreach ($routeCollection as $route) {
if ($host = $route->getHost()) {
$router->getContext()->setHost($host);
}
if ($route->getMethods()) {
$router->getContext()->setMethod($route->getMethods()[0]);
}
try {
$router->match($route->getPath());
$matched++;
} catch (\Symfony\Component\Routing\Exception\ResourceNotFoundException $e) {
$notFound++;
} catch (\Symfony\Component\Routing\Exception\MethodNotAllowedException $e) {
$methodNotAllowed++;
}
}
}
echo "matched: ".$matched.PHP_EOL;
echo "not found: ".$notFound.PHP_EOL;
echo "method not allowed: ".$methodNotAllowed.PHP_EOL;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment