Repository navigation
Expand file tree
/
Copy path_stat.py
More file actions
executable file
·312 lines (279 loc) · 11.3 KB
/
Copy path_stat.py
File metadata and controls
executable file
·312 lines (279 loc) · 11.3 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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
#!/usr/bin/env python3
"""Aggregate multiple wrk2 ``--latency`` runs into a single
stable/unstable datapoint.
Usage:
_stat.py <out.json> <run1.txt> <run2.txt> ...
Reads each runN.txt (raw wrk2 stdout) and parses out:
- Requests/sec (server-bottlenecked, since the harness drives
wrk2 in saturate mode with a very high ``-R``).
- Latency Distribution percentiles: 50, 75, 90, 99, 99.9,
99.99, 99.999.
- Timeouts.
- Socket errors.
Writes a JSON blob with runs[], median of middle N-2 runs (req/s),
median of all N runs (each percentile), mean req/s, req/s absolute
stdev, req/s stdev_pct, per-percentile sample stdev (ms) over all
N runs, and ``stable`` (True iff req/s stdev_pct < 3.0). The
per-percentile stdev pairs with each ``median_pXY_ms`` so
downstream consumers can quote "p99 = 2.66 ± 0.04 ms" directly
without re-deriving variance from the raw runs.
wrk2 ``--latency`` block looks like::
Latency Distribution (HdrHistogram - Recorded Latency)
50.000% 1.23ms
75.000% 1.45ms
90.000% 1.78ms
99.000% 3.10ms
99.900% 5.21ms
99.990% 8.93ms
99.999% 12.40ms
100.000% 18.20ms
Match against the percentage prefix and capture the value + unit
on the same line.
"""
import json
import os
import re
import statistics
import sys
from pathlib import Path
# wrk2 --latency: ``50.000% 1.23ms`` (note the trailing %.000).
LATENCY_RE = re.compile(
r"^\s*([0-9]+(?:\.[0-9]+)?)%\s+([0-9]+(?:\.[0-9]+)?)(us|ms|s|m)\s*$",
re.MULTILINE,
)
RPS_RE = re.compile(r"^\s*Requests/sec:\s+([0-9.]+)", re.MULTILINE)
TIMEOUT_RE = re.compile(r"Socket errors:.*timeout\s+([0-9]+)")
ERR_RE = re.compile(
r"Socket errors:\s+connect\s+([0-9]+),\s+read\s+([0-9]+),"
r"\s+write\s+([0-9]+)"
)
def _ms(val: float, unit: str | None) -> float:
"""Normalise a latency value to milliseconds."""
u = (unit or "ms").lower()
if u == "us":
return val / 1000.0
if u == "s":
return val * 1000.0
if u == "m":
return val * 60_000.0
return val
# Map nominal percentile -> output key. wrk2 prints them with 3
# decimals (50.000%, 99.900%, etc.); match within a tight epsilon.
_PCT_KEYS: list[tuple[float, str]] = [
(50.0, "p50_ms"),
(75.0, "p75_ms"),
(90.0, "p90_ms"),
(99.0, "p99_ms"),
(99.9, "p99_9_ms"),
(99.99, "p99_99_ms"),
(99.999, "p99_999_ms"),
]
def _parse(text: str) -> dict:
"""Parse one wrk2 stdout blob into a structured dict."""
rps = 0.0
m = RPS_RE.search(text)
if m:
rps = float(m.group(1))
out = {key: 0.0 for _, key in _PCT_KEYS}
# wrk2 also prints a header section with ``Latency`` /
# ``Req/Sec`` summary stats (in the ``Thread Stats`` block).
# The percentile block we want is under
# ``Latency Distribution (HdrHistogram - Recorded Latency)``;
# wrk's older ``Latency Distribution`` block has the same
# shape so the regex catches both.
in_dist = False
for line in text.splitlines():
if "Latency Distribution" in line:
in_dist = True
continue
if not in_dist:
continue
s = line.strip()
if not s or s.startswith("Detailed"):
in_dist = False
continue
m = LATENCY_RE.match(" " + s) # add leading whitespace for the regex
if not m:
# Could be a non-percentile line like
# ``HdrHistogram of 12345 entries`` or a blank.
continue
pct = float(m.group(1))
val = float(m.group(2))
unit = m.group(3)
ms_val = _ms(val, unit)
for nominal, key in _PCT_KEYS:
if abs(pct - nominal) < 0.001:
out[key] = ms_val
break
timeouts = 0
tm = TIMEOUT_RE.search(text)
if tm:
timeouts = int(tm.group(1))
socket_errors = 0
em = ERR_RE.search(text)
if em:
socket_errors = int(em.group(1)) + int(em.group(2)) + int(em.group(3))
return {
"req_per_sec": rps,
"p50_ms": out["p50_ms"],
"p75_ms": out["p75_ms"],
"p90_ms": out["p90_ms"],
"p99_ms": out["p99_ms"],
"p99_9_ms": out["p99_9_ms"],
"p99_99_ms": out["p99_99_ms"],
"p99_999_ms": out["p99_999_ms"],
"timeouts": timeouts,
"socket_errors": socket_errors,
}
def main(argv: list[str]) -> int:
# Optional --peak-rps <value> overrides the
# median_req_per_sec calculation. The harness's two-phase
# design measures peak capacity in a separate find-peak
# run; the 5 measurement runs are at a fixed sub-peak rate
# specifically to stress the latency tail. ``Requests/sec``
# values inside those measurement runs all equal the fixed
# rate, so a "median req/s" computed over them is
# uninformative. Pass the peak in so the summary headlines
# the capacity number.
peak_rps_override: float | None = None
args: list[str] = []
i = 1
while i < len(argv):
if argv[i] == "--peak-rps" and i + 1 < len(argv):
peak_rps_override = float(argv[i + 1])
i += 2
continue
args.append(argv[i])
i += 1
if len(args) < 2:
print(
"usage: _stat.py [--peak-rps R] <out.json> <run1.txt> [run2.txt ...]",
file=sys.stderr,
)
return 2
out = Path(args[0])
run_paths = [Path(p) for p in args[1:]]
runs: list[dict] = []
for p in run_paths:
text = p.read_text(errors="replace")
r = _parse(text)
r["run"] = len(runs) + 1
runs.append(r)
rps_values = [r["req_per_sec"] for r in runs if r["req_per_sec"] > 0]
def _median_of(field: str) -> float:
vals = sorted(r[field] for r in runs if r[field] > 0)
return statistics.median(vals) if vals else 0.0
def _stdev_of(field: str) -> float:
# Sample stdev (Bessel-corrected) across the N runs for one
# percentile. Drops zero-valued samples (parse miss) but
# keeps the surviving set; requires >=2 valid samples or
# returns 0.0 so the summary doesn't synthesise variance
# from a single observation.
vals = [r[field] for r in runs if r[field] > 0]
return statistics.stdev(vals) if len(vals) >= 2 else 0.0
if len(rps_values) < 3:
summary = {
"median_req_per_sec": 0.0,
"mean_req_per_sec": 0.0,
"stdev_req_per_sec": 0.0,
"stdev_pct": 100.0,
"median_p50_ms": 0.0,
"median_p99_ms": 0.0,
"median_p99_9_ms": 0.0,
"median_p99_99_ms": 0.0,
"median_p99_999_ms": 0.0,
"stdev_p50_ms": 0.0,
"stdev_p99_ms": 0.0,
"stdev_p99_9_ms": 0.0,
"stdev_p99_99_ms": 0.0,
"stdev_p99_999_ms": 0.0,
"total_timeouts": sum(r["timeouts"] for r in runs),
"total_socket_errors": sum(r["socket_errors"] for r in runs),
"stable": False,
"note": "too few valid runs for stats",
}
else:
sorted_rps = sorted(rps_values)
# Drop min + max, median of the middle 3.
trimmed = sorted_rps[1:-1] if len(sorted_rps) >= 3 else sorted_rps
median_rps = statistics.median(trimmed)
mean_rps = statistics.mean(rps_values)
# Sample stdev (Bessel-corrected) over the full N
# measurement runs -- this is the "correct statistics"
# number for the 5-run throughput: it includes both
# endpoints rather than the trimmed middle three, so the
# variance reflects the full sample, not the headline.
stdev = statistics.stdev(rps_values) if len(rps_values) >= 2 else 0.0
stdev_pct = (stdev / mean_rps * 100.0) if mean_rps > 0 else 100.0
# Bimodality gate: the throughput sigma can stay tight while
# whole rounds stall (a pinned worker's standing queue turns
# into hundreds-of-ms percentiles under wrk2's CO correction).
# A round-to-round p99 max/min ratio above BIMODAL_MAX means a
# subset of rounds cliffed even though req/s held, so the run
# must not be published as ``stable``. Threshold is env-tunable
# (FLARE_BENCH_BIMODAL_MAX, default 10.0).
_p99_vals = [r["p99_ms"] for r in runs if r["p99_ms"] > 0]
bimodal_ratio = (
max(_p99_vals) / min(_p99_vals)
if len(_p99_vals) >= 2 and min(_p99_vals) > 0
else 1.0
)
bimodal_max = float(os.environ.get("FLARE_BENCH_BIMODAL_MAX", "10.0"))
bimodal_ok = bimodal_ratio <= bimodal_max
# Headline req/s is peak capacity (find-peak phase) when
# the harness supplied it; otherwise fall back to the
# median over measurement runs.
headline_rps = (
peak_rps_override if peak_rps_override is not None else median_rps
)
summary = {
"median_req_per_sec": headline_rps,
"peak_req_per_sec": peak_rps_override or 0.0,
"sustain_req_per_sec": median_rps,
"mean_req_per_sec": mean_rps,
"stdev_req_per_sec": stdev,
"stdev_pct": stdev_pct,
"median_p50_ms": _median_of("p50_ms"),
"median_p99_ms": _median_of("p99_ms"),
"median_p99_9_ms": _median_of("p99_9_ms"),
"median_p99_99_ms": _median_of("p99_99_ms"),
"median_p99_999_ms": _median_of("p99_999_ms"),
# Per-percentile sample stdev across the 5 measurement
# runs (ms). Pairs with each median_pXY_ms above so
# consumers can quote "p99 = 2.66 ± 0.04 ms" directly
# without re-deriving variance from the raw runs.
"stdev_p50_ms": _stdev_of("p50_ms"),
"stdev_p99_ms": _stdev_of("p99_ms"),
"stdev_p99_9_ms": _stdev_of("p99_9_ms"),
"stdev_p99_99_ms": _stdev_of("p99_99_ms"),
"stdev_p99_999_ms": _stdev_of("p99_999_ms"),
"total_timeouts": sum(r["timeouts"] for r in runs),
"total_socket_errors": sum(r["socket_errors"] for r in runs),
"p99_bimodality_ratio": bimodal_ratio,
# A run is stable only when BOTH the throughput sigma is
# tight AND the per-round p99 did not cliff (no bimodality).
"stable": stdev_pct < 3.0 and bimodal_ok,
}
if not bimodal_ok:
summary["note"] = (
"bimodal p99: round max/min ratio %.1fx exceeds %.1fx"
" (throughput sigma was tight but some rounds cliffed;"
" pin server + loadgen to disjoint cores or set"
" FLARE_BENCH_PIN=0 and rerun)"
% (bimodal_ratio, bimodal_max)
)
payload = {"runs": runs, "summary": summary}
out.write_text(json.dumps(payload, indent=2) + "\n")
print(
f" median={summary['median_req_per_sec']:,.0f} req/s "
f"σ={summary.get('stdev_req_per_sec', 0.0):,.0f} req/s "
f"({summary['stdev_pct']:.2f}%) "
f"p50={summary['median_p50_ms']:.2f}±{summary.get('stdev_p50_ms', 0.0):.2f}ms "
f"p99={summary['median_p99_ms']:.2f}±{summary.get('stdev_p99_ms', 0.0):.2f}ms "
f"p99.9={summary['median_p99_9_ms']:.2f}±{summary.get('stdev_p99_9_ms', 0.0):.2f}ms "
f"p99.99={summary['median_p99_99_ms']:.2f}±{summary.get('stdev_p99_99_ms', 0.0):.2f}ms "
f"stable={summary['stable']}"
)
return 0
if __name__ == "__main__":
sys.exit(main(sys.argv))