loading...
Cover image for Benchmark of PHP Altorouter

Benchmark of PHP Altorouter

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

Hello all! The time has come to benchmark Altorouter and compare it with the Mezon router.

As usual we have two 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.

Changes in benchmark

Since the last benchmarks I have implemented routes caching. So our benchmark for the first will look like this:

$router = \Mezon\Benchmark\RouteGenerator::generateMezonStaticRoutes(1000);
$router->dumpOnDisk();
$router->clear();

$startTime = microtime(true);
for ($i = 0; $i < \Mezon\Benchmark\Base::$iterationsAmount; $i ++) {
    $router->loadFromDisk();
    $router->callRoute('/static/' . rand(0, 1000 - 1));
}
return microtime(true) - $startTime;

As you can see we create the cache before benchmarking and then using it.

And for the Altorouter the URL initialization function will look like this:

public static function generateAltorouterStaticRoutes(int $amount): \AltoRouter
{
    $router = new \AltoRouter();

    for ($i = 0; $i < $amount; $i ++) {
        $router->map('GET', '/static/' . $i, '\Mezon\Benchmark\staticCallback');
    }

    return $router;
}

The function for initialization non-static routes is alomost the same so I shall not spend your time on it.

And our benchmarks will be:

// first case, static routes
for ($i = 0; $i < \Mezon\Benchmark\Base::$iterationsAmount; $i ++) {
    $router = \Mezon\Benchmark\RouteGenerator::generateAltorouterStaticRoutes(1000);

    $router->match('/static/' . rand(0, 1000 - 1))['target']();
}

And

// second case, static routes also 
for ($i = 0; $i < \Mezon\Benchmark\Base::$iterationsAmount; $i ++) {
    $router->match('/static/' . rand(0, 1000 - 1));
}

The code for non-static routes is almost the same.

Let's see the results (the bigger numbers means better)!

table

As you can see Mezon router is up to 20 times faster then Altorouter almost in all cases. Only in one case Altorouter is faster.

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 on Github )

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

Discussion

markdown guide