DEV Community

Cover image for Real life comparison of Symfony router and Mezon router. Part 2
alexdodonov
alexdodonov

Posted on • Edited on

Real life comparison of Symfony router and Mezon router. Part 2

Intro

Hi all! Since the last article I have got some feedback. And the time has come to improve benchmark according to this feedback.

Improvements

The author of the CompiledUrlMatcher Nikolas Grekas pointed on mistake in this case:

for ($i = 0; $i < \Mezon\Benchmark\Base::$iterationsAmount; $i ++) {
    $staticRoutes = \Mezon\Benchmark\RouteGenerator::generateSymfonyStaticRoutes(1000);

    $dumper = new CompiledUrlMatcherDumper($staticRoutes);

    $requestContext = new RequestContext();
    $requestContext->fromRequest(Request::createFromGlobals());

    $staticMatcher = new CompiledUrlMatcher($dumper->getCompiledRoutes(), $requestContext);
    $staticMatcher->match('/static/' . rand(0, ROUTES_AMOUNT - 1))['controller']();
}
Enter fullscreen mode Exit fullscreen mode

He have told that $dumper->getCompiledRoutes() should not be called in array. Very reasonable. So I have changed the benchmark and now it looks like this:

// before the benchmark we shall compile routes and store them into cache
$staticRoutes = \Mezon\Benchmark\RouteGenerator::generateSymfonyStaticRoutes(1000);
$dumperStatic = new CompiledUrlMatcherDumper($staticRoutes);
file_put_contents(
    __DIR__ . '/../../../cache/static-cache.php',
    '<?php return ' . var_export($dumperStatic->getCompiledRoutes(), true) . ';');
Enter fullscreen mode Exit fullscreen mode

And then we shall use this cache in the case:

for ($i = 0; $i < \Mezon\Benchmark\Base::$iterationsAmount; $i ++) {
    $requestContext = new RequestContext();
    $requestContext->fromRequest(Request::createFromGlobals());

    $compiledRoutes = require __DIR__ . '/../../../cache/static-cache.php';

    $staticMatcher = new CompiledUrlMatcher($compiledRoutes, $requestContext);
    $staticMatcher->match('/static/' . rand(0, \Mezon\Benchmark\Base::$iterationsAmount - 1))['controller']();
}
Enter fullscreen mode Exit fullscreen mode

And what are the results? )

Alt Text

  1. Mezon router is almost in all cases faster then Symfony's default UrlMatcher;
  2. But if you agree to dig into Symfony's CompiledUrlMatcher - it will be the best choice.

Learn more

More information can be found here:

Twitter
Mezon Framework

What is mezon/router?

mezon/router now is:

  • php framework for routing with 100% code coverage
  • 10.0 points on scrutinizer-ci.com
  • router is a part of the Mezon Project

Repo on github.com: https://github.com/alexdodonov/mezon-router

It will be great if you will contribute something to this project. Documentation, sharing the project in your social media, bug fixing, refactoring, or even submitting issue with question or feature request. Thanks anyway )

Top comments (2)

Collapse
 
zubairmohsin33 profile image
Zubair Mohsin

Typo in Mezon router is almost in all cases faster then Symfony's default UtlMatcher; -> UrlMatcher.

Collapse
 
alexdodonov profile image
alexdodonov

Aha, thanks )