Files
mtusi2026lab3/scripts/fair-scaling-benchmark.py
T

230 lines
7.0 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" / "fair_scaling.json"
def register_and_login(client):
username = "fair_" + uuid.uuid4().hex[:10]
password = "Test-password-" + uuid.uuid4().hex[:8]
r = client.post(
f"{BASE}/api/v1/users",
json={"username": username, "display_name": "F", "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, token, concurrency, duration):
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": "fair-" + 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 write_nginx(strategy):
if strategy == "least_conn":
upstream = """ upstream api_backend {
least_conn;
server api:8080 max_fails=1 fail_timeout=10s;
}"""
else:
upstream = """ upstream api_backend {
server api:8080 max_fails=1 fail_timeout=10s;
}"""
content = f"""worker_processes auto;
events {{
worker_connections 1024;
}}
http {{
{upstream}
server {{
listen 8080;
server_name _;
location / {{
proxy_pass http://api_backend;
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_connect_timeout 1s;
proxy_send_timeout 30s;
proxy_read_timeout 30s;
proxy_next_upstream error timeout http_502 http_503 http_504;
}}
}}
}}
"""
(ROOT / "nginx.conf").write_text(content, encoding="utf-8")
def switch_stack(replicas, cpus, memory):
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 run_series(name, strategy, configs):
results = []
write_nginx(strategy)
print(f"\n### Series {name} ({strategy})", flush=True)
for cfg in configs:
replicas = cfg["replicas"]
cpus = cfg["cpus"]
memory = cfg["memory"]
print(f"\n=== replicas={replicas}, cpus={cpus}, memory={memory} ===", 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": "fair", "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=20, duration=20)
m["replicas"] = replicas
m["cpus_per_replica"] = cpus
m["memory_per_replica"] = memory
m["run"] = run + 1
m["series"] = name
m["strategy"] = strategy
results.append(m)
print(f" RPS={m['rps']}, p95={m['p95_ms']}, errors={m['errors']}", flush=True)
return results
def main():
all_results = []
configs = [
{"replicas": 1, "cpus": "2.0", "memory": "512M"},
{"replicas": 2, "cpus": "1.0", "memory": "256M"},
{"replicas": 4, "cpus": "0.5", "memory": "128M"},
]
all_results.extend(run_series("A_least_conn", "least_conn", configs))
all_results.extend(run_series("B_round_robin", "round_robin", configs))
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())