#!/usr/bin/env python3 """Reproducible isolated Compose experiment for track 1.""" import base64 import concurrent.futures import json import math import os import statistics import subprocess import time import urllib.request import uuid from pathlib import Path ROOT = Path(__file__).resolve().parents[1] BASE = os.environ.get("BASE_URL", "http://localhost:8080") def compose(*args, env=None): return subprocess.run(["docker", "compose", "--env-file", ".env", *args], cwd=ROOT, env=env, check=True, capture_output=True, text=True).stdout def api(method, path, token=None, data=None, extra=None): headers = {"Accept": "application/json"} if token: headers["Authorization"] = "Bearer " + token if data is not None: headers["Content-Type"] = "application/json" headers.update(extra or {}) raw = json.dumps(data).encode() if data is not None else None request = urllib.request.Request(BASE + path, method=method, headers=headers, data=raw) start = time.perf_counter() with urllib.request.urlopen(request, timeout=10) as response: payload = json.loads(response.read()) return payload, (time.perf_counter() - start) * 1000 def metrics(): code = "import json,urllib.request; print(urllib.request.urlopen('http://127.0.0.1:8080/internal/cache-metrics').read().decode())" return json.loads(compose("exec", "-T", "api", "python", "-c", code)) def run_batch(path, token, count=32): with concurrent.futures.ThreadPoolExecutor(max_workers=count) as workers: results = list(workers.map(lambda _: api("GET", path, token), range(count))) assert all(item[0] == results[0][0] for item in results) times = sorted(item[1] for item in results) return {"requests": count, "p50_ms": round(statistics.median(times), 2), "p95_ms": round(times[math.ceil(count * 0.95) - 1], 2), "p99_ms": round(times[-1], 2)} def restart_api(cache_enabled, lock_enabled): env = dict(os.environ, CACHE_ENABLED="1" if cache_enabled else "0", CACHE_LOCK_ENABLED="1" if lock_enabled else "0", CACHE_MISS_DELAY_MS="150", ENABLE_EXPERIMENT_METRICS="1") compose("up", "-d", "--force-recreate", "--wait", "api", env=env) def main(): username = "experiment_" + uuid.uuid4().hex[:12] password = "synthetic-" + uuid.uuid4().hex account, _ = api("POST", "/api/v1/users", data={"username": username, "display_name": "Experiment", "password": password}) basic = base64.b64encode((username + ":" + password).encode()).decode() session, _ = api("POST", "/api/v1/auth/sessions", extra={"Authorization": "Basic " + basic}) token = session["token"] chat, _ = api("POST", "/api/v1/chats", token, {"title": "Cache experiment", "member_ids": [account["id"]]}) chat_id = chat["id"] # Synthetic seed in one transaction; the same indexed data serves both modes. sql = ("BEGIN; INSERT INTO messages(id,chat_id,sender_id,text) " f"SELECT gen_random_uuid(),'{chat_id}'::uuid,'{account['id']}'::uuid,'seed-'||g " "FROM generate_series(1,10000) g; " f"UPDATE chat_versions SET version=version+1 WHERE chat_id='{chat_id}'; COMMIT;") compose("exec", "-T", "postgres", "psql", "-U", "messenger", "-d", "messenger", "-c", sql) explain_sql = ("EXPLAIN (ANALYZE, BUFFERS) SELECT id,chat_id,sender_id,text,created_at " f"FROM messages WHERE chat_id='{chat_id}' ORDER BY created_at,id LIMIT 101;") compose("exec", "-T", "postgres", "psql", "-U", "messenger", "-d", "messenger", "-c", "ANALYZE messages;") plan = compose("exec", "-T", "postgres", "psql", "-U", "messenger", "-d", "messenger", "-c", explain_sql) (ROOT / "evidence" / "history_explain.txt").write_text(explain_sql + "\n\n" + plan) path = f"/api/v1/chats/{chat_id}/messages?limit=100" result = {"seed_messages": 10000, "concurrency": 32, "cache_miss_delay_ms": 150, "modes": {}} for label, cache_enabled, lock_enabled in (("cache_off", False, False), ("cache_unlocked", True, False), ("cache_locked", True, True)): restart_api(cache_enabled, lock_enabled) compose("exec", "-T", "redis", "redis-cli", "FLUSHDB") before = metrics() cold = run_batch(path, token) middle = metrics() warm = run_batch(path, token) after = metrics() result["modes"][label] = { "cold": cold, "warm": warm, "cold_history_sql": middle["history_sql"] - before["history_sql"], "warm_history_sql": after["history_sql"] - middle["history_sql"], "cold_cache_hits": middle["history_cache_hit"] - before["history_cache_hit"], "cold_lock_waits": middle["history_lock_wait"] - before["history_lock_wait"], } # A write changes the database version in the same transaction as the message. fresh, _ = api("POST", "/api/v1/chats", token, {"title": "Consistency probe", "member_ids": [account["id"]]}) fresh_path = f"/api/v1/chats/{fresh['id']}/messages?limit=100" api("GET", fresh_path, token) # cache an empty page new, _ = api("POST", f"/api/v1/chats/{fresh['id']}/messages", token, {"text": "read after write"}) after = api("GET", fresh_path, token)[0] result["read_after_write"] = any(item["id"] == new["id"] for item in after["items"]) # The first page is restored from PostgreSQL after the disposable cache restarts. compose("restart", "redis") recovered = api("GET", fresh_path, token)[0] result["cache_restart_recovered"] = any(item["id"] == new["id"] for item in recovered["items"]) assert result["read_after_write"] and result["cache_restart_recovered"] output = ROOT / "evidence" / os.environ.get("EVIDENCE_OUTPUT", "cache_experiment.json") output.write_text(json.dumps(result, ensure_ascii=False, indent=2) + "\n") print(output) print(json.dumps(result, ensure_ascii=False, indent=2)) if __name__ == "__main__": main()