"""semif-serve latency, 2026-09-27. Sequential client on nh3-dev, fresh connection per request, 3 runs x 20 timed after 3 warm-ups per condition, conditions interleaved per run. e2e = wall time around the request; srv = what SemIf reports (direct: total_seconds; shared: timing.total_seconds), i.e. no network or HTTP. SEMIF_URL=... SEMIF_TOKEN=... uv run --with httpx python latency.py """ import os, statistics as st, time, httpx, itertools U, H = os.environ["SEMIF_URL"], {"Authorization": f"Bearer {os.environ['SEMIF_TOKEN']}"} OPTS = [{"id": "casual_outing", "description": "A casual outing"}, {"id": "romantic_date", "description": "A romantic date"}, {"id": "booty_call", "description": "A booty call"}] CALL = "She calls up and says, hey, what're you doing right now? It's 2AM and I'm bored." LONG = "The service logged a routine heartbeat from node alpha at the scheduled interval without incident. " * 120 Q = "What kind of invitation is this?" BIN = [{"id": "yes", "description": "Yes"}, {"id": "no", "description": "No"}] def rot(opts): return [opts[i:] + opts[:i] for i in range(len(opts))] CONDS = { "health (floor)": ("GET", "/health", None), "decide, short (~130 tok)": ("POST", "/decide", {"id": "a", "state": CALL, "question": Q, "options": OPTS}), "decide, long (~2,000 tok)": ("POST", "/decide", {"id": "b", "state": LONG, "question": "Was there an incident?", "options": BIN}), "shared, 3 rotations (short)": ("POST", "/decide/shared", {"state": CALL, "decisions": [ {"id": f"r{i}", "question": Q, "options": o} for i, o in enumerate(rot(OPTS))]}), "shared, 6 orderings (short)": ("POST", "/decide/shared", {"state": CALL, "decisions": [ {"id": f"p{i}", "question": Q, "options": list(o)} for i, o in enumerate(itertools.permutations(OPTS))]}), "shared, 3 rotations (long)": ("POST", "/decide/shared", {"state": LONG, "decisions": [ {"id": f"r{i}", "question": "Was there an incident?", "options": o} for i, o in enumerate(rot(BIN + [{"id": "unsure", "description": "Cannot tell"}]))]}), } def once(method, path, body): t = time.perf_counter() r = httpx.request(method, U + path, json=body, headers=H, timeout=120) r.raise_for_status() e2e = (time.perf_counter() - t) * 1000 j = r.json() srv = (j["timing"]["total_seconds"] if "timing" in j else j.get("total_seconds")) if path != "/health" else None return e2e, (srv * 1000 if srv is not None else None) res = {k: {"e2e": [], "srv": []} for k in CONDS} for run in range(3): for name, (m, p, b) in CONDS.items(): for _ in range(3): once(m, p, b) e, s = zip(*(once(m, p, b) for _ in range(20))) res[name]["e2e"].append(st.median(e)) if s[0] is not None: res[name]["srv"].append(st.median(s)) print(f"{'condition':<30} {'e2e p50 (runs)':<28} {'server p50 (runs)'}") for name, r in res.items(): fmt = lambda xs: f"{st.median(xs):6.1f} [{min(xs):.1f}-{max(xs):.1f}]" if xs else "-" print(f"{name:<30} {fmt(r['e2e']):<28} {fmt(r['srv'])}")