Files
mtusi2026lab3/scripts/scaling-benchmark.py

194 lines
6.1 KiB
Python

#!/usr/bin/env python3
import base64
import json
import os
import subprocess
import sys
import threading
import time
import uuid
from pathlib import Path
import httpx
BASE = "http://localhost:8080"
ROOT = Path(__file__).resolve().parent.parent
EVIDENCE = ROOT / "evidence" / "scaling.json"
def register_and_login(client):
username = "scale_" + uuid.uuid4().hex[:10]
password = "Test-password-" + uuid.uuid4().hex[:8]
r = client.post(
f"{BASE}/api/v1/users",
json={"username": username, "display_name": "S", "password": password},
)
assert r.status_code == 201, r.text
user = r.json()
credential = base64.b64encode((username + ":" + password).encode()).decode()
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 run_load(chat_id: str, token: str, concurrency: int, duration: float):
latencies = []
errors = 0
lock = threading.Lock()
def worker():
nonlocal errors
local_lat = []
local_err = 0
with httpx.Client(timeout=15) as c:
deadline = time.monotonic() + duration
i = 0
while time.monotonic() < deadline:
t0 = time.monotonic()
try:
if i % 10 == 0:
r = c.get(
f"{BASE}/api/v1/chats/{chat_id}/messages?limit=10",
headers={"Authorization": "Bearer " + token},
)
else:
r = c.post(
f"{BASE}/api/v1/chats/{chat_id}/messages",
json={"text": "s-" + uuid.uuid4().hex[:6]},
headers={
"Authorization": "Bearer " + token,
"Idempotency-Key": uuid.uuid4().hex,
},
)
if r.status_code not in (200, 201):
local_err += 1
else:
local_lat.append(time.monotonic() - t0)
except Exception:
local_err += 1
i += 1
with lock:
latencies.extend(local_lat)
errors += local_err
threads = [threading.Thread(target=worker) for _ in range(concurrency)]
started = time.monotonic()
for t in threads:
t.start()
for t in threads:
t.join()
elapsed = time.monotonic() - started
latencies.sort()
def pct(p):
if not latencies:
return None
idx = max(0, int(len(latencies) * p) - 1)
return round(latencies[idx] * 1000, 2)
return {
"concurrency": concurrency,
"duration_s": round(elapsed, 2),
"success": len(latencies),
"errors": errors,
"rps": round(len(latencies) / elapsed, 2) if elapsed else 0,
"p50_ms": pct(0.50),
"p95_ms": pct(0.95),
"p99_ms": pct(0.99),
}
def switch_stack(replicas: int, cpus: str, memory: str):
env = dict(os.environ)
env["API_CPUS"] = cpus
env["API_MEMORY"] = memory
subprocess.run(
["docker", "compose", "--env-file", ".env", "-f", "compose.yaml", "-f", "compose-scale.yaml", "down"],
cwd=ROOT, check=True, env=env,
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL,
)
subprocess.run(
["docker", "compose", "--env-file", ".env", "-f", "compose.yaml", "-f", "compose-scale.yaml",
"up", "-d", "--build", "--scale", f"api={replicas}"],
cwd=ROOT, check=True, env=env,
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL,
)
for _ in range(120):
try:
with httpx.Client(timeout=2) as c:
r = c.get(f"{BASE}/health/ready")
if r.status_code == 200:
time.sleep(8)
return
except Exception:
pass
time.sleep(1)
raise RuntimeError("API did not become ready")
def experiment_series(name, configurations, concurrency):
results = []
for cfg in configurations:
replicas = cfg["replicas"]
cpus = cfg["cpus"]
memory = cfg["memory"]
print(f"\n=== {name}: replicas={replicas}, cpus={cpus}, memory={memory}, clients={concurrency} ===", flush=True)
switch_stack(replicas, cpus, memory)
with httpx.Client(timeout=15) as c:
alice = register_and_login(c)
bob = register_and_login(c)
chat = c.post(
f"{BASE}/api/v1/chats",
json={"title": "scale", "member_ids": [alice["id"], bob["id"]]},
headers={"Authorization": "Bearer " + alice["token"]},
).json()
for run in range(3):
print(f" run {run+1}/3...", flush=True)
m = run_load(chat["id"], alice["token"], concurrency=concurrency, duration=20)
m["replicas"] = replicas
m["cpus_per_replica"] = cpus
m["memory_per_replica"] = memory
m["run"] = run + 1
m["series"] = name
results.append(m)
print(f" RPS={m['rps']}, p95={m['p95_ms']}, errors={m['errors']}", flush=True)
return results
def main():
all_results = []
print("### Baseline: 1 replica, 2 CPU, 20 clients", flush=True)
all_results.extend(experiment_series(
"baseline",
[{"replicas": 1, "cpus": "2.0", "memory": "512M"}],
concurrency=20,
))
print("\n### Improved: 2 replicas, 1 CPU, 20 clients", flush=True)
all_results.extend(experiment_series(
"improved",
[{"replicas": 2, "cpus": "1.0", "memory": "256M"}],
concurrency=20,
))
EVIDENCE.parent.mkdir(parents=True, exist_ok=True)
EVIDENCE.write_text(
json.dumps(all_results, indent=2, ensure_ascii=False),
encoding="utf-8",
)
print(f"\nSaved {len(all_results)} records to {EVIDENCE}")
return 0
if __name__ == "__main__":
sys.exit(main())