Skip to content

Commit d883ab7

Browse files
committed
Update proxies, examples, and benchmarks for Resolver
1 parent 86ca764 commit d883ab7

9 files changed

Lines changed: 302 additions & 216 deletions

File tree

benchmarks/http-backend.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?php
22

33
$host = getenv('BACKEND_HOST') ?: '127.0.0.1';
4-
$port = (int)(getenv('BACKEND_PORT') ?: 5678);
5-
$workers = (int)(getenv('BACKEND_WORKERS') ?: (function_exists('swoole_cpu_num') ? swoole_cpu_num() : 4));
4+
$port = (int) (getenv('BACKEND_PORT') ?: 5678);
5+
$workers = (int) (getenv('BACKEND_WORKERS') ?: (function_exists('swoole_cpu_num') ? swoole_cpu_num() : 4));
66

77
$server = new Swoole\Http\Server($host, $port, SWOOLE_PROCESS);
88

benchmarks/http.php

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,21 @@
2424

2525
$envInt = static function (string $key, int $default): int {
2626
$value = getenv($key);
27-
return $value === false ? $default : (int)$value;
27+
28+
return $value === false ? $default : (int) $value;
2829
};
2930
$envFloat = static function (string $key, float $default): float {
3031
$value = getenv($key);
31-
return $value === false ? $default : (float)$value;
32+
33+
return $value === false ? $default : (float) $value;
3234
};
3335
$envBool = static function (string $key, bool $default): bool {
3436
$value = getenv($key);
3537
if ($value === false) {
3638
return $default;
3739
}
3840
$parsed = filter_var($value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
41+
3942
return $parsed ?? $default;
4043
};
4144

@@ -47,25 +50,27 @@
4750
$timeout = $envFloat('BENCH_TIMEOUT', 10);
4851
$keepAlive = $envBool('BENCH_KEEP_ALIVE', true);
4952
$sampleTarget = $envInt('BENCH_SAMPLE_TARGET', 200000);
50-
$sampleEvery = $envInt('BENCH_SAMPLE_EVERY', max(1, (int)ceil($requests / max(1, $sampleTarget))));
53+
$sampleEvery = $envInt('BENCH_SAMPLE_EVERY', max(1, (int) ceil($requests / max(1, $sampleTarget))));
5154

5255
if ($requests < 1) {
5356
echo "Invalid request count.\n";
57+
5458
return;
5559
}
5660
if ($concurrent > $requests) {
5761
$concurrent = $requests;
5862
}
5963
if ($concurrent < 1) {
6064
echo "Invalid concurrency.\n";
65+
6166
return;
6267
}
6368

6469
echo "Configuration:\n";
6570
echo " Host: {$host}:{$port}\n";
6671
echo " Concurrent: {$concurrent}\n";
6772
echo " Total requests: {$requests}\n";
68-
echo " Keep-alive: " . ($keepAlive ? 'yes' : 'no') . "\n";
73+
echo ' Keep-alive: '.($keepAlive ? 'yes' : 'no')."\n";
6974
echo " Sample every: {$sampleEvery} req\n\n";
7075

7176
$startTime = microtime(true);
@@ -102,6 +107,7 @@
102107
'errors' => 0,
103108
'samples' => [],
104109
]);
110+
105111
return;
106112
}
107113

@@ -112,6 +118,7 @@
112118
'keep_alive' => $keepAlive,
113119
]);
114120
$client->setHeaders(['Host' => $host]);
121+
115122
return $client;
116123
};
117124

@@ -191,7 +198,7 @@
191198
$max = $result['max'];
192199
}
193200
}
194-
if (!empty($result['samples'])) {
201+
if (! empty($result['samples'])) {
195202
$samples = array_merge($samples, $result['samples']);
196203
}
197204
}
@@ -201,6 +208,7 @@
201208
// Calculate statistics
202209
if ($totalCount === 0) {
203210
echo "No requests completed.\n";
211+
204212
return;
205213
}
206214

@@ -209,9 +217,9 @@
209217

210218
sort($samples);
211219
$sampleCount = count($samples);
212-
$p50 = $sampleCount ? $samples[(int)floor($sampleCount * 0.5)] : 0.0;
213-
$p95 = $sampleCount ? $samples[(int)floor($sampleCount * 0.95)] : 0.0;
214-
$p99 = $sampleCount ? $samples[(int)floor($sampleCount * 0.99)] : 0.0;
220+
$p50 = $sampleCount ? $samples[(int) floor($sampleCount * 0.5)] : 0.0;
221+
$p95 = $sampleCount ? $samples[(int) floor($sampleCount * 0.95)] : 0.0;
222+
$p99 = $sampleCount ? $samples[(int) floor($sampleCount * 0.99)] : 0.0;
215223

216224
echo "\nResults:\n";
217225
echo "========\n";
@@ -229,10 +237,16 @@
229237
// Performance goals
230238
echo "\nPerformance Goals:\n";
231239
echo "==================\n";
232-
echo sprintf("Throughput goal: 250k+ req/s... %s\n",
233-
$throughput >= 250000 ? "✓ PASS" : "✗ FAIL");
234-
echo sprintf("p50 latency goal: <1ms... %s\n",
235-
$p50 < 1.0 ? "✓ PASS" : "✗ FAIL");
236-
echo sprintf("p99 latency goal: <5ms... %s\n",
237-
$p99 < 5.0 ? "✓ PASS" : "✗ FAIL");
240+
echo sprintf(
241+
"Throughput goal: 250k+ req/s... %s\n",
242+
$throughput >= 250000 ? '✓ PASS' : '✗ FAIL'
243+
);
244+
echo sprintf(
245+
"p50 latency goal: <1ms... %s\n",
246+
$p50 < 1.0 ? '✓ PASS' : '✗ FAIL'
247+
);
248+
echo sprintf(
249+
"p99 latency goal: <5ms... %s\n",
250+
$p99 < 5.0 ? '✓ PASS' : '✗ FAIL'
251+
);
238252
});

