"""Cache stampede experiment. Measures SQL calls and latency for parallel GET /messages with cold cache. Run twice: before and after single-flight protection. """ import base64 import json import os import subprocess import time import uuid from concurrent.futures import ThreadPoolExecutor from pathlib import Path import httpx BASE = "http://localhost:8080" EVIDENCE = Path(__file__).resolve().parent.parent / "evidence" / "cache_experiment.json" def register_and_login(client: httpx.Client) -> dict: username = "cache_" + uuid.uuid4().hex[:10] password = "Test-password-1234" r = client.post( f"{BASE}/api/v1/users", json={ "username": username, "display_name": "Cache", "password": password, }, ) assert r.status_code == 201, r.text user = r.json() credential = base64.b64encode( (username + ":" + password).encode("utf-8") ).decode("ascii") r = client.post( f"{BASE}/api/v1/auth/sessions", headers={"Authorization": "Basic " + credential}, ) assert r.status_code == 201, r.text return {"id": user["id"], "token": r.json()["token"]} def auth(actor: dict) -> dict: return {"Authorization": "Bearer " + actor["token"]} def sql_calls_for_query(substring: str) -> int: """Count SQL calls from pg_stat_statements matching substring.""" sql = ( "SELECT COALESCE(SUM(calls), 0) FROM pg_stat_statements " f"WHERE query ILIKE '%{substring}%';" ) result = subprocess.run( [ "docker", "compose", "exec", "-T", "postgres", "psql", "-U", "messenger", "-d", "messenger", "-t", "-A", "-c", sql, ], capture_output=True, text=True, check=True, ) return int(result.stdout.strip() or "0") def reset_pg_stats() -> None: subprocess.run( [ "docker", "compose", "exec", "-T", "postgres", "psql", "-U", "messenger", "-d", "messenger", "-c", "SELECT pg_stat_statements_reset();", ], capture_output=True, text=True, check=True, ) def flush_cache() -> None: subprocess.run( [ "docker", "compose", "exec", "-T", "valkey", "sh", "-c", "valkey-cli --scan --pattern 'messages:*' | xargs -r valkey-cli DEL", ], capture_output=True, text=True, check=True, ) def run_parallel_gets(chat_id: str, token: str, concurrency: int = 50): """Fire N parallel GET requests; return list of latencies and statuses.""" results = [] def worker(): with httpx.Client(timeout=15) as c: t0 = time.monotonic() r = c.get( f"{BASE}/api/v1/chats/{chat_id}/messages?limit=50", headers={"Authorization": "Bearer " + token}, ) dt = time.monotonic() - t0 return dt, r.status_code with ThreadPoolExecutor(max_workers=concurrency) as pool: results = list(pool.map(lambda _: worker(), range(concurrency))) return results def main(): label = os.getenv("LABEL", "before") concurrency = int(os.getenv("CONCURRENCY", "50")) with httpx.Client(timeout=30) as c: alice = register_and_login(c) bob = register_and_login(c) chat = c.post( f"{BASE}/api/v1/chats", json={"title": "cache", "member_ids": [alice["id"], bob["id"]]}, headers=auth(alice), ).json() for i in range(100): c.post( f"{BASE}/api/v1/chats/{chat['id']}/messages", json={"text": f"seed-{i:03d}"}, headers=auth(alice), ) flush_cache() reset_pg_stats() time.sleep(0.5) print(f"Running {concurrency} parallel GETs (label={label})...") started = time.monotonic() results = run_parallel_gets(chat["id"], alice["token"], concurrency) elapsed = time.monotonic() - started latencies = sorted(dt for dt, _ in results) statuses = [s for _, s in results] ok_count = sum(1 for s in statuses if s == 200) errors = concurrency - ok_count sql_calls = sql_calls_for_query("FROM messages") def pct(p): idx = max(0, int(len(latencies) * p) - 1) return round(latencies[idx] * 1000, 2) report = { "label": label, "concurrency": concurrency, "elapsed_s": round(elapsed, 3), "ok": ok_count, "errors": errors, "latency_ms": { "p50": pct(0.5), "p95": pct(0.95), "p99": pct(0.99), }, "sql_calls_from_messages": sql_calls, } print(json.dumps(report, indent=2, ensure_ascii=False)) existing = [] if EVIDENCE.exists(): try: existing = json.loads(EVIDENCE.read_text(encoding="utf-8")) except json.JSONDecodeError: existing = [] existing.append(report) EVIDENCE.parent.mkdir(parents=True, exist_ok=True) EVIDENCE.write_text( json.dumps(existing, indent=2, ensure_ascii=False), encoding="utf-8", ) print(f"Saved to {EVIDENCE}") if __name__ == "__main__": main()