#!/usr/bin/env python3 """Run the live-push latency probe: the real Docs server (optionally behind a loopback proxy that injects a round trip) and simulated typists/watchers. run-probe.py RTT=50 DOCS=1 [run ...] Each run is POLICY:TYPISTS[:MEAN_MS], e.g. web250:1 native250:1 idle0:1 idle50:5. Expects the Go test binary at $DOCS_LIVE_PROBE_GOTEST (built with `CGO_ENABLED=0 go test -c -o /tmp/live-push/docs.test ./internal/docs/`). One server serves every run (each run uses its own documents, so DOCS must cover the sum of TYPISTS). With SERVER_ONLY=1 it starts the server, prints connection.json and waits for /done (for the browser and native probes). """ import json, os, signal, subprocess, sys, time from pathlib import Path root = Path(__file__).resolve().parents[3] seed, updates, out = sys.argv[1], sys.argv[2], Path(sys.argv[3]) opts = dict(a.split("=", 1) for a in sys.argv[4:] if "=" in a) runs = [a for a in sys.argv[4:] if "=" not in a] gotest = os.environ.get("DOCS_LIVE_PROBE_GOTEST", "/tmp/live-push/docs.test") out.mkdir(parents=True, exist_ok=True) for f in ("connection.json", "done"): (out / f).unlink(missing_ok=True) env = dict(os.environ, DOCS_LIVE_PROBE_DIR=str(out), DOCS_LIVE_PROBE_SEED=seed, DOCS_LIVE_PROBE_ROLE="server", DOCS_LIVE_PROBE_RTT_MS=opts.get("RTT", "0"), DOCS_LIVE_PROBE_DOCS=opts.get("DOCS", "1"), TMPDIR="/tmp", DOCS_LIVE_PROBE_WARM=opts.get("WARM", "0"), DOCS_LIVE_PROBE_PUSH=opts.get("PUSHSRV", "0"), DOCS_LIVE_PROBE_DB=opts.get("DB", ""), DOCS_LIVE_PROBE_LATE_LIMIT=opts.get("LATE", "0"), DOCS_LIVE_PROBE_SEAL=opts.get("SEAL", "0")) log = (out / "server.log").open("w") extra = ["-test.cpuprofile", opts["CPUPROFILE"]] if "CPUPROFILE" in opts else [] server = subprocess.Popen([gotest, "-test.run", "^TestLivePushProbeServer$", "-test.count=1", "-test.timeout=70m"] + extra, cwd=root / "internal/docs", env=env, stdout=log, stderr=subprocess.STDOUT, start_new_session=True) try: deadline = time.monotonic() + 180 while not (out / "connection.json").exists(): if server.poll() is not None: raise SystemExit((out / "server.log").read_text()[-4000:]) if time.monotonic() > deadline: raise SystemExit("server did not start") time.sleep(0.05) if opts.get("SERVER_ONLY") == "1": print((out / "connection.json").read_text(), flush=True) while not (out / "done").exists(): time.sleep(0.2) offset = 0 conn = json.loads((out / "connection.json").read_text()) for n, run in enumerate(runs): parts = run.split(":") policy, typists = parts[0], int(parts[1]) mean = next((p for p in parts[2:] if p.isdigit()), opts.get("MEAN_MS", "150")) push = "push" in parts[2:] shared = next((p[6:] for p in parts[2:] if p.startswith("shared")), "0") ndocs = 1 if shared != "0" else typists sqids = conn["sqids"][offset:offset + ndocs] offset += ndocs sub = dict(conn, sqids=sqids) run_dir = out / f"run{n}-{run.replace(':', '-')}" run_dir.mkdir(exist_ok=True) (run_dir / "connection.json").write_text(json.dumps(sub)) cenv = dict(os.environ, DOCS_LIVE_PROBE_DIR=str(run_dir), DOCS_LIVE_PROBE_ROLE="clients", DOCS_LIVE_PROBE_POLICY=policy, DOCS_LIVE_PROBE_TYPISTS=str(typists), DOCS_LIVE_PROBE_KEYS=opts.get("KEYS", "200"), DOCS_LIVE_PROBE_MEAN_MS=mean, DOCS_LIVE_PROBE_UPDATES=updates, DOCS_LIVE_PROBE_LABEL=run.replace(":", "-") + f"-rtt{conn['rtt_ms']}", DOCS_LIVE_PROBE_PUSH="1" if push else "0", DOCS_LIVE_PROBE_SHARED=shared, TMPDIR="/tmp") r = subprocess.run([gotest, "-test.run", "^TestLivePushProbeClients$", "-test.count=1", "-test.timeout=20m", "-test.v"], cwd=root / "internal/docs", env=cenv, capture_output=True, text=True) for line in (r.stdout + r.stderr).splitlines(): if line.startswith("LIVEPROBE") or "FAIL" in line or "panic" in line or "_test.go" in line: print(line, flush=True) if line.startswith("LIVEPROBE"): with (out / "summary.jsonl").open("a") as f: f.write(line[len("LIVEPROBE "):] + "\n") (out / "done").touch() server.wait(timeout=30) finally: if server.poll() is None: os.killpg(server.pid, signal.SIGTERM) server.wait(timeout=10) log.close()