# SPDX-License-Identifier: Apache-2.0 """Served-latency benchmark: POST /predict to a running server, one hand per request, one keep-alive connection. python code/scripts/bench_http.py --url http://127.0.0.1:20000 --media media --n 1000 --warmup 20 --json out.json Sends the same request ``--n`` times (the full JPEG frame as base64 plus one hand box; by default ``sample1.jpg`` and its right-hand box from ``media/bboxes.json``) after ``--warmup`` untimed requests, and reports p50 / p99 of the client round trip on loopback and of the server's ``timing_ms`` fields (``preprocess``, ``device`` = the model call, ``mano``, ``total``). ``--cycle`` cycles through all 8 hands of the 4 sample images instead. Standard library only. """ from __future__ import annotations import argparse import base64 import http.client import json import os import statistics import time import urllib.parse from pathlib import Path def pct(v, q): s = sorted(v) return s[min(len(s) - 1, max(0, int(round(q / 100.0 * (len(s) - 1)))))] def summ(v): return {"n": len(v), "p50": statistics.median(v), "p99": pct(v, 99), "mean": statistics.fmean(v), "min": min(v), "max": max(v)} def main(): ap = argparse.ArgumentParser() ap.add_argument("--url", default="http://127.0.0.1:20000") ap.add_argument("--media", type=Path, required=True) ap.add_argument("--image", default="sample1.jpg") ap.add_argument("--hand", type=int, default=0, help="index of the box in bboxes.json[image]") ap.add_argument("--cycle", action="store_true", help="cycle through every hand of every sample image") ap.add_argument("--n", type=int, default=1000) ap.add_argument("--warmup", type=int, default=20) ap.add_argument("--json", default=None) a = ap.parse_args() boxes = json.loads((a.media / "bboxes.json").read_text()) reqs = [] names = sorted(boxes) if a.cycle else [a.image] for name in names: img = base64.b64encode((a.media / name).read_bytes()).decode() ents = boxes[name] if a.cycle else [boxes[name][a.hand]] for e in ents: reqs.append((name, json.dumps({"image": img, "bbox": e["bbox"], "is_right": bool(e.get("is_right", 1))}).encode())) u = urllib.parse.urlparse(a.url) conn = http.client.HTTPConnection(u.hostname, u.port or 80, timeout=120) hdr = {"Content-Type": "application/json"} def post(body): t0 = time.perf_counter() conn.request("POST", "/predict", body=body, headers=hdr) r = conn.getresponse() raw = r.read() dt = (time.perf_counter() - t0) * 1e3 if r.status != 200: raise SystemExit(f"HTTP {r.status}: {raw[:300]!r}") return dt, json.loads(raw) for i in range(a.warmup): post(reqs[i % len(reqs)][1]) rtt, tm = [], {} load0 = os.getloadavg() for i in range(a.n): dt, out = post(reqs[i % len(reqs)][1]) rtt.append(dt) for k, v in out.get("timing_ms", {}).items(): tm.setdefault(k, []).append(float(v)) load1 = os.getloadavg() res = {"url": a.url, "images": names, "requests": len(reqs), "n": a.n, "warmup": a.warmup, "body_bytes": [len(b) for _, b in reqs], "client_rtt_ms": summ(rtt), "server_timing_ms": {k: summ(v) for k, v in tm.items()}, "loadavg_start": load0, "loadavg_end": load1, "last_response": {k: v for k, v in out.items() if k not in ("vertices", "faces")}} print(f"client RTT p50 {res['client_rtt_ms']['p50']:.3f} ms p99 {res['client_rtt_ms']['p99']:.3f} ms (n={a.n}); " + "; ".join(f"{k} p50 {s['p50']:.3f} p99 {s['p99']:.3f}" for k, s in res["server_timing_ms"].items())) print("RESULT " + json.dumps({"client_rtt_ms": res["client_rtt_ms"], "server_timing_ms": res["server_timing_ms"]})) if a.json: Path(a.json).write_text(json.dumps(res, indent=1)) if __name__ == "__main__": main()