-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathperformance.php
More file actions
165 lines (133 loc) · 5.4 KB
/
Copy pathperformance.php
File metadata and controls
165 lines (133 loc) · 5.4 KB
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
<?php
/**
* FastD 路由性能对比测试工具
* 用于检测不同路由数量下的性能表现
*
* @author jan huang <bboyjanhuang@gmail.com>
* @copyright 2016
*
* @link https://www.github.com/janhuang
* @link http://www.fast-d.cn/
*/
use FastD\Http\Request\ServerRequest;
use FastD\Http\Response\Text as Response;
use FastD\Routing\Collection\RouteCollection;
use FastD\Routing\RouteMatcher as RouteDispatcher;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
include __DIR__ . '/vendor/autoload.php';
class DemoHandler implements MiddlewareInterface
{
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
return new Response(200, 'hello from demo handler');
}
}
class TestController
{
public function index(ServerRequestInterface $request, RequestHandlerInterface $handler)
{
return new Response(200, 'hello from controller@index');
}
public function show(ServerRequestInterface $request, RequestHandlerInterface $handler, $id)
{
return new Response(200, "hello from controller@show id: $id");
}
}
function testFunction(ServerRequestInterface $request, RequestHandlerInterface $handler, $param)
{
return new Response(200, "hello from function param: $param");
}
// 性能测试主函数
function runBenchmark($testName, $callback) {
$iterations = 10;
$times = [];
for ($i = 0; $i < $iterations; $i++) {
$start = microtime(true);
$callback();
$end = microtime(true);
$times[] = ($end - $start) * 1000; // 转换为毫秒
}
$avgTime = array_sum($times) / count($times);
$minTime = min($times);
$maxTime = max($times);
printf("%-35s | Avg: %8.4fms | Min: %8.4fms | Max: %8.4fms\n", $testName, $avgTime, $minTime, $maxTime);
}
// 测试不同路由数量的性能
$routeCounts = [1000, 5000, 10000, 50000, 100000];
echo "\n=== FastD 路由注册性能对比测试 ===\n\n";
printf("%-10s | %-30s\n", "路由数量", "平均注册时间(ms)");
echo str_repeat("-", 50) . "\n";
foreach ($routeCounts as $count) {
$startTime = microtime(true);
$routeCollection = new RouteCollection();
for ($i = 0; $i < $count; $i++) {
$routeCollection->addRoute('GET', '/route' . $i . '/{param}', 'DemoHandler');
}
$endTime = microtime(true);
$registrationTime = ($endTime - $startTime) * 1000;
printf("%-10d | %8.4f\n", $count, $registrationTime);
}
echo "\n=== FastD 路由匹配性能对比测试 ===\n\n";
// 测试不同数量的路由匹配性能
echo "静态路由匹配性能对比:\n";
printf("%-10s | %-35s\n", "路由数量", "平均匹配时间(ms)");
echo str_repeat("-", 50) . "\n";
foreach ([1000, 5000, 10000, 50000, 100000] as $count) {
$routeCollection = new RouteCollection();
for ($i = 0; $i < $count; $i++) {
$routeCollection->addRoute('GET', '/static' . $i, 'DemoHandler');
}
$routeMatcher = new RouteDispatcher($routeCollection);
$request = new ServerRequest('GET', '/static' . ($count / 2)); // 匹配中间的路由
$iterations = 100;
$start = microtime(true);
for ($i = 0; $i < $iterations; $i++) {
$response = $routeMatcher->dispatch($request);
}
$end = microtime(true);
$matchTime = (($end - $start) / $iterations) * 1000;
printf("%-10d | %8.4f\n", $count, $matchTime);
}
echo "\n动态路由匹配性能测试 (单个动态路由):
";
$dynamicRouteCollection = new RouteCollection();
$dynamicRouteCollection->addRoute('GET', '/dynamic/{id}', 'DemoHandler');
$dynamicMatcher = new RouteDispatcher($dynamicRouteCollection);
$dynamicRequest = new ServerRequest('GET', '/dynamic/12345');
runBenchmark('Dynamic Route Match (100000 routes)', function() use ($dynamicMatcher, $dynamicRequest) {
$response = $dynamicMatcher->dispatch($dynamicRequest);
});
echo "\n=== 不同处理器类型性能测试 (100000路由环境) ===\n";
// 在10000路由环境下测试不同处理器类型
$routeCollection = new RouteCollection();
for ($i = 0; $i < 10000; $i++) {
$routeCollection->addRoute('GET', '/env' . $i . '/{param}', 'DemoHandler');
}
// 控制器@方法处理器
$routeCollectionCtrl = new RouteCollection();
$routeCollectionCtrl->get('/controller/{id}', 'TestController@show');
$ctrlMatcher = new RouteDispatcher($routeCollectionCtrl);
$ctrlRequest = new ServerRequest('GET', '/controller/999');
runBenchmark('Controller Handler', function() use ($ctrlMatcher, $ctrlRequest) {
$response = $ctrlMatcher->dispatch($ctrlRequest);
});
// 中间件处理器
$routeCollectionMid = new RouteCollection();
$routeCollectionMid->get('/middleware', 'DemoHandler');
$midMatcher = new RouteDispatcher($routeCollectionMid);
$midRequest = new ServerRequest('GET', '/middleware');
runBenchmark('Middleware Handler', function() use ($midMatcher, $midRequest) {
$response = $midMatcher->dispatch($midRequest);
});
// 函数处理器
$routeCollectionFunc = new RouteCollection();
$routeCollectionFunc->get('/function/{param}', 'testFunction');
$funcMatcher = new RouteDispatcher($routeCollectionFunc);
$funcRequest = new ServerRequest('GET', '/function/value');
runBenchmark('Function Handler', function() use ($funcMatcher, $funcRequest) {
$response = $funcMatcher->dispatch($funcRequest);
});
echo "\n=== 性能测试完成 ===\n";