forked from mtusi2026/mtusi2026lab3
111 lines
3.4 KiB
Python
111 lines
3.4 KiB
Python
#!/usr/bin/env python3
|
|
import base64
|
|
import json
|
|
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" / "rolling-update.json"
|
|
|
|
|
|
def register_and_login(client):
|
|
username = "roll_" + uuid.uuid4().hex[:10]
|
|
password = "Test-password-" + uuid.uuid4().hex[:8]
|
|
r = client.post(
|
|
f"{BASE}/api/v1/users",
|
|
json={"username": username, "display_name": "R", "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 main():
|
|
with httpx.Client(timeout=5) as c:
|
|
alice = register_and_login(c)
|
|
bob = register_and_login(c)
|
|
chat = c.post(
|
|
f"{BASE}/api/v1/chats",
|
|
json={"title": "roll", "member_ids": [alice["id"], bob["id"]]},
|
|
headers={"Authorization": "Bearer " + alice["token"]},
|
|
).json()
|
|
|
|
running = {"flag": True}
|
|
latencies = []
|
|
errors = 0
|
|
lock = threading.Lock()
|
|
|
|
def worker():
|
|
nonlocal errors
|
|
with httpx.Client(timeout=3) as c:
|
|
while running["flag"]:
|
|
t0 = time.monotonic()
|
|
try:
|
|
r = c.post(
|
|
f"{BASE}/api/v1/chats/{chat['id']}/messages",
|
|
json={"text": "roll-" + uuid.uuid4().hex[:8]},
|
|
headers={
|
|
"Authorization": "Bearer " + alice["token"],
|
|
"Idempotency-Key": uuid.uuid4().hex,
|
|
},
|
|
)
|
|
dt = time.monotonic() - t0
|
|
if r.status_code == 201:
|
|
with lock:
|
|
latencies.append(dt)
|
|
else:
|
|
with lock:
|
|
errors += 1
|
|
except Exception:
|
|
with lock:
|
|
errors += 1
|
|
|
|
threads = [threading.Thread(target=worker) for _ in range(4)]
|
|
for t in threads:
|
|
t.start()
|
|
|
|
time.sleep(3)
|
|
with lock:
|
|
before = len(latencies)
|
|
print(f"[roll] baseline RPS ~{before/3:.1f}", flush=True)
|
|
|
|
for replica_name in ["mtusi2026lab3-api-1", "mtusi2026lab3-api-2"]:
|
|
print(f"[roll] restarting {replica_name}", flush=True)
|
|
subprocess.run(["docker", "restart", replica_name], check=True)
|
|
time.sleep(8)
|
|
|
|
time.sleep(2)
|
|
|
|
running["flag"] = False
|
|
for t in threads:
|
|
t.join(timeout=5)
|
|
|
|
with lock:
|
|
report = {
|
|
"total_successful": len(latencies),
|
|
"total_errors": errors,
|
|
"baseline_rps_3s": round(before / 3.0, 2),
|
|
"avg_latency_ms": round(1000 * sum(latencies) / len(latencies), 2) if latencies else None,
|
|
}
|
|
print(json.dumps(report, indent=2, ensure_ascii=False))
|
|
|
|
EVIDENCE.parent.mkdir(parents=True, exist_ok=True)
|
|
EVIDENCE.write_text(json.dumps(report, indent=2, ensure_ascii=False), encoding="utf-8")
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(main()) |