#!/usr/bin/env python3 """Join run-clients.sh's typing and watching sides by key index: per key, the time from keydown on one copy to the "Q" showing on the other (the browser: DOM updated; native: rendered and reconciled into the text storage). Prints median / p95 per combination and writes latency.json.""" import json, math, sys from pathlib import Path out = Path(sys.argv[1]) def pct(xs, p): s = sorted(xs) return s[max(0, math.ceil(p * len(s)) - 1)] if s else float("nan") def load(name): path = out / name return json.loads(path.read_text()) if path.exists() else {} pairs = { "web -> web": (load("web-web.json").get("keys"), load("web-web.json").get("seen")), "native -> native": (load("nn-types.json").get("keys"), load("nn-watch.json").get("seen")), "native -> web": (load("nw-types.json").get("keys"), load("nw-watch.json").get("seen")), "web -> native": (load("wn-types.json").get("keys"), load("wn-watch.json").get("seen")), } result = {} for name, (keys, seen) in pairs.items(): if not keys or not seen: print(f"{name:18} missing") continue lat = [s[1] - keys[int(s[0])] for s in seen if 0 <= int(s[0]) < len(keys)] result[name] = {"n": len(lat), "keys": len(keys), "p50": pct(lat, 0.5), "p95": pct(lat, 0.95), "max": max(lat)} print(f"{name:18} keys {len(keys):4} seen {len(lat):4} p50 {pct(lat, .5):7.1f} ms p95 {pct(lat, .95):7.1f} ms max {max(lat):7.1f} ms") (out / "latency.json").write_text(json.dumps(result, indent=1))