#!/usr/bin/env python3 """Split run-clients.sh's end-to-end latency into components with the server's log (events.jsonl, Unix ms, same clock as the clients). breakdown.py Per key: the POST that carried it (the first update POST on its document to reach the server after the key, less the one-way delay), the first stream event announcing that seq, the first ?since= fetch that returned it, and the moment the other copy showed it. Components, median / p95 ms: wait key -> POST leaves the sender (sender pacing and local work) post POST reaches server -> server broadcasts (server time) react broadcast -> the receiver's GET reaches the server (one way down, the receiver's reaction, one way up) get the GET's server time draw GET answered -> shown (one way down, apply, publish, render) """ import json, math, sys from pathlib import Path server, clients = Path(sys.argv[1]), Path(sys.argv[2]) conn = json.loads((server / "connection.json").read_text()) oneway = conn["rtt_ms"] / 2 events = [json.loads(l) for l in (server / "events.jsonl").open()] def pct(xs, p): s = sorted(xs) return s[max(0, math.ceil(p * len(s)) - 1)] if s else float("nan") combos = [("web -> web", "web-web.json", "web-web.json", 0), ("native -> native", "nn-types.json", "nn-watch.json", 1), ("native -> web", "nw-types.json", "nw-watch.json", 2), ("web -> native", "wn-types.json", "wn-watch.json", 3)] print(f"{'combination':18} {'total':>13} {'wait':>11} {'post':>11} {'react':>11} {'get':>9} {'draw':>11} POSTs GETs") out = {} for name, tf, wf, idx in combos: try: keys = json.loads((clients / tf).read_text())["keys"] seen = json.loads((clients / wf).read_text())["seen"] except (FileNotFoundError, KeyError): continue sqid = conn["sqids"][idx] posts = sorted((e for e in events if e["ev"] == "req" and e["method"] == "POST" and e["path"] == f"/d/{sqid}/updates"), key=lambda e: e["t"]) gets = sorted((e for e in events if e["ev"] == "req" and e["method"] == "GET" and e["path"].startswith(f"/d/{sqid}/updates?since")), key=lambda e: e["t"]) sses = sorted((e for e in events if e["ev"] == "sse" and e["path"].startswith(f"/d/{sqid}/stream")), key=lambda e: e["t"]) parts = {k: [] for k in ("total", "wait", "post", "react", "get", "draw")} t0, t1 = keys[0], keys[-1] + 3000 for row in seen: i, shown = int(row[0]), row[1] if i >= len(keys): continue k = keys[i] p = next((p for p in posts if p["t"] - oneway >= k - 1), None) if not p or not p.get("seq"): continue s = int(p["seq"]) a = next((e for e in sses if e["t"] >= p["t"] and e["seq"].isdigit() and int(e["seq"]) >= s), None) g = next((g for g in gets if g["t"] >= (a["t"] if a else p["t"]) and g.get("seq", "").isdigit() and int(g["seq"]) >= s), None) if not a or not g: continue parts["total"].append(shown - k) parts["wait"].append(p["t"] - oneway - k) parts["post"].append(a["t"] - p["t"]) parts["react"].append(g["t"] - a["t"]) parts["get"].append(g["end"] - g["t"]) parts["draw"].append(shown - g["end"]) npost = sum(1 for p in posts if t0 <= p["t"] <= t1) nget = sum(1 for g in gets if t0 <= g["t"] <= t1) f = lambda k: f"{pct(parts[k], .5):5.0f}/{pct(parts[k], .95):<5.0f}" print(f"{name:18} {f('total'):>13} {f('wait'):>11} {f('post'):>11} {f('react'):>11} {f('get'):>9} {f('draw'):>11} {npost:5} {nget:4}") out[name] = {k: [pct(v, .5), pct(v, .95)] for k, v in parts.items()} | {"posts": npost, "gets": nget, "n": len(parts["total"])} (clients / "breakdown.json").write_text(json.dumps(out, indent=1))