|
24 | 24 |
|
25 | 25 | $envInt = static function (string $key, int $default): int { |
26 | 26 | $value = getenv($key); |
27 | | - return $value === false ? $default : (int)$value; |
| 27 | + |
| 28 | + return $value === false ? $default : (int) $value; |
28 | 29 | }; |
29 | 30 | $envFloat = static function (string $key, float $default): float { |
30 | 31 | $value = getenv($key); |
31 | | - return $value === false ? $default : (float)$value; |
| 32 | + |
| 33 | + return $value === false ? $default : (float) $value; |
32 | 34 | }; |
33 | 35 | $envBool = static function (string $key, bool $default): bool { |
34 | 36 | $value = getenv($key); |
35 | 37 | if ($value === false) { |
36 | 38 | return $default; |
37 | 39 | } |
38 | 40 | $parsed = filter_var($value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE); |
| 41 | + |
39 | 42 | return $parsed ?? $default; |
40 | 43 | }; |
41 | 44 |
|
|
61 | 64 | $connections = max(300000, $concurrent * 100); |
62 | 65 | if ($payloadBytes > 0) { |
63 | 66 | $connections = max(100000, $concurrent * 20); |
64 | | - $maxByTarget = (int)floor($targetBytes / max(1, $payloadBytes)); |
| 67 | + $maxByTarget = (int) floor($targetBytes / max(1, $payloadBytes)); |
65 | 68 | if ($maxByTarget > 0) { |
66 | 69 | $connections = min($connections, $maxByTarget); |
67 | 70 | } |
68 | 71 | } |
69 | 72 | } else { |
70 | | - $connections = (int)$connectionsEnv; |
| 73 | + $connections = (int) $connectionsEnv; |
71 | 74 | } |
72 | 75 | $sampleTarget = $envInt('BENCH_SAMPLE_TARGET', 200000); |
73 | | - $sampleEvery = $envInt('BENCH_SAMPLE_EVERY', max(1, (int)ceil($connections / max(1, $sampleTarget)))); |
| 76 | + $sampleEvery = $envInt('BENCH_SAMPLE_EVERY', max(1, (int) ceil($connections / max(1, $sampleTarget)))); |
74 | 77 |
|
75 | 78 | if ($connections < 1) { |
76 | 79 | echo "Invalid connection count.\n"; |
| 80 | + |
77 | 81 | return; |
78 | 82 | } |
79 | 83 | if ($concurrent > $connections) { |
80 | 84 | $concurrent = $connections; |
81 | 85 | } |
82 | 86 | if ($concurrent < 1) { |
83 | 87 | echo "Invalid concurrency.\n"; |
| 88 | + |
84 | 89 | return; |
85 | 90 | } |
86 | 91 |
|
|
130 | 135 | } |
131 | 136 |
|
132 | 137 | if ($persistent) { |
133 | | - if ($payloadBytes <= 0) { |
134 | | - echo "Persistent mode requires BENCH_PAYLOAD_BYTES > 0.\n"; |
135 | | - return; |
136 | | - } |
| 138 | + if ($payloadBytes <= 0) { |
| 139 | + echo "Persistent mode requires BENCH_PAYLOAD_BYTES > 0.\n"; |
| 140 | + |
| 141 | + return; |
| 142 | + } |
137 | 143 |
|
138 | 144 | echo "Mode: persistent\n"; |
139 | 145 | if ($streamBytes > 0) { |
|
162 | 168 | Coroutine::create(function () use ( |
163 | 169 | $host, |
164 | 170 | $port, |
165 | | - $protocol, |
166 | 171 | $timeout, |
167 | 172 | $payloadBytes, |
168 | 173 | $payloadDataBytes, |
|
181 | 186 | $client = new Client(SWOOLE_SOCK_TCP); |
182 | 187 | $client->set(['timeout' => $timeout]); |
183 | 188 |
|
184 | | - if (!$client->connect($host, $port, $timeout)) { |
| 189 | + if (! $client->connect($host, $port, $timeout)) { |
185 | 190 | $errors++; |
186 | 191 | $channel->push([ |
187 | 192 | 'bytes' => 0, |
188 | 193 | 'ops' => 0, |
189 | 194 | 'errors' => $errors, |
190 | 195 | ]); |
| 196 | + |
191 | 197 | return; |
192 | 198 | } |
193 | 199 |
|
|
199 | 205 | 'ops' => 0, |
200 | 206 | 'errors' => $errors, |
201 | 207 | ]); |
| 208 | + |
202 | 209 | return; |
203 | 210 | } |
204 | 211 |
|
|
211 | 218 | 'ops' => 0, |
212 | 219 | 'errors' => $errors, |
213 | 220 | ]); |
| 221 | + |
214 | 222 | return; |
215 | 223 | } |
216 | 224 |
|
|
327 | 335 | $host, |
328 | 336 | $port, |
329 | 337 | $workerConnections, |
330 | | - $protocol, |
331 | 338 | $timeout, |
332 | 339 | $payloadBytes, |
333 | 340 | $payloadDataBytes, |
|
356 | 363 | 'bytes' => 0, |
357 | 364 | 'samples' => [], |
358 | 365 | ]); |
| 366 | + |
359 | 367 | return; |
360 | 368 | } |
361 | 369 |
|
|
367 | 375 | 'timeout' => $timeout, |
368 | 376 | ]); |
369 | 377 |
|
370 | | - if (!$client->connect($host, $port, $timeout)) { |
| 378 | + if (! $client->connect($host, $port, $timeout)) { |
371 | 379 | $errors++; |
372 | 380 | $latency = (microtime(true) - $connStart) * 1000; |
373 | 381 | $count++; |
|
381 | 389 | if (($count % $sampleEvery) === 0) { |
382 | 390 | $samples[] = $latency; |
383 | 391 | } |
| 392 | + |
384 | 393 | continue; |
385 | 394 | } |
386 | 395 |
|
|
469 | 478 | $max = $result['max']; |
470 | 479 | } |
471 | 480 | } |
472 | | - if (!empty($result['samples'])) { |
| 481 | + if (! empty($result['samples'])) { |
473 | 482 | $samples = array_merge($samples, $result['samples']); |
474 | 483 | } |
475 | 484 | } |
|
479 | 488 | // Calculate statistics |
480 | 489 | if ($totalCount === 0) { |
481 | 490 | echo "No connections completed.\n"; |
| 491 | + |
482 | 492 | return; |
483 | 493 | } |
484 | 494 |
|
|
487 | 497 |
|
488 | 498 | sort($samples); |
489 | 499 | $sampleCount = count($samples); |
490 | | - $p50 = $sampleCount ? $samples[(int)floor($sampleCount * 0.5)] : 0.0; |
491 | | - $p95 = $sampleCount ? $samples[(int)floor($sampleCount * 0.95)] : 0.0; |
492 | | - $p99 = $sampleCount ? $samples[(int)floor($sampleCount * 0.99)] : 0.0; |
| 500 | + $p50 = $sampleCount ? $samples[(int) floor($sampleCount * 0.5)] : 0.0; |
| 501 | + $p95 = $sampleCount ? $samples[(int) floor($sampleCount * 0.95)] : 0.0; |
| 502 | + $p99 = $sampleCount ? $samples[(int) floor($sampleCount * 0.99)] : 0.0; |
493 | 503 | $throughputGb = $bytes > 0 ? ($bytes / $totalTime / 1024 / 1024 / 1024) : 0.0; |
494 | 504 |
|
495 | 505 | echo "\nResults:\n"; |
|
511 | 521 | // Performance goals |
512 | 522 | echo "\nPerformance Goals:\n"; |
513 | 523 | echo "==================\n"; |
514 | | - echo sprintf("Connections/sec goal: 100k+... %s\n", |
515 | | - $connPerSec >= 100000 ? "✓ PASS" : "✗ FAIL"); |
516 | | - echo sprintf("Forwarding overhead goal: <1ms... %s\n", |
517 | | - $avgLatency < 1.0 ? "✓ PASS" : "✗ FAIL"); |
| 524 | + echo sprintf( |
| 525 | + "Connections/sec goal: 100k+... %s\n", |
| 526 | + $connPerSec >= 100000 ? '✓ PASS' : '✗ FAIL' |
| 527 | + ); |
| 528 | + echo sprintf( |
| 529 | + "Forwarding overhead goal: <1ms... %s\n", |
| 530 | + $avgLatency < 1.0 ? '✓ PASS' : '✗ FAIL' |
| 531 | + ); |
518 | 532 | }); |
0 commit comments