DEV Community

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

Posted on • Updated on

Real life comparison of Symfony router and Mezon router

Hello world! In my previous article Nicolas Grekas noted that CompiledUrlMatcher (part of the Symfony framework) works better.

The time has come to benchmark it too )

To be honest it was quite tricky to do it, because Google don't give any documentation and tutorials about CompiledUrlMatcher. But you are in better conditions - you can use this article as a tutorial if you will use this matcher.

Benchmark concepts

Since now I have decided to change tests because in general we have two big cases:

  1. http server accepts request, launches php script, wich handles this request, and then all script data uploads from memory. All following requests are processed in the same way. In this case very critical to launch script as soon as possible and we do not have time for long pre-compilations and preparations. Because all of it will be lost after the script will finish working.
  2. php script is launching, initiating all internal components (and router is one of them) and then starting processing requests. This case can be organized via for example react-php. It differs from the previous case because we can spend reasonable time to pre-compile routes for faster processing

That's why I shall benchmark both of these cases.

In one group of tests all route objects will be re-created every loop. And in another group of tests time of initialization will outside the benchmarking code.

Let's start!

Here is benchmark for the static routes and as I said we take into consideration the time of creation of all router's structures and objects.

And by the way - we shall use 1000 routes here:

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

The benchmark for non-static routes is almost the same so I shall not put it here.

Let's see at the benchmark for the second case:

// this code is not in the benchmark scope...
$staticRoutes = \Mezon\Benchmark\RouteGenerator::generateSymfonyStaticRoutes(1000);

$dumper = new CompiledUrlMatcherDumper($staticRoutes);

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

$staticMatcher = new CompiledUrlMatcher($dumper->getCompiledRoutes(), $requestContext);

$startTime = microtime(true);
// ... only this one
for ($i = 0; $i < \Mezon\Benchmark\Base::$iterationsAmount; $i ++) {
    $staticMatcher->match('/static/' . rand(0, ROUTES_AMOUNT - 1))['controller']();
}
echo microtime(true) - $startTime;
Enter fullscreen mode Exit fullscreen mode

And the results are (the bigger numbers means better and the numbers are requests per second):

table

Here we can see:

  1. Mezon router is still almost always faster then Symfony+UrlMatcher;
  2. CompiledUrlMatcher is lightning fast (Nicolas Grekas have done great optimizations) for the second case, and terrible results for the first case. The reason of these results is hidden in the url matcher's optimizations - pre-compilation of routers takes very long time.

What is the conclusion? I am still sure that Mezon router is the best common purpose routing framework. But in some specialized cases you should use Symfony with the CompiledUrlMatcher.

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 (3)

Collapse
 
nicolasgrekas profile image
Nicolas Grekas

Thanks for the update. You've got the wiring right, but $dumper->getCompiledRoutes() should never be called in a loop.

The way this is supposed to be used is:

  1. warm the list of routes by dumping the compiled routes once (eg using var_export() to generate a cache.php file)
  2. reuse the already dumped routes in the loop.
Collapse
 
alexdodonov profile image
alexdodonov

Mmmmmmm I have not dig so deep, but I shall change the benchmark.

reuse the already dumped routes in the loop.

Just include cache.php in my php script? or there is a method wich loads cached routes?

Collapse
 
nicolasgrekas profile image
Nicolas Grekas

you can dump cache.php doing eg:
file_put_contents('cache.php', '<?php return '.var_export($dumper->getCompiledRoutes(), true).';');

then to get the compiled routes:
$compiledRoutes = require 'cache.php';