Files
mtusi2026lab2/student-tests/benchmark.py
T

117 lines
3.6 KiB
Python

import json
import time
import uuid
from concurrent.futures import ThreadPoolExecutor
import httpx
BASE = "http://localhost:8080"
def register(client: httpx.Client) -> dict:
username = "u_" + uuid.uuid4().hex[:10]
return client.post(
f"{BASE}/api/v1/users",
json={"username": username, "display_name": "Bench"},
).json()
def setup(history_size: int):
with httpx.Client(timeout=120) as c:
alice = register(c)
bob = register(c)
chat = c.post(
f"{BASE}/api/v1/chats",
json={"title": "bench", "member_ids": [alice["id"], bob["id"]]},
headers={"X-User-Id": alice["id"]},
).json()
for i in range(history_size):
c.post(
f"{BASE}/api/v1/chats/{chat['id']}/messages",
json={"text": f"seed-{i}"},
headers={"X-User-Id": alice["id"]},
)
return alice, bob, chat
def measure(chat_id: str, actor_id: str, concurrency: int, duration: float = 5.0):
latencies = []
errors = 0
lock = __import__("threading").Lock()
def worker(worker_id):
nonlocal errors
local_lat = []
local_err = 0
with httpx.Client(timeout=10) 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=50",
headers={"X-User-Id": actor_id},
)
else:
r = c.post(
f"{BASE}/api/v1/chats/{chat_id}/messages",
json={"text": "load"},
headers={"X-User-Id": actor_id},
)
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
t_start = time.monotonic()
with ThreadPoolExecutor(max_workers=concurrency) as pool:
list(pool.map(lambda w: worker(w), range(concurrency)))
elapsed = time.monotonic() - t_start
latencies.sort()
def pct(p):
if not latencies:
return None
idx = max(0, int(len(latencies) * p) - 1)
return round(latencies[idx] * 1000, 3)
rps = len(latencies) / elapsed if elapsed else 0
return {
"concurrency": concurrency,
"elapsed_s": round(elapsed, 2),
"success": len(latencies),
"errors": errors,
"rps": round(rps, 2),
"p50_ms": pct(0.50),
"p95_ms": pct(0.95),
"p99_ms": pct(0.99),
}
def main():
results = []
for history in (100, 1000, 10000):
print(f"Setup history={history}...")
alice, _, chat = setup(history)
for conc in (1, 10, 50):
print(f" measure history={history} concurrency={conc}...")
m = measure(chat["id"], alice["id"], conc, duration=5.0)
m["history_size"] = history
results.append(m)
print(" ", json.dumps(m, ensure_ascii=False))
with open("benchmark_results.json", "w", encoding="utf-8") as f:
json.dump(results, f, ensure_ascii=False, indent=2)
print("Saved to benchmark_results.json")
if __name__ == "__main__":
main()