#!/usr/bin/env python3 """Offline: summarize cp_replay result files (experiments only). summarize.py DIR -- reads DIR/{base,new}-{proc,grpc}-N.json and prints, per mode and history size, the warm-turn p50 and p95 per metric and per stage (min over repeats of each repeat's percentile: the machine is shared). """ import glob, json, os, re, statistics, sys def pct(values, q): v = sorted(values) return v[round(q * (len(v) - 1))] if v else float("nan") def load(directory): runs = {} for path in sorted(glob.glob(os.path.join(directory, "*.json"))): m = re.match(r"(base|new)-(proc|grpc)-(\d+)\.json", os.path.basename(path)) if not m: continue doc = json.load(open(path)) runs.setdefault((m.group(2), m.group(1)), []).append(doc) return runs def per_size(docs, metric, stage=None): """size -> list over repeats of (cold, p50, p95)""" out = {} for doc in docs: for res in doc["results"]: rows = res["rows"] def val(r): if stage is None: return r[metric] return r["stages"].get(stage, {}).get("ms", 0.0) warm = [val(r) for r in rows[1:]] out.setdefault(res["size"], []).append((val(rows[0]), pct(warm, 0.5), pct(warm, 0.95))) return out def best(entries, k): return min(e[k] for e in entries) def main(): runs = load(sys.argv[1]) metrics = ["pre_engine_ms", "first_event_ms", "tail_ms"] stages = ["http_body_json", "request_decode", "request_validate", "render", "tokenize", "worker_prompt_text", "engine_start", "post_decode", "post_assemble", "post_store", "delta_retokenize"] for mode in ("proc", "grpc"): if (mode, "base") not in runs: continue print(f"\n### {'in-process instant engine' if mode == 'proc' else 'instant engine behind the real gRPC runtime server'}") sizes = sorted(per_size(runs[(mode, "new")], "pre_engine_ms")) for label, names, stage in (("metric", metrics, False), ("stage", stages, True)): print(f"\n| {label} (ms, warm turns) | " + " | ".join(f"{s//1000}k base p50 / p95 | {s//1000}k new p50 / p95" for s in sizes) + " |") print("|---|" + "---|---|" * len(sizes)) for name in names: cells = [] for s in sizes: for cfg in ("base", "new"): e = per_size(runs[(mode, cfg)], name if not stage else None, name if stage else None).get(s) cells.append(f"{best(e,1):.2f} / {best(e,2):.2f}" if e else "-") print(f"| {name} | " + " | ".join(cells) + " |") print("\ncold first request (ms, min over repeats):") for cfg in ("base", "new"): e = per_size(runs[(mode, cfg)], "pre_engine_ms") print(f" {cfg:5s} pre_engine cold: " + ", ".join(f"{s//1000}k={best(e[s],0):.1f}" for s in sorted(e))) main()