r/PHPhelp • u/deadringer3480 • Jan 30 '25
How would you benchmark PHP routers?
I’m currently benchmarking popular PHP routers and have built a benchmark tool that makes it easy to add more Composer packages and run multiple test suites.
Each test runs PHP 8.4 CLI, calling a PHP-FPM server with opcache enabled via curl to better simulate a real-world scenario. The tool automatically orders results, calculates median times from 30 test runs, and updates a README file with the results.
Many benchmarks simply create a router, add routes, and then measure lookup speed for 1,000 routes. However, real-world applications often define a fixed set of routes and repeatedly call only one or a few paths. Because of this, I think both initial setup time and per-route resolution speed are important to measure.
What metrics and tests would you like to see in a PHP router benchmark? Would you be more interested in functionality, raw speed, setup time, memory usage, or something else?
Currently I have FastRoute, PHRoute, Rammewerk and Symfony. Any more to add?
3
u/Mastodont_XXX Jan 30 '25
Altorouter, klein
3
u/allen_jb Jan 30 '25 edited Jan 30 '25
Downvoted for the excessive and random bold text that makes this post hard to read. -.-(Downvote removed)Outside of that, have you looked at existing benchmarks of PHP routers / frameworks to see what they implement? You're not exactly the first person to be doing this type of benchmarking.
3
u/equilni Jan 30 '25
I don't want to read another
my router is faster than x(the next will bemy container is faster than x). There will always be improvements to existing libraries, I get it. Is is easy to use, implement, and make sense of what is going on?Maybe not a benchmark, but API differences. How easy is it to create the routes and dispatch to a given url, then call the not found/allowed handlers.
FastRoute
Phroute:
I am sure you can find some in r/php but...
Laravel (Use Torch as a guide)
laminas Router\
Nette Routing
Would be awesome to see Temptest Router
Bramus/Router
I could go into more micro frameworks that don't extract out the router component, but you may or may not want that... for instance:
Leaf - micro framework
noodlehaus/dispatch - micro framework
For grins, you can compare how Slim & League/Router compare to FastRoute
More would be Flight and FatFree
3
u/MateusAzevedo Jan 30 '25
I think it's perfectly fine to benchmark routers without involving real web requests. I'd say this setup isn't necessary.
The most important part of a router is the lookup algorithm, that's why.
I agree with that, but I consider these to be different things that needs to be measured independently. Remember that many routers have some sort of compiling/caching that solves the bootstrap step.
What I'd do in your case: make the benchmark code CLI only; Create measurements for both bootstrap and lookup (don't forget to enable caching mechanism); You can also create a "combined" benchmark if you want.
In case you didn't see this already, I recommend reading Nikic's blog post about FastRoute. The key takeaway there is that benchmarking a router isn't straight forward, because you want to validate different contexts or use cases, that can drastically change the results. Yes, I know that the examples he used with 9 placeholders is exaggerated, but he's testing extreme cases to make sure it covers all common scenarios (if it works well for 9 placeholders, it sure works well for 1). So also make sure your benchmark code also test all these permutations, because without that, I think it's an incomplete test.
2
u/jmp_ones Jan 30 '25
Agreed; this is how I did it for AutoRoute, and it has a similar approach: https://github.com/pmjones/AutoRoute-benchmark
1
u/lsv20 Jan 30 '25
I would like to know if the router can handle parameters with
/in it./category/<category-slug>/<product-slug>Where category slug is ofcourse
category/subcategory/subsubcategoryThats where many routers break.
1
u/minn0w Jan 30 '25
Be sure to include all setup time taken at runtime. I have seen routers that build massive multidimensional arrays that take a relatively long time to initialise, but perform fast when routing the request.
1
u/gingertek Jan 31 '25
If you want to benchmark mine, I'm curious to know where it stands against the big boys: https://github.com/ginger-tek/routy
1
u/valzargaming Jan 31 '25
https://git.sharkk.net/PHP/Router
1
u/CodeSpike Feb 01 '25
Have you looked at https://github.com/kktsvetkov/benchmark-php-routing ?
I forked that when I wanted to benchmark my router against the rest.