benchmarks/tcp-backend.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
$envInt = static function (string $key, int $default): int {
44
$value = getenv($key);
5-
return $value === false ? $default : (int)$value;
5+
6+
return $value === false ? $default : (int) $value;
67
};
78

89
$host = getenv('BACKEND_HOST') ?: '127.0.0.1';

benchmarks/tcp.php

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,21 @@
2424

2525
$envInt = static function (string $key, int $default): int {
2626
$value = getenv($key);
27-
return $value === false ? $default : (int)$value;
27+
28+
return $value === false ? $default : (int) $value;
2829
};
2930
$envFloat = static function (string $key, float $default): float {
3031
$value = getenv($key);
31-
return $value === false ? $default : (float)$value;
32+
33+
return $value === false ? $default : (float) $value;
3234
};
3335
$envBool = static function (string $key, bool $default): bool {
3436
$value = getenv($key);
3537
if ($value === false) {
3638
return $default;
3739
}
3840
$parsed = filter_var($value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
41+
3942
return $parsed ?? $default;
4043
};
4144

@@ -61,26 +64,28 @@
6164
$connections = max(300000, $concurrent * 100);
6265
if ($payloadBytes > 0) {
6366
$connections = max(100000, $concurrent * 20);
64-
$maxByTarget = (int)floor($targetBytes / max(1, $payloadBytes));
67+
$maxByTarget = (int) floor($targetBytes / max(1, $payloadBytes));
6568
if ($maxByTarget > 0) {
6669
$connections = min($connections, $maxByTarget);
6770
}
6871
}
6972
} else {
70-
$connections = (int)$connectionsEnv;
73+
$connections = (int) $connectionsEnv;
7174
}
7275
$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))));
7477

7578
if ($connections < 1) {
7679
echo "Invalid connection count.\n";
80+
7781
return;
7882
}
7983
if ($concurrent > $connections) {
8084
$concurrent = $connections;
8185
}
8286
if ($concurrent < 1) {
8387
echo "Invalid concurrency.\n";
88+
8489
return;
8590
}
8691

@@ -130,10 +135,11 @@
130135
}
131136

132137
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+
}
137143

138144
echo "Mode: persistent\n";
139145
if ($streamBytes > 0) {
@@ -162,7 +168,6 @@
162168
Coroutine::create(function () use (
163169
$host,
164170
$port,
165-
$protocol,
166171
$timeout,
167172
$payloadBytes,
168173
$payloadDataBytes,
@@ -181,13 +186,14 @@
181186
$client = new Client(SWOOLE_SOCK_TCP);
182187
$client->set(['timeout' => $timeout]);
183188

184-
if (!$client->connect($host, $port, $timeout)) {
189+
if (! $client->connect($host, $port, $timeout)) {
185190
$errors++;
186191
$channel->push([
187192
'bytes' => 0,
188193
'ops' => 0,
189194
'errors' => $errors,
190195
]);
196+
191197
return;
192198
}
193199

@@ -199,6 +205,7 @@
199205
'ops' => 0,
200206
'errors' => $errors,
201207
]);
208+
202209
return;
203210
}
204211

@@ -211,6 +218,7 @@
211218
'ops' => 0,
212219
'errors' => $errors,
213220
]);
221+
214222
return;
215223
}
216224

@@ -327,7 +335,6 @@
327335
$host,
328336
$port,
329337
$workerConnections,
330-
$protocol,
331338
$timeout,
332339
$payloadBytes,
333340
$payloadDataBytes,
@@ -356,6 +363,7 @@
356363
'bytes' => 0,
357364
'samples' => [],
358365
]);
366+
359367
return;
360368
}
361369

@@ -367,7 +375,7 @@
367375
'timeout' => $timeout,
368376
]);
369377

370-
if (!$client->connect($host, $port, $timeout)) {
378+
if (! $client->connect($host, $port, $timeout)) {
371379
$errors++;
372380
$latency = (microtime(true) - $connStart) * 1000;
373381
$count++;
@@ -381,6 +389,7 @@
381389
if (($count % $sampleEvery) === 0) {
382390
$samples[] = $latency;
383391
}
392+
384393
continue;
385394
}
386395

@@ -469,7 +478,7 @@
469478
$max = $result['max'];
470479
}
471480
}
472-
if (!empty($result['samples'])) {
481+
if (! empty($result['samples'])) {
473482
$samples = array_merge($samples, $result['samples']);
474483
}
475484
}
@@ -479,6 +488,7 @@
479488
// Calculate statistics
480489
if ($totalCount === 0) {
481490
echo "No connections completed.\n";
491+
482492
return;
483493
}
484494

@@ -487,9 +497,9 @@
487497

488498
sort($samples);
489499
$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;
493503
$throughputGb = $bytes > 0 ? ($bytes / $totalTime / 1024 / 1024 / 1024) : 0.0;
494504

495505
echo "\nResults:\n";
@@ -511,8 +521,12 @@
511521
// Performance goals
512522
echo "\nPerformance Goals:\n";
513523
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+
);
518532
});

0 commit comments

Comments
 (0)