This repository has been archived by the owner on Jun 2, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathfirst-route-tests.php
226 lines (191 loc) · 7.33 KB
/
first-route-tests.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
<?php
namespace FirstRouteMatching;
use Nice\Benchmark\Benchmark;
use Nice\Benchmark\ResultPrinter\MarkdownPrinter;
/**
* Sets up the First-route matching benchmark.
*
* This benchmark tests how quickly each router can match the first route
*
* @param $numIterations
* @param $numRoutes
* @param $numArgs
*
* @return Benchmark
*/
function setupBenchmark($numIterations, $numRoutes, $numArgs)
{
$benchmark = new Benchmark($numIterations, 'First route matching', new MarkdownPrinter());
$benchmark->setDescription(sprintf(
'This benchmark tests how quickly each router can match the first route. %s routes each with %s arguments.',
number_format($numRoutes),
$numArgs
));
setupAura2($benchmark, $numRoutes, $numArgs);
setupFastRoute($benchmark, $numRoutes, $numArgs);
if (extension_loaded('r3')) {
setupR3($benchmark, $numRoutes, $numArgs);
} else {
echo "R3 extension is not loaded. Skipping initialization for \"First route matching\" test using R3.\n";
}
setupSymfony2($benchmark, $numRoutes, $numArgs);
setupSymfony2Optimized($benchmark, $numRoutes, $numArgs);
setupPux($benchmark, $numRoutes, $numArgs);
return $benchmark;
}
function getRandomParts()
{
$rand = md5(uniqid(mt_rand(), true));
return array(
substr($rand, 0, 10),
substr($rand, -10),
);
}
/**
* Sets up R3 tests
*/
function setupR3(Benchmark $benchmark, $routes, $args)
{
$argString = implode('/', array_map(function ($i) { return "{arg$i}"; }, range(1, $args)));
$str = $firstStr = $lastStr = '';
$router = r3_tree_create_persist("app", 10);
if (!r3_tree_is_compiled($router)) {
for ($i = 0; $i < $routes; $i++) {
list ($pre, $post) = getRandomParts();
$str = '/' . $pre . '/' . $argString . '/' . $post;
if (0 === $i) {
$firstStr = str_replace(array('{', '}'), '', $str);
}
$lastStr = str_replace(array('{', '}'), '', $str);
r3_tree_insert($router, $str, "handler" . $i);
}
r3_tree_compile($router);
}
$benchmark->register(sprintf('php-r3 - first route', $routes), function () use ($router, $firstStr) {
$data = r3_tree_match($router, $firstStr);
});
}
/**
* Sets up FastRoute tests
*/
function setupFastRoute(Benchmark $benchmark, $routes, $args)
{
$argString = implode('/', array_map(function ($i) { return "{arg$i}"; }, range(1, $args)));
$str = $firstStr = $lastStr = '';
$router = \FastRoute\simpleDispatcher(function ($router) use ($routes, $argString, &$lastStr, &$firstStr) {
for ($i = 0; $i < $routes; $i++) {
list ($pre, $post) = getRandomParts();
$str = '/' . $pre . '/' . $argString . '/' . $post;
if (0 === $i) {
$firstStr = str_replace(array('{', '}'), '', $str);
}
$lastStr = str_replace(array('{', '}'), '', $str);
$router->addRoute('GET', $str, 'handler' . $i);
}
});
$benchmark->register(sprintf('FastRoute - first route', $routes), function () use ($router, $firstStr) {
$route = $router->dispatch('GET', $firstStr);
});
}
/**
* Sets up Pux tests
*/
function setupPux(Benchmark $benchmark, $routes, $args)
{
$name = extension_loaded('pux') ? 'Pux ext' : 'Pux PHP';
$argString = implode('/', array_map(function ($i) { return ':arg' . $i; }, range(1, $args)));
$str = $firstStr = $lastStr = '';
$router = new \Pux\Mux;
for ($i = 0; $i < $routes; $i++) {
list ($pre, $post) = getRandomParts();
$str = '/' . $pre . '/' . $argString . '/' . $post;
if (0 === $i) {
$firstStr = str_replace(':', '', $str);
}
$lastStr = str_replace(':', '', $str);
$router->add($str, 'handler' . $i);
}
$benchmark->register(sprintf('%s - first route', $name), function () use ($router, $firstStr) {
$route = $router->match($firstStr);
});
}
/**
* Sets up Symfony 2 tests
*/
function setupSymfony2(Benchmark $benchmark, $routes, $args)
{
$argString = implode('/', array_map(function ($i) { return "{arg$i}"; }, range(1, $args)));
$str = $firstStr = $lastStr = '';
$sfRoutes = new \Symfony\Component\Routing\RouteCollection();
$router = new \Symfony\Component\Routing\Matcher\UrlMatcher($sfRoutes, new \Symfony\Component\Routing\RequestContext());
for ($i = 0, $str = 'a'; $i < $routes; $i++, $str++) {
list ($pre, $post) = getRandomParts();
$str = '/' . $pre . '/' . $argString . '/' . $post;
if (0 === $i) {
$firstStr = str_replace(array('{', '}'), '', $str);
}
$lastStr = str_replace(array('{', '}'), '', $str);
$sfRoutes->add($str, new \Symfony\Component\Routing\Route($str, array('controller' => 'handler' . $i)));
}
$benchmark->register('Symfony2 - first route', function () use ($router, $firstStr) {
$route = $router->match($firstStr);
});
}
/**
* Sets up Symfony2 optimized tests
*/
function setupSymfony2Optimized(Benchmark $benchmark, $routes, $args)
{
$argString = implode('/', array_map(function ($i) { return "{arg$i}"; }, range(1, $args)));
$str = $firstStr = $lastStr = '';
$sfRoutes = new \Symfony\Component\Routing\RouteCollection();
for ($i = 0, $str = 'a'; $i < $routes; $i++, $str++) {
list ($pre, $post) = getRandomParts();
$str = '/' . $pre . '/' . $argString . '/' . $post;
if (0 === $i) {
$firstStr = str_replace(array('{', '}'), '', $str);
}
$lastStr = str_replace(array('{', '}'), '', $str);
$sfRoutes->add($str, new \Symfony\Component\Routing\Route($str, array('controller' => 'handler' . $i)));
}
$dumper = new \Symfony\Component\Routing\Matcher\Dumper\PhpMatcherDumper($sfRoutes);
file_put_contents(__DIR__ . '/files/first-route-sf2.php', $dumper->dump(array(
'class' => 'FirstRouteSf2UrlMatcher'
)));
require_once __DIR__ . '/files/first-route-sf2.php';
$router = new \FirstRouteSf2UrlMatcher(new \Symfony\Component\Routing\RequestContext());
$benchmark->register('Symfony2 Dumped - first route', function () use ($router, $firstStr) {
$route = $router->match($firstStr);
});
}
/**
* Sets up Aura v2 tests
*
* https://github.com/auraphp/Aura.Router
*/
function setupAura2(Benchmark $benchmark, $routes, $args)
{
$argString = implode('/', array_map(function ($i) { return "{arg$i}"; }, range(1, $args)));
$lastStr = '';
$router = new \Aura\Router\Router(
new \Aura\Router\RouteCollection(
new \Aura\Router\RouteFactory()
),
new \Aura\Router\Generator()
);
for ($i = 0, $str = 'a'; $i < $routes; $i++, $str++) {
list ($pre, $post) = getRandomParts();
$str = '/' . $pre . '/' . $argString . '/' . $post;
if (0 === $i) {
$firstStr = str_replace(array('{', '}'), '', $str);
}
$lastStr = str_replace(array('{', '}'), '', $str);
$router->add($str, $str)
->addValues(array(
'controller' => 'handler' . $i
));
}
$benchmark->register('Aura v2 - first route', function () use ($router, $firstStr) {
$route = $router->match($firstStr);
});
}