loading...
Cover image for New PHP router wich is 7 to 15 times faster then Symfony router

New PHP router wich is 7 to 15 times faster then Symfony router

alexdodonov profile image alexdodonov Updated on ・2 min read

php routes benchmarks (10 Part Series)

1) New php router is 25 times faster then klein router 2) New PHP router wich is 7 to 15 times faster then Symfony router 3 ... 8 3) New PHP router wich is 30 to 50 times faster then Laravel router 4) New PHP router wich is 24 to 42 times fatser then Yii2 router 5) Real life comparison of Symfony router and Mezon router 6) Real life comparison of Symfony router and Mezon router. Part 2 7) Benchmark of PHP Altorouter 8) New benchmark shows that Mezon PHP Router is up to 60 times faster then Slim one 9) Benchmark of Mezon PHP Router and LeagueRouter 10) Mezon PHP Router is up to 82 times fatser then Bramus Router

Intro

Hi people! I want to continue the previous article in wich I have compared mezon/router with the klein router.

And in this article I shall compare mezon router with the Symfony routing class.

Benchmark for mezon/router

But now I have decided to change benchmark a little bit. Since now we shall take into consideration not only routing itself but also creation of the router object.

For example we shall benchmark not only this code:

for ($i = 0; $i < $iterationCount1; $i ++) {
    $routerTest1->callRoute('/static');
}

But this one:

for ($i = 0; $i < $iterationCount1; $i ++) {
    $routerTest1 = new \Mezon\Router\Router();
    $routerTest1->addRoute('/static', function () {
        return 'static';
    }, 'GET');
    $routerTest1->callRoute('/static');
}

Benchmark for klein/klein

The same changes will be made for klein benchmark:

for ($i = 0; $i < $iterationCount1; $i ++) {
    $routerTest1 = new \Klein\Klein();
    $routerTest1->respond('GET', '/static', function () {
        return 'static';
    });
    $routerTest1->dispatch(null,null,true,\Klein\Klein::DISPATCH_CAPTURE_AND_RETURN);
}

And:

for ($i = 0; $i < $iterationCount2; $i ++) {
    $routerTest2 = new \Klein\Klein();
    $routerTest2->respond('GET', '/[i:id]', function () {
        return 'param';
    });
    $routerTest2->dispatch(null,null,true,\Klein\Klein::DISPATCH_CAPTURE_AND_RETURN);
}

Benchmark for symfony routing

And now it is time to look at the symphony benchmark code:

$iterationCount1 = 10000;
$startTime1 = microtime(true);
for ($i = 0; $i < $iterationCount1; $i ++) {
    $static = new Route('/static', [
        'controller' => function () {
            return 'static';
        }
    ]);
    $staticRoutes = new RouteCollection();
    $staticRoutes->add('static', $static);

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

    $staticMatcher = new UrlMatcher($staticRoutes, $staticContext);
    $parameters = $staticMatcher->match('/static');
    $parameters['controller']();
}
$endTime1 = microtime(true);

And for URLs with parameters:

$iterationCount2 = 10000;
$startTime2 = microtime(true);
for ($i = 0; $i < $iterationCount2; $i ++) {
    $param = new Route('/{id}', [
        'controller' => function () {
            return 'param';
        },
        [
            'id' => '[0-9]+'
        ]
    ]);

    $paramRoutes = new RouteCollection();
    $paramRoutes->add('param', $param);

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

    $paramMatcher = new UrlMatcher($paramRoutes, $paramContext);
    $paramMatcher->match('/1')['controller']();
}
$endTime2 = microtime(true);

And the results are (the bigger numbers means better):

table

graph

As you can see - Mezon router is 7 to 15 times faster than Symfony router.

What is mezon/router?

mezon/router now is:

  • 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

I'll be very glad if you'll press "STAR" button in Github repo )

php routes benchmarks (10 Part Series)

1) New php router is 25 times faster then klein router 2) New PHP router wich is 7 to 15 times faster then Symfony router 3 ... 8 3) New PHP router wich is 30 to 50 times faster then Laravel router 4) New PHP router wich is 24 to 42 times fatser then Yii2 router 5) Real life comparison of Symfony router and Mezon router 6) Real life comparison of Symfony router and Mezon router. Part 2 7) Benchmark of PHP Altorouter 8) New benchmark shows that Mezon PHP Router is up to 60 times faster then Slim one 9) Benchmark of Mezon PHP Router and LeagueRouter 10) Mezon PHP Router is up to 82 times fatser then Bramus Router

Posted on by:

Discussion

markdown guide
 

Hello, thanks for contributing to the space of routing libraries in PHP.
FYI, your benchmark for the Symfony router is using the slowest path supported by the component.
If you really want to compare its performance to your work, you should use the CompiledUrlMatcher.
AFAIK, this is the fastest PHP Router existing to date.
I would be very (and happily) surprised if your router were (significantly) faster. Let me know!
Check medium.com/@nicolas.grekas/making-... for some background on the technology.

 
 

How about memory consumption and latency?

You could compare your benchmark there, assume is around 100th while Symfony is at 145th:
github.com/the-benchmarker/web-fra...

Router isn't the bottleneck. Both API and data processing are the problem you could look in another language for robust and reliability where speed is a concern and static compiled language are exist to solve these problem.

 

Thanks for your comment. I shall benchmark memory consumption in the next article.