Files
mtusi2026lab4/scripts/measure-detection.py

201 lines
6.6 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" / "detection.json"
STOP_LABEL = "mtusi2026lab3-api-"
def register_and_login(client: httpx.Client) -> dict:
username = "det_" + uuid.uuid4().hex[:10]
password = "Test-password-" + uuid.uuid4().hex[:8]
r = client.post(
f"{BASE}/api/v1/users",
json={"username": username, "display_name": "D", "password": password},
)
assert r.status_code == 201, r.text
user = r.json()
credential = base64.b64encode(
(username + ":" + password).encode("utf-8")
).decode("ascii")
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 find_container_by_instance(target: str) -> str:
names = subprocess.run(
["docker", "ps", "--filter", f"name={STOP_LABEL}", "--format", "{{.Names}}"],
capture_output=True, text=True, check=True,
).stdout.split()
for name in names:
result = subprocess.run(
["docker", "logs", name],
capture_output=True, text=True, check=False,
)
combined = result.stdout + result.stderr
if f"INSTANCE_ID={target}" in combined:
return name
raise RuntimeError(f"no container with INSTANCE_ID={target}")
def get_instances(n: int = 30) -> set:
seen = set()
with httpx.Client(timeout=2) as c:
for _ in range(n):
try:
r = c.get(f"{BASE}/health/live")
if r.status_code == 200:
seen.add(r.headers.get("x-instance-id"))
except Exception:
pass
return seen
def main():
print("[measure] preparing user and chat", flush=True)
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": "det", "member_ids": [alice["id"], bob["id"]]},
headers={"Authorization": "Bearer " + alice["token"]},
).json()
instances_before = get_instances()
if len(instances_before) < 2:
print(f"[measure] need >=2 replicas, got {instances_before}", file=sys.stderr)
return 1
print(f"[measure] instances before: {instances_before}", flush=True)
target = sorted(instances_before)[0]
container = find_container_by_instance(target)
print(f"[measure] will stop {container} (instance={target})", flush=True)
shared = {
"latencies": [],
"errors": 0,
"instances": [],
"stop_time": None,
"first_success_after_stop": None,
"first_success_instance": None,
"stop_seen": False,
}
lock = threading.Lock()
running = {"flag": True}
def worker():
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": "det-" + uuid.uuid4().hex[:8]},
headers={
"Authorization": "Bearer " + alice["token"],
"Idempotency-Key": uuid.uuid4().hex,
},
)
dt = time.monotonic() - t0
if r.status_code == 201:
inst = r.headers.get("x-instance-id")
with lock:
shared["latencies"].append(dt)
shared["instances"].append(inst)
if shared["stop_time"] is not None and not shared["stop_seen"]:
if inst != target:
shared["first_success_after_stop"] = time.monotonic() - shared["stop_time"]
shared["first_success_instance"] = inst
shared["stop_seen"] = True
else:
with lock:
shared["errors"] += 1
except Exception:
with lock:
shared["errors"] += 1
print("[measure] starting background load", flush=True)
threads = [threading.Thread(target=worker) for _ in range(4)]
for t in threads:
t.start()
time.sleep(3)
with lock:
baseline_count = len(shared["latencies"])
baseline_rps = baseline_count / 3.0
print(f"[measure] baseline RPS ~{baseline_rps:.1f}", flush=True)
print(f"[measure] stopping {container}", flush=True)
with lock:
shared["stop_time"] = time.monotonic()
subprocess.run(["docker", "stop", "--time=2", container], check=True)
deadline = time.monotonic() + 20
while time.monotonic() < deadline:
with lock:
if shared["first_success_after_stop"] is not None:
break
time.sleep(0.05)
time.sleep(2)
running["flag"] = False
for t in threads:
t.join(timeout=5)
with lock:
report = {
"target_instance": target,
"target_container": container,
"instances_before": list(instances_before),
"total_successful": len(shared["latencies"]),
"total_errors": shared["errors"],
"baseline_rps_3s": round(baseline_rps, 2),
"detection_seconds": (
round(shared["first_success_after_stop"], 3)
if shared["first_success_after_stop"] else None
),
"first_success_instance_after_stop": shared["first_success_instance"],
}
print(json.dumps(report, ensure_ascii=False, indent=2))
EVIDENCE.parent.mkdir(parents=True, exist_ok=True)
existing = []
if EVIDENCE.exists():
try:
existing = json.loads(EVIDENCE.read_text(encoding="utf-8"))
except json.JSONDecodeError:
existing = []
existing.append(report)
EVIDENCE.write_text(
json.dumps(existing, indent=2, ensure_ascii=False),
encoding="utf-8",
)
print(f"[measure] restoring {container}", flush=True)
subprocess.run(
["docker", "compose", "--env-file", ".env", "up", "-d", "--scale", "api=2"],
cwd=ROOT, check=True,
)
time.sleep(5)
return 0
if __name__ == "__main__":
sys.exit(main())