|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of Flight Routing. |
|
7
|
|
|
* |
|
8
|
|
|
* PHP version 7.4 and above required |
|
9
|
|
|
* |
|
10
|
|
|
* @author Divine Niiquaye Ibok <[email protected]> |
|
11
|
|
|
* @copyright 2019 Biurad Group (https://biurad.com/) |
|
12
|
|
|
* @license https://opensource.org/licenses/BSD-3-Clause License |
|
13
|
|
|
* |
|
14
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
15
|
|
|
* file that was distributed with this source code. |
|
16
|
|
|
*/ |
|
17
|
|
|
|
|
18
|
|
|
namespace Flight\Routing\Tests\Benchmarks; |
|
19
|
|
|
|
|
20
|
|
|
use Flight\Routing\Exceptions\MethodNotAllowedException; |
|
21
|
|
|
use Flight\Routing\{RouteCollection, Router}; |
|
22
|
|
|
use Flight\Routing\Interfaces\RouteMatcherInterface; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @Groups({"performance"}) |
|
26
|
|
|
*/ |
|
27
|
|
|
final class PerformanceBench extends RouteBench |
|
28
|
|
|
{ |
|
29
|
|
|
/** |
|
30
|
|
|
* {@inheritdoc} |
|
31
|
|
|
*/ |
|
32
|
|
|
public function createDispatcher(string $cache = null): RouteMatcherInterface |
|
33
|
|
|
{ |
|
34
|
|
|
$router = new Router(null, $cache); |
|
35
|
|
|
$router->setCollection(static function (RouteCollection $routes): void { |
|
36
|
|
|
for ($i = 1; $i < self::MAX_ROUTES; ++$i) { |
|
37
|
|
|
if (199 === $i) { |
|
38
|
|
|
$routes->add('//localhost.com/route'.$i, ['GET'])->bind('static-'.$i); |
|
39
|
|
|
$routes->add('//{host}/route{foo}/'.$i, ['GET'])->bind('no-static-'.$i); |
|
40
|
|
|
continue; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
$routes->add('/route'.$i, ['GET'])->bind('static-'.$i); |
|
44
|
|
|
$routes->add('/route{foo}/'.$i, ['GET'])->bind('no-static-'.$i); |
|
45
|
|
|
} |
|
46
|
|
|
}); |
|
47
|
|
|
|
|
48
|
|
|
return $router; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* {@inheritdoc} |
|
53
|
|
|
*/ |
|
54
|
|
|
public function provideStaticRoutes(): iterable |
|
55
|
|
|
{ |
|
56
|
|
|
yield 'first' => [ |
|
57
|
|
|
'method' => 'GET', |
|
58
|
|
|
'route' => '/route0', |
|
59
|
|
|
'result' => ['handler' => null, 'prefix' => '/route0', 'path' => '/route0', 'methods' => ['GET' => true], 'name' => 'static-0'], |
|
60
|
|
|
]; |
|
61
|
|
|
|
|
62
|
|
|
yield 'middle' => [ |
|
63
|
|
|
'method' => 'GET', |
|
64
|
|
|
'route' => '//localhost/route199', |
|
65
|
|
|
'result' => ['handler' => null, 'hosts' => ['localhost' => true], 'prefix' => '/route199', 'path' => '/route199', 'methods' => ['GET' => true], 'name' => 'static-199'], |
|
66
|
|
|
]; |
|
67
|
|
|
|
|
68
|
|
|
yield 'last' => [ |
|
69
|
|
|
'method' => 'GET', |
|
70
|
|
|
'route' => '/route399', |
|
71
|
|
|
'result' => ['handler' => null, 'prefix' => '/route399', 'path' => '/route399', 'methods' => ['GET' => true], 'name' => 'static-399'], |
|
72
|
|
|
]; |
|
73
|
|
|
|
|
74
|
|
|
yield 'invalid-method' => [ |
|
75
|
|
|
'method' => 'PUT', |
|
76
|
|
|
'route' => '/route399', |
|
77
|
|
|
'result' => MethodNotAllowedException::class, |
|
78
|
|
|
]; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* {@inheritdoc} |
|
83
|
|
|
*/ |
|
84
|
|
|
public function provideDynamicRoutes(): iterable |
|
85
|
|
|
{ |
|
86
|
|
|
yield 'first' => [ |
|
87
|
|
|
'method' => 'GET', |
|
88
|
|
|
'route' => '/routebar/0', |
|
89
|
|
|
'result' => ['handler' => null, 'prefix' => '/route', 'path' => '/route{foo}/0', 'methods' => ['GET' => true], 'name' => 'not-static-0', ['arguments' => ['foo' => 'bar']]], |
|
90
|
|
|
]; |
|
91
|
|
|
|
|
92
|
|
|
yield 'middle' => [ |
|
93
|
|
|
'method' => 'GET', |
|
94
|
|
|
'route' => '//localhost/routebar/199', |
|
95
|
|
|
'result' => ['handler' => null, 'hosts' => ['{host}' => true], 'prefix' => '/route', 'path' => '/route{foo}/199', 'methods' => ['GET' => true], 'name' => 'not-static-199', ['arguments' => ['foo' => 'bar']]], |
|
96
|
|
|
]; |
|
97
|
|
|
|
|
98
|
|
|
yield 'last' => [ |
|
99
|
|
|
'method' => 'GET', |
|
100
|
|
|
'route' => '/routebar/399', |
|
101
|
|
|
'result' => ['handler' => null, 'prefix' => '/route', 'path' => '/route{foo}/399', 'methods' => ['GET' => true], 'name' => 'not-static-399', ['arguments' => ['foo' => 'bar']]], |
|
102
|
|
|
]; |
|
103
|
|
|
|
|
104
|
|
|
yield 'invalid-method' => [ |
|
105
|
|
|
'method' => 'PUT', |
|
106
|
|
|
'route' => '/routebar/399', |
|
107
|
|
|
'result' => MethodNotAllowedException::class, |
|
108
|
|
|
]; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* {@inheritdoc} |
|
113
|
|
|
*/ |
|
114
|
|
|
public function provideOtherScenarios(): iterable |
|
115
|
|
|
{ |
|
116
|
|
|
yield 'non-existent' => [ |
|
117
|
|
|
'method' => 'GET', |
|
118
|
|
|
'route' => '/testing', |
|
119
|
|
|
'result' => null, |
|
120
|
|
|
]; |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|