diff --git a/evidence/track1.txt b/evidence/track1.txt new file mode 100644 index 0000000..21fb32c Binary files /dev/null and b/evidence/track1.txt differ diff --git a/scripts/track1_bench.py b/scripts/track1_bench.py new file mode 100644 index 0000000..da15900 --- /dev/null +++ b/scripts/track1_bench.py @@ -0,0 +1,79 @@ +import time +import sys +import uuid +import json +import base64 +import urllib.request +import urllib.error +import threading + +BASE = "http://127.0.0.1:8080" +PASSWORD = "Example-only-not-a-real-secret-123" + +def call(method, path, body=None, token=None): + h = {"Accept": "application/json"} + if token: + h["Authorization"] = f"Bearer {token}" + if method == "POST" and "/messages" in path: + h["Idempotency-Key"] = uuid.uuid4().hex + data = None + if body is not None: + data = json.dumps(body).encode("utf-8") + h["Content-Type"] = "application/json" + req = urllib.request.Request(BASE + path, data=data, headers=h, method=method) + try: + with urllib.request.urlopen(req, timeout=30) as r: + payload = r.read() + return r.status, (json.loads(payload) if payload else None) + except urllib.error.HTTPError as e: + payload = e.read() + return e.code, (json.loads(payload) if payload else None) + +def main(replicas, concurrency, duration): + name = f"load_{uuid.uuid4().hex[:6]}" + s, u = call("POST", "/api/v1/users", {"username": name, "display_name": name, "password": PASSWORD}) + if s != 201: raise Exception(f"Create user failed: {s} {u}") + + cred = base64.b64encode(f"{name}:{PASSWORD}".encode()).decode() + req = urllib.request.Request( + BASE + "/api/v1/auth/sessions", + headers={"Authorization": f"Basic {cred}"}, + method="POST" + ) + with urllib.request.urlopen(req) as r: + s = json.loads(r.read()) + tok = s["token"] + + s, chat = call("POST", "/api/v1/chats", {"title": "bench", "member_ids": [u["id"]]}, token=tok) + if s != 201: raise Exception(f"Create chat failed: {s} {chat}") + cid = chat["id"] + + latencies = [] + errors = 0 + lock = threading.Lock() + + def worker(): + nonlocal errors + end = time.time() + duration + while time.time() < end: + t0 = time.time() + s, _ = call("POST", f"/api/v1/chats/{cid}/messages", {"text": "hello"}, token=tok) + with lock: + if s == 201: + latencies.append(time.time() - t0) + else: + errors += 1 + + threads = [threading.Thread(target=worker) for _ in range(concurrency)] + start = time.time() + for t in threads: t.start() + for t in threads: t.join() + elapsed = time.time() - start + + latencies.sort() + p95 = latencies[int(len(latencies) * 0.95)] * 1000 if latencies else 0 + rps = len(latencies) / elapsed if elapsed > 0 else 0 + print(f"Replicas: {replicas} | RPS: {rps:.2f} | p95: {p95:.2f}ms | Errors: {errors}") + +if __name__ == "__main__": + main(sys.argv[1], int(sys.argv[2]), int(sys.argv[3])) \ No newline at end of file