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"""Replicated bytes per minute of typing (live-push spec §4.3, slice 1b's
3done list): the probe server on a known database path, `litestream
4replicate` (the version CARLOS pins: 0.5.14) to a local file replica, and
5one simulated typist. Litestream's files are written once, so the bytes it
6ships are the sizes of the files that appear, compaction levels included.
7
8 replica-bytes.py <seed.bin> <updates-dir> <out-dir> POLICY[:push] [KEYS=360] [LITESTREAM=litestream]
9
10POLICY is a run-probe.py policy (native250 = today's native pacing; idle50
11with :push = the 50 ms floor on a push stream). Expects the Go test binary
12at $DOCS_LIVE_PROBE_GOTEST.
13"""
14import json, os, signal, subprocess, sys, time
15from pathlib import Path
16
17root = Path(__file__).resolve().parents[3]
18seed, updates, out, run = sys.argv[1], sys.argv[2], Path(sys.argv[3]), sys.argv[4]
19opts = dict(a.split("=", 1) for a in sys.argv[5:] if "=" in a)
20gotest = os.environ.get("DOCS_LIVE_PROBE_GOTEST", "/tmp/live-push/docs.test")
21litestream = opts.get("LITESTREAM", "litestream")
22out.mkdir(parents=True, exist_ok=True)
23db, replica = out / "docs.db", out / "replica"
24for f in ("connection.json", "done"):
25 (out / f).unlink(missing_ok=True)
26
27senv = dict(os.environ, DOCS_LIVE_PROBE_DIR=str(out), DOCS_LIVE_PROBE_ROLE="server", DOCS_LIVE_PROBE_SEED=seed,
28 DOCS_LIVE_PROBE_DOCS="1", DOCS_LIVE_PROBE_SEAL="1", DOCS_LIVE_PROBE_WARM="1", DOCS_LIVE_PROBE_PUSH="1",
29 DOCS_LIVE_PROBE_DB=str(db), TMPDIR="/tmp")
30server = subprocess.Popen([gotest, "-test.run", "^TestLivePushProbeServer$", "-test.count=1", "-test.timeout=30m"],
31 cwd=root / "internal/docs", env=senv, stdout=(out / "server.log").open("w"), stderr=subprocess.STDOUT,
32 start_new_session=True)
33ls = None
34try:
35 while not (out / "connection.json").exists():
36 if server.poll() is not None:
37 raise SystemExit((out / "server.log").read_text()[-3000:])
38 time.sleep(0.05)
39 (out / "litestream.yml").write_text(f"dbs:\n - path: {db}\n replica:\n type: file\n path: {replica}\n")
40 ls = subprocess.Popen([litestream, "replicate", "-config", str(out / "litestream.yml")],
41 stdout=(out / "litestream.log").open("w"), stderr=subprocess.STDOUT, start_new_session=True)
42 seen = {}
43
44 def poll():
45 for p in replica.rglob("*"):
46 if p.is_file():
47 try:
48 size = p.stat().st_size
49 except FileNotFoundError:
50 continue
51 if seen.get(p) != size:
52 seen[p] = size
53
54 for _ in range(50): # the initial snapshot settles
55 time.sleep(0.1)
56 poll()
57 time.sleep(3)
58 poll()
59 base = dict(seen)
60
61 parts = run.split(":")
62 policy, push = parts[0], "push" in parts[1:]
63 run_dir = out / "run"
64 run_dir.mkdir(exist_ok=True)
65 (run_dir / "connection.json").write_text((out / "connection.json").read_text())
66 cenv = dict(os.environ, DOCS_LIVE_PROBE_DIR=str(run_dir), DOCS_LIVE_PROBE_ROLE="clients", DOCS_LIVE_PROBE_POLICY=policy,
67 DOCS_LIVE_PROBE_TYPISTS="1", DOCS_LIVE_PROBE_KEYS=opts.get("KEYS", "360"), DOCS_LIVE_PROBE_MEAN_MS="150",
68 DOCS_LIVE_PROBE_UPDATES=updates, DOCS_LIVE_PROBE_LABEL=run.replace(":", "-"),
69 DOCS_LIVE_PROBE_PUSH="1" if push else "0", DOCS_LIVE_PROBE_SHARED="0", TMPDIR="/tmp")
70 clients = subprocess.Popen([gotest, "-test.run", "^TestLivePushProbeClients$", "-test.count=1", "-test.timeout=20m", "-test.v"],
71 cwd=root / "internal/docs", env=cenv, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True)
72 start = time.time()
73 while clients.poll() is None:
74 poll()
75 time.sleep(0.25)
76 typed = time.time() - start
77 for _ in range(12): # the last syncs
78 time.sleep(0.25)
79 poll()
80 summary = next((json.loads(l[10:]) for l in clients.stdout.read().splitlines() if l.startswith("LIVEPROBE ")), {})
81 written = sum(size for p, size in seen.items() if base.get(p) != size)
82 posts = summary.get("posts_per_s_per_typist", 0) * summary.get("seconds", 0)
83 result = {"run": run, "seconds": round(summary.get("seconds", typed), 1), "uploads": round(posts),
84 "replica_bytes": written, "replica_kb_per_minute": round(written / 1024 / (summary.get("seconds", typed) / 60), 1),
85 "replica_files": sum(1 for p in seen if p not in base)}
86 print("REPLICA " + json.dumps(result), flush=True)
87 with (out / "replica.jsonl").open("a") as f:
88 f.write(json.dumps(result) + "\n")
89finally:
90 (out / "done").touch()
91 for p in (ls, server):
92 if p is not None and p.poll() is None:
93 os.killpg(p.pid, signal.SIGTERM)
94 p.wait(timeout=15)
95