-
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy path06-stress-test.php
More file actions
163 lines (135 loc) · 3.59 KB
/
06-stress-test.php
File metadata and controls
163 lines (135 loc) · 3.59 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
<?php
declare(strict_types=1);
require implode(DIRECTORY_SEPARATOR, [__DIR__, "..", "vendor", "autoload.php"]);
use Gt\Fetch\Http;
use Gt\Http\Response;
const DEFAULT_URL = "http://127.0.0.1:8080/?type=big-page&size-kb=512&delay-ms=25&speed-kbps=2048";
const DEFAULT_START = 10;
const DEFAULT_STEP = 10;
const DEFAULT_MAX = 1000;
ini_set("memory_limit", "2G");
if(in_array("--help", $argv, true) || in_array("-h", $argv, true)) {
printf(<<<TEXT
Stress test example for PhpGt/Fetch.
Usage:
php example/06-stress-test.php [url] [start] [step] [max]
Defaults:
url = %s
start = %d
step = %d
max = %d
Quick local smoke test:
php example/target/06-stress-test-server.php
php example/06-stress-test.php
The script increases the number of simultaneous fetches each round and
reports timing plus success/failure counts. The first failing round is a
practical concurrency ceiling for the current environment.
The bundled target server accepts concurrent connections in a single
non-blocking process and can simulate variable latency and response sizes.
You can still point this script at another controlled endpoint if needed.
TEXT, DEFAULT_URL, DEFAULT_START, DEFAULT_STEP, DEFAULT_MAX);
exit(0);
}
$url = $argv[1] ?? DEFAULT_URL;
$start = max(1, (int)($argv[2] ?? DEFAULT_START));
$step = max(1, (int)($argv[3] ?? DEFAULT_STEP));
$max = max($start, (int)($argv[4] ?? DEFAULT_MAX));
printf("Target: %s\n", $url);
printf(
"Ramping concurrency from %d to %d in steps of %d\n\n",
$start,
$max,
$step
);
$firstFailureAt = null;
for($concurrency = $start; $concurrency <= $max; $concurrency += $step) {
$http = new Http();
$successCount = 0;
$errorCount = 0;
$statusCounts = [];
$errorMessages = [];
$startedAt = microtime(true);
for($i = 0; $i < $concurrency; $i++) {
$requestUrl = sprintf(
"%s%srequest-id=%d-%d",
$url,
str_contains($url, "?") ? "&" : "?",
$concurrency,
$i
);
$http->fetch($requestUrl)
->then(function(Response $response) use(
&$successCount,
&$errorCount,
&$statusCounts
) {
$statusCode = $response->status;
$statusCounts[$statusCode] ??= 0;
$statusCounts[$statusCode]++;
if($response->ok) {
$successCount++;
return;
}
$errorCount++;
})
->catch(function(Throwable $throwable) use(
&$errorCount,
&$errorMessages
) {
$errorCount++;
$errorMessages[$throwable->getMessage()] ??= 0;
$errorMessages[$throwable->getMessage()]++;
});
}
try {
$http->wait();
}
catch(Throwable $throwable) {
$errorCount++;
$errorMessages[$throwable->getMessage()] ??= 0;
$errorMessages[$throwable->getMessage()]++;
}
$durationSeconds = microtime(true) - $startedAt;
$requestsPerSecond = $durationSeconds > 0
? $concurrency / $durationSeconds
: 0;
printf(
"Concurrency %4d | ok %4d | errors %4d | %.3fs | %.2f req/s | memory %4d MiB\n",
$concurrency,
$successCount,
$errorCount,
$durationSeconds,
$requestsPerSecond,
memory_get_usage(true) / 1024 / 1024,
);
if($statusCounts) {
ksort($statusCounts);
echo " Statuses: ";
foreach($statusCounts as $statusCode => $count) {
printf("%d=%d ", $statusCode, $count);
}
echo PHP_EOL;
}
if($errorMessages) {
echo " Errors: ";
foreach($errorMessages as $message => $count) {
printf("[%dx] %s ", $count, $message);
}
echo PHP_EOL;
}
if($firstFailureAt === null && $errorCount > 0) {
$firstFailureAt = $concurrency;
}
}
echo PHP_EOL;
if($firstFailureAt !== null) {
printf(
"First failing round: %d concurrent requests\n",
$firstFailureAt
);
exit(1);
}
printf(
"No failures observed up to %d concurrent requests\n",
$max
);