PHP route performance test PHP route controller performance test
Some time ago, I wrote a micro-route controller. Although it is designed to avoid the time-consuming operations such as regular expression matching and array loop. Save time as much as possible. But that's all theoretical. I tested it today.
Code
Github has a repository named php-router-benchmark to test the routing distribution performance.
Test the performance of the php-r3, Pux, FastRoute, Symfony2 Dumped, Symfony2, and Aura v2. In general, R3 and FastRoute are the best in the worst case. R3 and Pux are plug-ins implemented in C language, with performance advantages over the language.
Test
First fork a copy of the benchmark library and added the test code of the Router. The same test conditions. Pux installation fails because R3 is not installed. Therefore, this test only applies to FastRoute, Symfony2 Dumped, Symfony2, Aura v2, and Router.
In this case, all these are implemented in pure PHP. The other two C language implementations will not be compared this time.
Result
The first is the worst case test, which contains two test cases. one is 404 that cannot be found, and the other is that the last one in the list can be matched, that is, the two worst cases.
Worst-case matching
This benchmark matches the last route and unknown route. It generates a randomly prefixed and suffixed route in an attempt to th1_any optimization. 1,000 routes each with 9 arguments.
This benchmark consists of 10 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 Name |
Results |
Time |
+ Interval |
Change |
Router-unknown route (1000 routes) |
993 |
0.0000232719 |
+ 0.0000000000 |
Baseline |
Router-last route (1000 routes) |
981 |
0.0000955424 |
+ 0.0000722705 |
311% slower |
FastRoute-unknown route (1000 routes) |
990 |
0.0005051955 |
+ 0.0004819236 |
2071% slower |
FastRoute-last route (1000 routes) |
998 |
0.0005567203 |
+ 0.0005334484 |
2292% slower |
Symfony2 Dumped-unknown route (1000 routes) |
998 |
0.0006116139 |
+ 0.0005883420 |
2528% slower |
Symfony2 Dumped-last route (1000 routes) |
998 |
0.0007765370 |
+ 0.0007532651 |
3237% slower |
Symfony2-unknown route (1000 routes) |
996 |
0.0028456177 |
+ 0.0028223458 |
12128% slower |
Symfony2-last route (1000 routes) |
993 |
0.0030129542 |
+ 0.0029896823 |
12847% slower |
Aura v2-last route (1000 routes) |
989 |
0.1707107230 |
+ 0.1706874511 |
733450% slower |
Aura v2-unknown route (1000 routes) |
988 |
0.1798588730 |
+ 0.1798356011 |
772760% slower |
View the data in the preceding table. my own Router performs the best in the worst case of both test cases. And leave the other few far away from the street. It seems that this is mainly because the PHP array in the tree structure is used to store route tables. Therefore, the time complexity of O (log n) can always be reached during traversal.
Then let's look at the best situation, that is, the situation that can be matched in the first url.
First route matching
This benchmark tests how quickly each router can match the first route. 1,000 routes each with 9 arguments.
This benchmark consists of 5 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 Name |
Results |
Time |
+ Interval |
Change |
FastRoute-first route |
998 |
0.0000498390 |
+ 0.0000000000 |
Baseline |
Symfony2 Dumped-first route |
995 |
0.0000517531 |
+ 0.0000019141 |
4% slower |
Router-first route |
994 |
0.0001499363 |
+ 0.0001000972 |
201% slower |
Aura v2-first route |
998 |
0.0008559464 |
+ 0.0008061073 |
1617% slower |
Symfony2-first route |
998 |
0.0012734995 |
+ 0.0012236604 |
2455% slower |
View the data in this table. each database has only one test case. Is the "best" test. The reason why the quotation marks are added here is that the Router uses a tree structure for storage. The so-called "best" situations of other libraries do not apply. It cannot be matched in the first one. Therefore, in this test case, the Router still comparesAverage timeAnd several other librariesBest timeFor comparison !!! (Compare the absolute values of Time in the two tables above and below, we can see that the "best" situations of the other databases are much better than the above worst cases, however, the performance of the Router database is indeed the opposite, and the time is relatively close regardless of the situation, which is related to the time complexity of the previous tree node traversal)
Postscript
This time I tested several routing controllers implemented by pure PHP. next time I will continue to compare the libraries implemented by the two C languages !!!