| 1 | #!/usr/bin/env python3 |
| 2 | """Split run-clients.sh's end-to-end latency into components with the |
| 3 | server's log (events.jsonl, Unix ms, same clock as the clients). |
| 4 | |
| 5 | breakdown.py <server out-dir> <clients out-dir> |
| 6 | |
| 7 | Per key: the POST that carried it (the first update POST on its document to |
| 8 | reach the server after the key, less the one-way delay), the first stream |
| 9 | event announcing that seq, the first ?since= fetch that returned it, and the |
| 10 | moment the other copy showed it. Components, median / p95 ms: |
| 11 | wait key -> POST leaves the sender (sender pacing and local work) |
| 12 | post POST reaches server -> server broadcasts (server time) |
| 13 | react broadcast -> the receiver's GET reaches the server (one way down, |
| 14 | the receiver's reaction, one way up) |
| 15 | get the GET's server time |
| 16 | draw GET answered -> shown (one way down, apply, publish, render) |
| 17 | """ |
| 18 | import json, math, sys |
| 19 | from pathlib import Path |
| 20 | |
| 21 | server, clients = Path(sys.argv[1]), Path(sys.argv[2]) |
| 22 | conn = json.loads((server / "connection.json").read_text()) |
| 23 | oneway = conn["rtt_ms"] / 2 |
| 24 | events = [json.loads(l) for l in (server / "events.jsonl").open()] |
| 25 | |
| 26 | def pct(xs, p): |
| 27 | s = sorted(xs) |
| 28 | return s[max(0, math.ceil(p * len(s)) - 1)] if s else float("nan") |
| 29 | |
| 30 | combos = [("web -> web", "web-web.json", "web-web.json", 0), ("native -> native", "nn-types.json", "nn-watch.json", 1), |
| 31 | ("native -> web", "nw-types.json", "nw-watch.json", 2), ("web -> native", "wn-types.json", "wn-watch.json", 3)] |
| 32 | print(f"{'combination':18} {'total':>13} {'wait':>11} {'post':>11} {'react':>11} {'get':>9} {'draw':>11} POSTs GETs") |
| 33 | out = {} |
| 34 | for name, tf, wf, idx in combos: |
| 35 | try: |
| 36 | keys = json.loads((clients / tf).read_text())["keys"] |
| 37 | seen = json.loads((clients / wf).read_text())["seen"] |
| 38 | except (FileNotFoundError, KeyError): |
| 39 | continue |
| 40 | sqid = conn["sqids"][idx] |
| 41 | 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"]) |
| 42 | 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"]) |
| 43 | sses = sorted((e for e in events if e["ev"] == "sse" and e["path"].startswith(f"/d/{sqid}/stream")), key=lambda e: e["t"]) |
| 44 | parts = {k: [] for k in ("total", "wait", "post", "react", "get", "draw")} |
| 45 | t0, t1 = keys[0], keys[-1] + 3000 |
| 46 | for row in seen: |
| 47 | i, shown = int(row[0]), row[1] |
| 48 | if i >= len(keys): |
| 49 | continue |
| 50 | k = keys[i] |
| 51 | p = next((p for p in posts if p["t"] - oneway >= k - 1), None) |
| 52 | if not p or not p.get("seq"): |
| 53 | continue |
| 54 | s = int(p["seq"]) |
| 55 | a = next((e for e in sses if e["t"] >= p["t"] and e["seq"].isdigit() and int(e["seq"]) >= s), None) |
| 56 | 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) |
| 57 | if not a or not g: |
| 58 | continue |
| 59 | parts["total"].append(shown - k) |
| 60 | parts["wait"].append(p["t"] - oneway - k) |
| 61 | parts["post"].append(a["t"] - p["t"]) |
| 62 | parts["react"].append(g["t"] - a["t"]) |
| 63 | parts["get"].append(g["end"] - g["t"]) |
| 64 | parts["draw"].append(shown - g["end"]) |
| 65 | npost = sum(1 for p in posts if t0 <= p["t"] <= t1) |
| 66 | nget = sum(1 for g in gets if t0 <= g["t"] <= t1) |
| 67 | f = lambda k: f"{pct(parts[k], .5):5.0f}/{pct(parts[k], .95):<5.0f}" |
| 68 | 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}") |
| 69 | out[name] = {k: [pct(v, .5), pct(v, .95)] for k, v in parts.items()} | {"posts": npost, "gets": nget, "n": len(parts["total"])} |
| 70 | (clients / "breakdown.json").write_text(json.dumps(out, indent=1)) |
| 71 | |