Home

Awesome

PHP Router Benchmark

The intent here is to benchmark different PHP routing solutions. This is a micro-optimization, done purely out of dumb curiosity.

History Records

Please see ./records

Test Router List

always use dev-master branch

Installation

Clone the repo, run composer install, run php run-tests.php.

You can install the Pux extension to test that as well. If the extension is not installed, the tests will fallback to the pure PHP implementation of Pux.

To test the R3 library, you also need to install that extension. If the extension is not installed, the tests for R3 will be skipped.

Currently

The current test creates 1000 routes, each with a randomized prefix and postfix, with 9 parameters each.

It was run with the Pux and R3 extensions enabled.

An example route: /9b37eef21e/{arg1}/{arg2}/{arg3}/{arg4}/{arg5}/{arg6}/{arg7}/{arg8}/{arg9}/bda37e9f9b

Collect Routes Time(ms):

collect code write in the first-test-cases.php

Worst-case matching

php run-worst-tests.php

This benchmark matches the last route and unknown route. It generates a randomly prefixed and suffixed route in an attempt to thwart any optimization. 1,000 routes each with 9 arguments.

This benchmark consists of 22 tests. Each test is executed 1,000 times, the results pruned, and then averaged. Values that fall outside of 3 standard d eviations of the mean are discarded.

Test NameResultsTime(ms)+ IntervalChange
inhere/sroute(ORouter) - unknown route (1000 routes)9870.010222+0.000000baseline
inhere/sroute(SRouter) - unknown route (1000 routes)9840.012239+0.00201720% slower
inhere/sroute(SRouter) - last route (1000 routes)9990.024386+0.014820155% slower
inhere/sroute(ORouter) - last route (1000 routes)9750.024554+0.014989157% slower
Symfony Cached - last route (1000 routes)9970.029091+0.019525204% slower
Symfony Cached - unknown route (1000 routes)9850.037226+0.027661289% slower
FastRoute - unknown route (1000 routes)9880.089904+0.080338840% slower
FastRoute(cached) - unknown route (1000 routes)9880.091358+0.081792855% slower
FastRoute(cached) - last route (1000 routes)9990.092567+0.083001868% slower
FastRoute - last route (1000 routes)9990.113668+0.1041031088% slower
phroute/phroute - unknown route (1000 routes)9870.168871+0.1593051665% slower
phroute/phroute - last route (1000 routes)9990.169914+0.1603481676% slower
Pux PHP - unknown route (1000 routes)9810.866280+0.8567148956% slower
Pux PHP - last route (1000 routes)9990.941322+0.9317579741% slower
AltoRouter - unknown route (1000 routes)9822.245384+2.23581923373% slower
AltoRouter - last route (1000 routes)9792.281995+2.27242923756% slower
Symfony - unknown route (1000 routes)9842.488247+2.47868125912% slower
Symfony - last route (1000 routes)9992.540170+2.53060526455% slower
Macaw - unknown route (1000 routes)9822.617635+2.60806927265% slower
Macaw - last route (1000 routes)9992.700128+2.69056228127% slower
noodlehaus/dispatch - unknown route (1000 routes)9724.729197+4.71963249339% slower
noodlehaus/dispatch - last route (1000 routes)9994.942050+4.93248451564% slower

First route matching

php run-first-tests.php

This benchmark tests how quickly each router can match the first route. 1,000 routes each with 9 arguments.

This benchmark consists of 11 tests. Each test is executed 1,000 times, the results pruned, and then averaged. Values that fall outside of 3 standard deviations of the mean are discarded.

Test NameResultsTime(ms)+ IntervalChange
Pux PHP - first route(1000)9970.006587+0.000000baseline
FastRoute - first route(1000)9990.008751+0.00216533% slower
phroute/phroute - first route (1000 routes)9990.021902+0.015315233% slower
Symfony Dumped - first route9970.022254+0.015667238% slower
ORouter - first route(1000)9930.025026+0.018440280% slower
SRouter - first route(1000)9970.025553+0.018967288% slower
noodlehaus/dispatch - first route (1000 routes)9890.030126+0.023540357% slower
AltoRouter - first route (1000 routes)9940.041488+0.034902530% slower
Symfony - first route9910.047335+0.040748619% slower
FastRoute(cached) - first route(1000)9990.092703+0.0861171307% slower
Macaw - first route (1000 routes)9992.710132+2.70354541047% slower