-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path20-rule-benchmarks.php
More file actions
275 lines (220 loc) · 7.88 KB
/
Copy path20-rule-benchmarks.php
File metadata and controls
275 lines (220 loc) · 7.88 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
<?php
/**
* Example 20: Firewall Rule Benchmarks
*
* Benchmarks the overhead of the Firewall::decide() loop with various
* rule configurations to catch performance regressions.
*
* Scenarios:
* - Baseline (no rules)
* - Allow2Ban (1 rule, 10 rules)
* - Fail2Ban (1 rule)
* - Throttle (1 rule)
* - Mixed (5 allow2ban + 5 fail2ban + 5 throttle)
*
* All benchmarks use InMemoryCache -- no external dependencies required.
*
* Run: php examples/20-rule-benchmarks.php
*/
declare(strict_types=1);
require __DIR__ . '/../vendor/autoload.php';
use Flowd\Phirewall\Config;
use Flowd\Phirewall\Http\Firewall;
use Flowd\Phirewall\Store\InMemoryCache;
use Nyholm\Psr7\ServerRequest;
echo "=== Firewall Rule Benchmarks ===\n\n";
// =============================================================================
// BENCHMARK HELPERS
// =============================================================================
$iterations = 10_000;
function benchmarkRule(string $name, Config $config, int $iterations): array
{
$firewall = new Firewall($config);
// Warm up with 100 iterations
for ($i = 0; $i < 100; ++$i) {
$ip = sprintf('172.16.%d.%d', $i >> 8, $i & 0xFF);
$request = new ServerRequest('GET', '/api/resource', [], null, '1.1', ['REMOTE_ADDR' => $ip]);
$firewall->decide($request);
}
// Re-create firewall + cache to start clean
$freshCache = new InMemoryCache();
// Rebuild config with a fresh cache for the actual run
$freshConfig = new Config($freshCache);
// Copy rule sections from original config
foreach ($config->allow2ban->rules() as $allow2BanRule) {
$freshConfig->allow2ban->addRule($allow2BanRule);
}
foreach ($config->fail2ban->rules() as $fail2BanRule) {
$freshConfig->fail2ban->addRule($fail2BanRule);
}
foreach ($config->throttles->rules() as $throttleRule) {
$freshConfig->throttles->addRule($throttleRule);
}
$firewall = new Firewall($freshConfig);
$startNs = hrtime(true);
for ($i = 0; $i < $iterations; ++$i) {
// Unique IP per iteration to avoid hitting thresholds
$ip = sprintf('10.0.%d.%d', $i >> 8, $i & 0xFF);
$request = new ServerRequest('GET', '/api/resource', [], null, '1.1', ['REMOTE_ADDR' => $ip]);
$firewall->decide($request);
}
$elapsedNs = hrtime(true) - $startNs;
$elapsedMs = $elapsedNs / 1_000_000;
$elapsedSec = $elapsedNs / 1_000_000_000;
$opsPerSec = $iterations / max(1e-9, $elapsedSec);
$avgMicros = ($elapsedNs / $iterations) / 1_000;
return [
'name' => $name,
'iterations' => $iterations,
'elapsed_ms' => $elapsedMs,
'ops_per_sec' => $opsPerSec,
'avg_us' => $avgMicros,
];
}
function formatRuleResult(array $result): string
{
return sprintf(
"%-35s: %8.1f ms, %9.0f ops/sec, %7.2f us/op",
$result['name'],
$result['elapsed_ms'],
$result['ops_per_sec'],
$result['avg_us']
);
}
$ipExtractor = fn($req): string => $req->getServerParams()['REMOTE_ADDR'];
$alwaysMatchFilter = fn($req): bool => true;
$neverMatchFilter = fn($req): bool => false;
$results = [];
// =============================================================================
// 1) BASELINE: No rules configured
// =============================================================================
echo "--- Baseline (no rules) ---\n";
$config = new Config(new InMemoryCache());
$result = benchmarkRule('Baseline (no rules)', $config, $iterations);
echo formatRuleResult($result) . "\n\n";
$results[] = $result;
// =============================================================================
// 2) ALLOW2BAN: 1 rule
// =============================================================================
echo "--- Allow2Ban (1 rule) ---\n";
$config = new Config(new InMemoryCache());
$config->allow2ban->add(
name: 'a2b-bench',
threshold: 100_000, // High threshold to avoid bans during benchmark
period: 3600,
banSeconds: 3600,
key: $ipExtractor,
);
$result = benchmarkRule('Allow2Ban (1 rule)', $config, $iterations);
echo formatRuleResult($result) . "\n\n";
$results[] = $result;
// =============================================================================
// 3) ALLOW2BAN: 10 rules
// =============================================================================
echo "--- Allow2Ban (10 rules) ---\n";
$config = new Config(new InMemoryCache());
for ($r = 0; $r < 10; ++$r) {
$config->allow2ban->add(
name: 'a2b-bench-' . $r,
threshold: 100_000,
period: 3600,
banSeconds: 3600,
key: $ipExtractor,
);
}
$result = benchmarkRule('Allow2Ban (10 rules)', $config, $iterations);
echo formatRuleResult($result) . "\n\n";
$results[] = $result;
// =============================================================================
// 4) FAIL2BAN: 1 rule (filter never matches, so only ban-key check runs)
// =============================================================================
echo "--- Fail2Ban (1 rule) ---\n";
$config = new Config(new InMemoryCache());
$config->fail2ban->add(
name: 'f2b-bench',
threshold: 100_000,
period: 3600,
ban: 3600,
filter: $neverMatchFilter, // Filter never matches: measures ban-key lookup overhead
key: $ipExtractor,
);
$result = benchmarkRule('Fail2Ban (1 rule)', $config, $iterations);
echo formatRuleResult($result) . "\n\n";
$results[] = $result;
// =============================================================================
// 5) THROTTLE: 1 rule
// =============================================================================
echo "--- Throttle (1 rule) ---\n";
$config = new Config(new InMemoryCache());
$config->throttles->add(
name: 'throttle-bench',
limit: 100_000, // High limit to avoid throttling during benchmark
period: 3600,
key: $ipExtractor,
);
$result = benchmarkRule('Throttle (1 rule)', $config, $iterations);
echo formatRuleResult($result) . "\n\n";
$results[] = $result;
// =============================================================================
// 6) MIXED: 5 allow2ban + 5 fail2ban + 5 throttle
// =============================================================================
echo "--- Mixed (5 a2b + 5 f2b + 5 throttle) ---\n";
$config = new Config(new InMemoryCache());
for ($r = 0; $r < 5; ++$r) {
$config->allow2ban->add(
name: 'a2b-mixed-' . $r,
threshold: 100_000,
period: 3600,
banSeconds: 3600,
key: $ipExtractor,
);
}
for ($r = 0; $r < 5; ++$r) {
$config->fail2ban->add(
name: 'f2b-mixed-' . $r,
threshold: 100_000,
period: 3600,
ban: 3600,
filter: $neverMatchFilter,
key: $ipExtractor,
);
}
for ($r = 0; $r < 5; ++$r) {
$config->throttles->add(
name: 'throttle-mixed-' . $r,
limit: 100_000,
period: 3600,
key: $ipExtractor,
);
}
$result = benchmarkRule('Mixed (5+5+5 rules)', $config, $iterations);
echo formatRuleResult($result) . "\n\n";
$results[] = $result;
// =============================================================================
// SUMMARY
// =============================================================================
echo "=== Summary ===\n\n";
echo sprintf("%-35s %10s %10s %10s\n", 'Scenario', 'ops/sec', 'avg us/op', 'elapsed ms');
echo str_repeat('-', 75) . "\n";
foreach ($results as $result) {
echo sprintf(
"%-35s %10s %10.2f %10.1f\n",
$result['name'],
number_format($result['ops_per_sec']),
$result['avg_us'],
$result['elapsed_ms']
);
}
echo "\n";
// Slowdown factor vs baseline
$baselineOps = $results[0]['ops_per_sec'];
echo "Overhead vs baseline:\n";
for ($i = 1, $iMax = count($results); $i < $iMax; ++$i) {
$factor = $baselineOps / max(1, $results[$i]['ops_per_sec']);
echo sprintf(
" %-33s %.2fx slower\n",
$results[$i]['name'],
$factor
);
}
echo "\n=== Benchmark Complete ===\n";