#!/usr/bin/env python3 """Replicated bytes per minute of typing (live-push spec ยง4.3, slice 1b's done list): the probe server on a known database path, `litestream replicate` (the version CARLOS pins: 0.5.14) to a local file replica, and one simulated typist. Litestream's files are written once, so the bytes it ships are the sizes of the files that appear, compaction levels included. replica-bytes.py POLICY[:push] [KEYS=360] [LITESTREAM=litestream] POLICY is a run-probe.py policy (native250 = today's native pacing; idle50 with :push = the 50 ms floor on a push stream). Expects the Go test binary at $DOCS_LIVE_PROBE_GOTEST. """ import json, os, signal, subprocess, sys, time from pathlib import Path root = Path(__file__).resolve().parents[3] seed, updates, out, run = sys.argv[1], sys.argv[2], Path(sys.argv[3]), sys.argv[4] opts = dict(a.split("=", 1) for a in sys.argv[5:] if "=" in a) gotest = os.environ.get("DOCS_LIVE_PROBE_GOTEST", "/tmp/live-push/docs.test") litestream = opts.get("LITESTREAM", "litestream") out.mkdir(parents=True, exist_ok=True) db, replica = out / "docs.db", out / "replica" for f in ("connection.json", "done"): (out / f).unlink(missing_ok=True) senv = dict(os.environ, DOCS_LIVE_PROBE_DIR=str(out), DOCS_LIVE_PROBE_ROLE="server", DOCS_LIVE_PROBE_SEED=seed, DOCS_LIVE_PROBE_DOCS="1", DOCS_LIVE_PROBE_SEAL="1", DOCS_LIVE_PROBE_WARM="1", DOCS_LIVE_PROBE_PUSH="1", DOCS_LIVE_PROBE_DB=str(db), TMPDIR="/tmp") server = subprocess.Popen([gotest, "-test.run", "^TestLivePushProbeServer$", "-test.count=1", "-test.timeout=30m"], cwd=root / "internal/docs", env=senv, stdout=(out / "server.log").open("w"), stderr=subprocess.STDOUT, start_new_session=True) ls = None try: while not (out / "connection.json").exists(): if server.poll() is not None: raise SystemExit((out / "server.log").read_text()[-3000:]) time.sleep(0.05) (out / "litestream.yml").write_text(f"dbs:\n - path: {db}\n replica:\n type: file\n path: {replica}\n") ls = subprocess.Popen([litestream, "replicate", "-config", str(out / "litestream.yml")], stdout=(out / "litestream.log").open("w"), stderr=subprocess.STDOUT, start_new_session=True) seen = {} def poll(): for p in replica.rglob("*"): if p.is_file(): try: size = p.stat().st_size except FileNotFoundError: continue if seen.get(p) != size: seen[p] = size for _ in range(50): # the initial snapshot settles time.sleep(0.1) poll() time.sleep(3) poll() base = dict(seen) parts = run.split(":") policy, push = parts[0], "push" in parts[1:] run_dir = out / "run" run_dir.mkdir(exist_ok=True) (run_dir / "connection.json").write_text((out / "connection.json").read_text()) 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="1", DOCS_LIVE_PROBE_KEYS=opts.get("KEYS", "360"), DOCS_LIVE_PROBE_MEAN_MS="150", DOCS_LIVE_PROBE_UPDATES=updates, DOCS_LIVE_PROBE_LABEL=run.replace(":", "-"), DOCS_LIVE_PROBE_PUSH="1" if push else "0", DOCS_LIVE_PROBE_SHARED="0", TMPDIR="/tmp") clients = subprocess.Popen([gotest, "-test.run", "^TestLivePushProbeClients$", "-test.count=1", "-test.timeout=20m", "-test.v"], cwd=root / "internal/docs", env=cenv, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True) start = time.time() while clients.poll() is None: poll() time.sleep(0.25) typed = time.time() - start for _ in range(12): # the last syncs time.sleep(0.25) poll() summary = next((json.loads(l[10:]) for l in clients.stdout.read().splitlines() if l.startswith("LIVEPROBE ")), {}) written = sum(size for p, size in seen.items() if base.get(p) != size) posts = summary.get("posts_per_s_per_typist", 0) * summary.get("seconds", 0) result = {"run": run, "seconds": round(summary.get("seconds", typed), 1), "uploads": round(posts), "replica_bytes": written, "replica_kb_per_minute": round(written / 1024 / (summary.get("seconds", typed) / 60), 1), "replica_files": sum(1 for p in seen if p not in base)} print("REPLICA " + json.dumps(result), flush=True) with (out / "replica.jsonl").open("a") as f: f.write(json.dumps(result) + "\n") finally: (out / "done").touch() for p in (ls, server): if p is not None and p.poll() is None: os.killpg(p.pid, signal.SIGTERM) p.wait(timeout=15)