oficina / docs Public

Clone
git clone https://amadan.net/oficina/docs

Plain git — no account needed to clone.

Download

Download this file

1#!/usr/bin/env python3
2"""Run the live-push latency probe: the real Docs server (optionally behind a
3loopback proxy that injects a round trip) and simulated typists/watchers.
4
5 run-probe.py <seed.bin> <updates-dir> <out-dir> RTT=50 DOCS=1 [run ...]
6
7Each run is POLICY:TYPISTS[:MEAN_MS], e.g. web250:1 native250:1 idle0:1 idle50:5.
8Expects the Go test binary at $DOCS_LIVE_PROBE_GOTEST (built with
9`CGO_ENABLED=0 go test -c -o /tmp/live-push/docs.test ./internal/docs/`).
10One server serves every run (each run uses its own documents, so DOCS must
11cover the sum of TYPISTS). With SERVER_ONLY=1 it starts the server, prints
12connection.json and waits for <out-dir>/done (for the browser and native probes).
13"""
14import json, os, signal, subprocess, sys, time
15from pathlib import Path
16
17root = Path(__file__).resolve().parents[3]
18seed, updates, out = sys.argv[1], sys.argv[2], Path(sys.argv[3])
19opts = dict(a.split("=", 1) for a in sys.argv[4:] if "=" in a)
20runs = [a for a in sys.argv[4:] if "=" not in a]
21gotest = os.environ.get("DOCS_LIVE_PROBE_GOTEST", "/tmp/live-push/docs.test")
22out.mkdir(parents=True, exist_ok=True)
23for f in ("connection.json", "done"):
24 (out / f).unlink(missing_ok=True)
25env = dict(os.environ, DOCS_LIVE_PROBE_DIR=str(out), DOCS_LIVE_PROBE_SEED=seed, DOCS_LIVE_PROBE_ROLE="server",
26 DOCS_LIVE_PROBE_RTT_MS=opts.get("RTT", "0"), DOCS_LIVE_PROBE_DOCS=opts.get("DOCS", "1"), TMPDIR="/tmp",
27 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"))
28log = (out / "server.log").open("w")
29extra = ["-test.cpuprofile", opts["CPUPROFILE"]] if "CPUPROFILE" in opts else []
30server = subprocess.Popen([gotest, "-test.run", "^TestLivePushProbeServer$", "-test.count=1", "-test.timeout=70m"] + extra,
31 cwd=root / "internal/docs", env=env, stdout=log, stderr=subprocess.STDOUT, start_new_session=True)
32try:
33 deadline = time.monotonic() + 180
34 while not (out / "connection.json").exists():
35 if server.poll() is not None:
36 raise SystemExit((out / "server.log").read_text()[-4000:])
37 if time.monotonic() > deadline:
38 raise SystemExit("server did not start")
39 time.sleep(0.05)
40 if opts.get("SERVER_ONLY") == "1":
41 print((out / "connection.json").read_text(), flush=True)
42 while not (out / "done").exists():
43 time.sleep(0.2)
44 offset = 0
45 conn = json.loads((out / "connection.json").read_text())
46 for n, run in enumerate(runs):
47 parts = run.split(":")
48 policy, typists = parts[0], int(parts[1])
49 mean = next((p for p in parts[2:] if p.isdigit()), opts.get("MEAN_MS", "150"))
50 push = "push" in parts[2:]
51 shared = next((p[6:] for p in parts[2:] if p.startswith("shared")), "0")
52 ndocs = 1 if shared != "0" else typists
53 sqids = conn["sqids"][offset:offset + ndocs]
54 offset += ndocs
55 sub = dict(conn, sqids=sqids)
56 run_dir = out / f"run{n}-{run.replace(':', '-')}"
57 run_dir.mkdir(exist_ok=True)
58 (run_dir / "connection.json").write_text(json.dumps(sub))
59 cenv = dict(os.environ, DOCS_LIVE_PROBE_DIR=str(run_dir), DOCS_LIVE_PROBE_ROLE="clients", DOCS_LIVE_PROBE_POLICY=policy,
60 DOCS_LIVE_PROBE_TYPISTS=str(typists), DOCS_LIVE_PROBE_KEYS=opts.get("KEYS", "200"), DOCS_LIVE_PROBE_MEAN_MS=mean,
61 DOCS_LIVE_PROBE_UPDATES=updates, DOCS_LIVE_PROBE_LABEL=run.replace(":", "-") + f"-rtt{conn['rtt_ms']}",
62 DOCS_LIVE_PROBE_PUSH="1" if push else "0", DOCS_LIVE_PROBE_SHARED=shared, TMPDIR="/tmp")
63 r = subprocess.run([gotest, "-test.run", "^TestLivePushProbeClients$", "-test.count=1", "-test.timeout=20m", "-test.v"],
64 cwd=root / "internal/docs", env=cenv, capture_output=True, text=True)
65 for line in (r.stdout + r.stderr).splitlines():
66 if line.startswith("LIVEPROBE") or "FAIL" in line or "panic" in line or "_test.go" in line:
67 print(line, flush=True)
68 if line.startswith("LIVEPROBE"):
69 with (out / "summary.jsonl").open("a") as f:
70 f.write(line[len("LIVEPROBE "):] + "\n")
71 (out / "done").touch()
72 server.wait(timeout=30)
73finally:
74 if server.poll() is None:
75 os.killpg(server.pid, signal.SIGTERM)
76 server.wait(timeout=10)
77 log.close()
78