Очереди и асинхронная обработка изображений, трек 1

This commit is contained in:
Queenfish
2026-11-12 21:14:30 +03:00
parent 276dbdad8a
commit 01a5023c6d
102 changed files with 6374 additions and 47 deletions
+122
View File
@@ -0,0 +1,122 @@
import base64
import io
import os
import subprocess
import time
import uuid
import threading
import statistics
import httpx
from PIL import Image
BASE = "http://localhost:8080"
CONCURRENCY = 10
TOTAL_FILES = 30
def _png(size=(320, 200), color=(0, 255, 0)):
buf = io.BytesIO()
Image.new("RGB", size, color).save(buf, format="PNG")
return buf.getvalue()
def _user(c):
u = "bench_" + uuid.uuid4().hex[:8]
p = "Test-password-1234"
c.post(f"{BASE}/api/v1/users", json={"username": u, "display_name": "B", "password": p})
cred = base64.b64encode(f"{u}:{p}".encode()).decode()
s = c.post(f"{BASE}/api/v1/auth/sessions", headers={"Authorization": "Basic " + cred}).json()
return {"id": s["user"]["id"], "token": s["token"]}
def _chat(c, a, b):
return c.post(
f"{BASE}/api/v1/chats",
json={"title": "bench", "member_ids": [a["id"], b["id"]]},
headers={"Authorization": "Bearer " + a["token"]},
).json()
def _mem(container):
out = subprocess.run(
["docker", "stats", "--no-stream", "--format", "{{.MemUsage}}", container],
capture_output=True, text=True, check=True,
).stdout.strip()
v = out.split("/")[0].strip()
units = {"B": 1, "KiB": 1024, "Ki": 1024, "MiB": 1024 ** 2, "Mi": 1024 ** 2, "GiB": 1024 ** 3, "Gi": 1024 ** 3}
for u in sorted(units, key=len, reverse=True):
if v.endswith(u):
return int(float(v[: -len(u)]) * units[u])
return 0
def main():
prefetch = os.getenv("PREFETCH_COUNT", "1")
data = _png()
with httpx.Client(timeout=30) as c:
a = _user(c)
b = _user(c)
chat = _chat(c, a, b)
containers = subprocess.run(
["docker", "ps", "--filter", "name=mtusi2026lab5-worker-", "--format", "{{.Names}}"],
capture_output=True, text=True, check=True,
).stdout.split()
if not containers:
raise SystemExit("worker not found")
worker_container = containers[0]
start_time = time.monotonic()
ids = []
def upload_one():
with httpx.Client(timeout=30) as c:
r = c.post(
f"{BASE}/api/v1/chats/{chat['id']}/attachments",
params={"filename": "bench.png"},
content=data,
headers={"Authorization": "Bearer " + a["token"], "Content-Type": "image/png"},
)
if r.status_code == 202:
return r.json()["id"]
return None
threads = []
for _ in range(TOTAL_FILES):
t = threading.Thread(target=lambda: ids.append(upload_one()))
t.start()
threads.append(t)
if len(threads) >= CONCURRENCY:
for t in threads: t.join()
threads = []
for t in threads: t.join()
upload_time = time.monotonic() - start_time
ids = [i for i in ids if i]
print(f"Uploaded {len(ids)} files in {upload_time:.2f}s. RPS (upload): {len(ids) / upload_time:.2f}")
ready_times = []
start_wait = time.monotonic()
for att_id in ids:
with httpx.Client(timeout=10) as c:
while True:
r = c.get(f"{BASE}/api/v1/attachments/{att_id}", headers={"Authorization": "Bearer " + a["token"]})
if r.status_code == 200 and r.json()["status"] == "ready":
ready_times.append(time.monotonic() - start_wait)
break
time.sleep(0.1)
total_time = time.monotonic() - start_time
p95 = statistics.quantiles(ready_times, n=20)[18] if len(ready_times) > 1 else 0
ram = _mem(worker_container)
print(f"--- RESULTS (prefetch={prefetch}) ---")
print(f"Total time: {total_time:.2f}s")
print(f"Throughput: {len(ids) / total_time:.2f} files/sec")
print(f"p95 time to ready: {p95:.2f}s")
print(f"Worker RAM: {ram / 1024 ** 2:.1f} MiB")
if __name__ == "__main__":
main()
+31
View File
@@ -0,0 +1,31 @@
import argparse
import sys
import urllib.error
import urllib.request
def main() -> int:
parser = argparse.ArgumentParser(description="Replay a failed attachment")
parser.add_argument("attachment_id")
parser.add_argument("--base-url", default="http://localhost:8080")
parser.add_argument("--token", required=True)
args = parser.parse_args()
req = urllib.request.Request(
f"{args.base_url}/api/v1/attachments/{args.attachment_id}/replay",
method="POST",
headers={"Authorization": "Bearer " + args.token},
)
try:
with urllib.request.urlopen(req, timeout=10) as resp:
print(f"status={resp.status}")
print(resp.read().decode())
return 0
except urllib.error.HTTPError as e:
print(f"status={e.code}")
print(e.read().decode())
return 1
if __name__ == "__main__":
sys.exit(main())
+38
View File
@@ -0,0 +1,38 @@
#!/usr/bin/env python3
import threading
import time
import uuid
from collections import Counter
import httpx
BASE = "http://localhost:8080"
def main():
counter = Counter()
lock = threading.Lock()
def worker():
with httpx.Client(timeout=5) as c:
for _ in range(20):
try:
r = c.get(f"{BASE}/health/live")
if r.status_code == 200:
with lock:
counter[r.headers.get("x-instance-id")] += 1
except Exception:
pass
time.sleep(0.05)
threads = [threading.Thread(target=worker) for _ in range(20)]
for t in threads:
t.start()
for t in threads:
t.join()
print(counter)
if __name__ == "__main__":
main()
+45
View File
@@ -0,0 +1,45 @@
import base64
import uuid
import httpx
BASE = "http://localhost:8080"
def main():
with httpx.Client(timeout=10) as c:
username = "cv_" + uuid.uuid4().hex[:8]
password = "Test-password-1234"
c.post(f"{BASE}/api/v1/users", json={
"username": username, "display_name": "C", "password": password,
})
credential = base64.b64encode((username + ":" + password).encode()).decode()
s = c.post(f"{BASE}/api/v1/auth/sessions",
headers={"Authorization": "Basic " + credential}).json()
alice_id = s["user"]["id"]
alice_token = s["token"]
r = c.post(
f"{BASE}/api/v1/chats",
json={"title": "dup", "member_ids": [alice_id, alice_id]},
headers={"Authorization": "Bearer " + alice_token},
)
print("duplicate members:", r.status_code, r.text[:200])
r = c.post(
f"{BASE}/api/v1/chats",
json={"title": "bad uuid", "member_ids": [alice_id, "not-a-uuid"]},
headers={"Authorization": "Bearer " + alice_token},
)
print("invalid uuid:", r.status_code, r.text[:200])
r = c.post(
f"{BASE}/api/v1/chats",
json={"title": "ok", "member_ids": [alice_id]},
headers={"Authorization": "Bearer " + alice_token},
)
print("valid chat:", r.status_code)
if __name__ == "__main__":
main()
+63
View File
@@ -0,0 +1,63 @@
import base64
import uuid
import httpx
BASE = "http://localhost:8080"
def register_and_login(c):
username = "m_" + uuid.uuid4().hex[:8]
password = "Test-password-1234"
c.post(f"{BASE}/api/v1/users", json={
"username": username, "display_name": "T", "password": password,
})
credential = base64.b64encode((username + ":" + password).encode()).decode()
s = c.post(f"{BASE}/api/v1/auth/sessions",
headers={"Authorization": "Basic " + credential}).json()
return s["user"]["id"], s["token"]
def main():
with httpx.Client(timeout=10) as c:
alice_id, alice_token = register_and_login(c)
chat = c.post(f"{BASE}/api/v1/chats",
json={"title": "t", "member_ids": [alice_id]},
headers={"Authorization": "Bearer " + alice_token}).json()
r = c.post(
f"{BASE}/api/v1/chats/{chat['id']}/messages",
json={"text": " "},
headers={
"Authorization": "Bearer " + alice_token,
"Idempotency-Key": uuid.uuid4().hex,
},
)
print("blank text:", r.status_code, r.text[:200])
r = c.post(
f"{BASE}/api/v1/chats/{chat['id']}/messages",
json={"text": "hi", "sender_id": "extra"},
headers={
"Authorization": "Bearer " + alice_token,
"Idempotency-Key": uuid.uuid4().hex,
},
)
print("extra sender_id:", r.status_code, r.text[:200])
r = c.post(
f"{BASE}/api/v1/users",
json={"username": " ", "display_name": "X", "password": "Test-password-1234"},
)
print("blank username:", r.status_code, r.text[:200])
r = c.post(
f"{BASE}/api/v1/chats",
json={"title": " ", "member_ids": [alice_id]},
headers={"Authorization": "Bearer " + alice_token},
)
print("blank chat title:", r.status_code, r.text[:200])
if __name__ == "__main__":
main()
+54
View File
@@ -0,0 +1,54 @@
#!/usr/bin/env python3
"""Offline structural checks; full schema validation is a separate target."""
import ast
import json
from pathlib import Path
import re
ROOT = Path(__file__).resolve().parents[1]
def main():
lab = json.loads((ROOT/'lab.json').read_text())['number']
spec = json.loads((ROOT/'contracts/openapi.json').read_text())
assert spec['openapi']=='3.1.0'
assert spec['info']['version']=='1.%d.0'%lab
operation_ids = set()
def walk(value):
if isinstance(value,dict):
if '$ref' in value:
pointer = value['$ref']
assert pointer.startswith('#/'), 'unexpected external reference'
target = spec
for part in pointer[2:].split('/'):
target = target[part.replace('~1','/').replace('~0','~')]
for v in value.values():
walk(v)
elif isinstance(value,list):
for v in value:
walk(v)
walk(spec)
for route,methods in spec['paths'].items():
placeholders = set(re.findall(r'\{([^}]+)\}',route))
for method,op in methods.items():
assert op['operationId'] not in operation_ids, 'duplicate operationId'
operation_ids.add(op['operationId'])
params = {p['name'] for p in op.get('parameters',[]) if p['in']=='path' and p.get('required')}
assert placeholders==params, 'path parameter mismatch: '+route
for response in op['responses'].values():
assert 'X-Request-Id' in response['headers']
if lab>=3:
assert 'X-Instance-Id' in response['headers']
assert ('/api/v1/auth/sessions' in spec['paths']) == (lab>=2)
assert ('/api/v1/attachments/{attachment_id}' in spec['paths']) == (lab>=4)
for file in list((ROOT/'tests').glob('*.py'))+list((ROOT/'scripts').glob('*.py')):
ast.parse(file.read_text(),filename=str(file))
for file in ROOT.rglob('*.json'):
if any(part in {'.git','.venv','node_modules','vendor','output'} for part in file.parts):
continue
json.loads(file.read_text())
for required in ('README.md','COURSE.md','CONTRIBUTING.md','REPORT.md','contracts/README.md','tests/smoke.py','tests/README.md'):
assert (ROOT/required).is_file(), 'missing '+required
print('PASS: offline skeleton structure, local refs and Python syntax; application NOT tested.')
if __name__=='__main__':
main()
+230
View File
@@ -0,0 +1,230 @@
#!/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())
+201
View File
@@ -0,0 +1,201 @@
#!/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())
+75
View File
@@ -0,0 +1,75 @@
#!/usr/bin/env python3
from pathlib import Path
import plotly.graph_objects as go
ROOT = Path(__file__).resolve().parent.parent
EVIDENCE = ROOT / "evidence"
def make_rps_chart():
fig = go.Figure()
fig.add_trace(go.Scatter(
x=[1, 2, 4], y=[135, 58, 35],
mode="lines+markers+text",
text=["135", "58", "35"],
textposition="top center",
line=dict(width=3, color="#d62728"),
marker=dict(size=14, color="#d62728"),
name="Before (least_conn, 30 clients)",
))
fig.add_trace(go.Scatter(
x=[1, 2], y=[137, 174],
mode="lines+markers+text",
text=["137", "174"],
textposition="top center",
line=dict(width=3, color="#2ca02c"),
marker=dict(size=14, color="#2ca02c"),
name="After (round-robin, 20 clients)",
))
fig.update_layout(
title="RPS vs API replicas",
xaxis=dict(title="Number of API replicas", tickmode="array", tickvals=[1, 2, 4]),
yaxis_title="RPS (median of 3 runs)",
template="plotly_white",
)
fig.write_image(str(EVIDENCE / "scaling-rps.png"))
def make_p95_chart():
fig = go.Figure()
fig.add_trace(go.Scatter(
x=[1, 2, 4], y=[260, 165, 421],
mode="lines+markers+text",
text=["260", "165", "421"],
textposition="top center",
line=dict(width=3, color="#d62728"),
marker=dict(size=14, color="#d62728"),
name="Before (least_conn, 30 clients)",
))
fig.add_trace(go.Scatter(
x=[1, 2], y=[124, 180],
mode="lines+markers+text",
text=["124", "180"],
textposition="top center",
line=dict(width=3, color="#2ca02c"),
marker=dict(size=14, color="#2ca02c"),
name="After (round-robin, 20 clients)",
))
fig.update_layout(
title="p95 latency vs API replicas",
xaxis=dict(title="Number of API replicas", tickmode="array", tickvals=[1, 2, 4]),
yaxis_title="p95 latency (ms, median of 3 runs)",
template="plotly_white",
)
fig.write_image(str(EVIDENCE / "scaling-p95.png"))
def main():
make_rps_chart()
make_p95_chart()
print(f"Saved charts to {EVIDENCE}")
if __name__ == "__main__":
main()
+144
View File
@@ -0,0 +1,144 @@
#!/usr/bin/env python3
"""Compare proxy vs presigned upload: API RAM peak and bytes through API."""
import base64
import io
import subprocess
import threading
import time
import uuid
import httpx
from PIL import Image
BASE = "http://localhost:8080"
SIZES = [1 * 1024 * 1024, 9 * 1024 * 1024]
def _png(size_bytes):
img = Image.new("RGB", (1000, 1000), (0, 0, 255))
buf = io.BytesIO()
img.save(buf, format="PNG")
data = buf.getvalue()
if len(data) < size_bytes:
data = data + b"\x00" * (size_bytes - len(data))
return data[:size_bytes]
def _user(c):
u = "cmp_" + uuid.uuid4().hex[:8]
p = "Test-password-1234"
c.post(f"{BASE}/api/v1/users", json={"username": u, "display_name": "C", "password": p})
cred = base64.b64encode(f"{u}:{p}".encode()).decode()
s = c.post(f"{BASE}/api/v1/auth/sessions", headers={"Authorization": "Basic " + cred}).json()
return {"id": s["user"]["id"], "token": s["token"]}
def _chat(c, a, b):
return c.post(
f"{BASE}/api/v1/chats",
json={"title": "cmp", "member_ids": [a["id"], b["id"]]},
headers={"Authorization": "Bearer " + a["token"]},
).json()
def _mem(container):
out = subprocess.run(
["docker", "stats", "--no-stream", "--format", "{{.MemUsage}}", container],
capture_output=True, text=True, check=True,
).stdout.strip()
v = out.split("/")[0].strip()
units = {
"B": 1,
"KiB": 1024, "Ki": 1024,
"MiB": 1024**2, "Mi": 1024**2,
"GiB": 1024**3, "Gi": 1024**3,
}
for u in sorted(units, key=len, reverse=True):
if v.endswith(u):
return int(float(v[: -len(u)]) * units[u])
return 0
def _measure(container, fn):
baseline = _mem(container)
peak = baseline
stop = {"flag": False}
def sampler():
nonlocal peak
while not stop["flag"]:
try:
m = _mem(container)
if m > peak:
peak = m
except Exception:
pass
time.sleep(0.15)
t = threading.Thread(target=sampler)
t.start()
started = time.monotonic()
fn()
elapsed = time.monotonic() - started
stop["flag"] = True
t.join(timeout=5)
return baseline, peak, elapsed
def main():
containers = subprocess.run(
["docker", "ps", "--filter", "name=mtusi2026lab4-api-", "--format", "{{.Names}}"],
capture_output=True, text=True, check=True,
).stdout.split()
if not containers:
raise SystemExit("no api containers found")
target = containers[0]
with httpx.Client(timeout=60) as c:
a = _user(c)
b = _user(c)
chat = _chat(c, a, b)
for size in SIZES:
data = _png(size)
def proxy_upload():
with httpx.Client(timeout=60) as c:
r = c.post(
f"{BASE}/api/v1/chats/{chat['id']}/attachments",
params={"filename": "x.png"},
content=data,
headers={"Authorization": "Bearer " + a["token"], "Content-Type": "image/png"},
)
assert r.status_code == 201, r.text
def presigned_upload():
with httpx.Client(timeout=60) as c:
r = c.post(
f"{BASE}/api/v1/chats/{chat['id']}/attachments/presign",
params={"filename": "x.png", "size": len(data), "content_type": "image/png"},
headers={"Authorization": "Bearer " + a["token"]},
)
assert r.status_code == 201, r.text
p = r.json()
put = httpx.put(p["upload_url"], content=data, headers={"Content-Type": "image/png"}, timeout=60)
assert put.status_code == 200, put.text
r = c.post(
f"{BASE}/api/v1/attachments/{p['attachment_id']}/complete",
headers={"Authorization": "Bearer " + a["token"]},
)
assert r.status_code == 201, r.text
for label, fn in (("proxy", proxy_upload), ("presigned", presigned_upload)):
baseline, peak, elapsed = _measure(target, fn)
print(
f"size={size} mode={label} "
f"elapsed={elapsed:.2f}s "
f"api_ram_baseline={baseline/1024**2:.1f}MiB "
f"api_ram_peak={peak/1024**2:.1f}MiB "
f"delta={(peak-baseline)/1024**2:.1f}MiB"
)
if __name__ == "__main__":
main()
+137
View File
@@ -0,0 +1,137 @@
#!/usr/bin/env python3
"""Measure API RAM peak under >=10 concurrent uploads via docker stats."""
import base64
import io
import subprocess
import threading
import time
import uuid
import httpx
from PIL import Image
BASE = "http://localhost:8080"
CONCURRENCY = 10
UPLOAD_BYTES = 9 * 1024 * 1024
def _png(size_bytes: int) -> bytes:
img = Image.new("RGB", (1000, 1000), (255, 0, 0))
buf = io.BytesIO()
img.save(buf, format="PNG")
data = buf.getvalue()
if len(data) < size_bytes:
data = data + b"\x00" * (size_bytes - len(data))
return data[:size_bytes]
def _register_and_login(c):
u = "ram_" + uuid.uuid4().hex[:8]
p = "Test-password-1234"
r = c.post(f"{BASE}/api/v1/users", json={"username": u, "display_name": "R", "password": p})
assert r.status_code == 201, r.text
user = r.json()
cred = base64.b64encode(f"{u}:{p}".encode()).decode()
s = c.post(f"{BASE}/api/v1/auth/sessions", headers={"Authorization": "Basic " + cred}).json()
return {"id": user["id"], "token": s["token"]}
def _stats_mem(container: str) -> int:
out = subprocess.run(
["docker", "stats", "--no-stream", "--format", "{{.MemUsage}}", container],
capture_output=True, text=True, check=True,
).stdout.strip()
value = out.split("/")[0].strip()
units = {
"B": 1,
"KiB": 1024, "Ki": 1024,
"MiB": 1024**2, "Mi": 1024**2,
"GiB": 1024**3, "Gi": 1024**3,
}
for u in sorted(units, key=len, reverse=True):
if value.endswith(u):
return int(float(value[: -len(u)]) * units[u])
raise ValueError(f"unexpected docker stats format: {out!r}")
def main():
data = _png(UPLOAD_BYTES)
with httpx.Client(timeout=30) as c:
alice = _register_and_login(c)
bob = _register_and_login(c)
chat = c.post(
f"{BASE}/api/v1/chats",
json={"title": "ram", "member_ids": [alice["id"], bob["id"]]},
headers={"Authorization": "Bearer " + alice["token"]},
).json()
containers = subprocess.run(
["docker", "ps", "--filter", "name=mtusi2026lab4-api-", "--format", "{{.Names}}"],
capture_output=True, text=True, check=True,
).stdout.split()
if not containers:
raise SystemExit("no api containers found")
target = containers[0]
baseline = _stats_mem(target)
errors = []
lock = threading.Lock()
def worker():
try:
with httpx.Client(timeout=60) as c:
r = c.post(
f"{BASE}/api/v1/chats/{chat['id']}/attachments",
params={"filename": "ram.png"},
content=data,
headers={
"Authorization": "Bearer " + alice["token"],
"Content-Type": "image/png",
},
)
if r.status_code != 201:
with lock:
errors.append((r.status_code, r.text[:200]))
except Exception as e:
with lock:
errors.append((0, str(e)))
threads = [threading.Thread(target=worker) for _ in range(CONCURRENCY)]
peak = baseline
stop = {"flag": False}
def sampler():
nonlocal peak
while not stop["flag"]:
try:
m = _stats_mem(target)
if m > peak:
peak = m
except Exception:
pass
time.sleep(0.2)
sampler_thread = threading.Thread(target=sampler)
sampler_thread.start()
started = time.monotonic()
for t in threads:
t.start()
for t in threads:
t.join()
elapsed = time.monotonic() - started
stop["flag"] = True
sampler_thread.join(timeout=5)
print(f"container: {target}")
print(f"uploads: {CONCURRENCY} x {UPLOAD_BYTES} bytes")
print(f"elapsed: {elapsed:.2f}s")
print(f"errors: {len(errors)}")
for e in errors:
print(" error:", e)
print(f"baseline RAM: {baseline / 1024**2:.1f} MiB")
print(f"peak RAM: {peak / 1024**2:.1f} MiB")
print(f"delta: {(peak - baseline) / 1024**2:.1f} MiB")
if __name__ == "__main__":
main()
+75
View File
@@ -0,0 +1,75 @@
#!/usr/bin/env python3
"""Find and clean orphan S3 objects older than GRACE; log metadata without object.
Usage (inside api container):
docker compose exec -T api-1 python /tmp/reconcile.py --dry-run
docker compose exec -T api-1 python /tmp/reconcile.py
"""
import argparse
import asyncio
import os
from datetime import datetime, timedelta, timezone
import asyncpg
from minio import Minio
GRACE_HOURS = 1
BUCKET = os.getenv("S3_BUCKET", "attachments")
DB_URL = os.getenv(
"DATABASE_URL",
"postgresql://messenger:messenger@postgres:5432/messenger",
)
S3 = Minio(
os.getenv("S3_ENDPOINT", "minio:9000"),
access_key=os.getenv("S3_ACCESS_KEY", "api-user"),
secret_key=os.getenv("S3_SECRET_KEY", "api-user-secret-123"),
secure=False,
)
async def main(dry_run: bool) -> int:
pool = await asyncpg.create_pool(DB_URL)
try:
async with pool.acquire() as conn:
rows = await conn.fetch("SELECT object_key FROM attachments")
known = {r["object_key"] for r in rows}
cutoff = datetime.now(timezone.utc) - timedelta(hours=GRACE_HOURS)
removed = 0
skipped_fresh = 0
for obj in S3.list_objects(BUCKET, recursive=True):
if obj.object_name in known:
continue
if obj.last_modified and obj.last_modified > cutoff:
skipped_fresh += 1
continue
print(f"orphan: {obj.object_name}")
if not dry_run:
S3.remove_object(BUCKET, obj.object_name)
removed += 1
missing = 0
async with pool.acquire() as conn:
rows = await conn.fetch("SELECT id, object_key FROM attachments")
for r in rows:
try:
S3.stat_object(BUCKET, r["object_key"])
except Exception:
missing += 1
print(f"missing object for attachment {r['id']}: {r['object_key']}")
mode = "dry-run" if dry_run else "apply"
print(
f"reconcile ({mode}): orphans_found={removed} "
f"fresh_skipped={skipped_fresh} metadata_without_object={missing}"
)
return 0
finally:
await pool.close()
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--dry-run", action="store_true")
args = parser.parse_args()
raise SystemExit(asyncio.run(main(args.dry_run)))
+2
View File
@@ -0,0 +1,2 @@
@echo off
python "%~dp0restart.py" %*
+10
View File
@@ -0,0 +1,10 @@
#!/bin/sh
set -eu
# TODO: copy to restart.sh and implement using YOUR compose/stack services.
# Restart API and ALL used state stores (DB, sessions, S3, broker as applicable).
# Preserve persistent volumes and schema/data; never down -v / flush / re-seed.
# Allow operator unseal if required in lab7; do not print credentials.
# Exit 0 only after the intended action; test waits for API readiness separately.
printf '%s
' 'TODO: implement restart.sh for your infrastructure' >&2
exit 2
+50
View File
@@ -0,0 +1,50 @@
#!/usr/bin/env python3
import subprocess
import sys
import time
import urllib.request
from pathlib import Path
ROOT = Path(__file__).resolve().parent.parent
BASE_URL = "http://localhost:8080"
def run(cmd):
print(f"[restart] $ {' '.join(cmd)}", flush=True)
result = subprocess.run(cmd, cwd=ROOT)
if result.returncode != 0:
print(
f"[restart] command failed with code {result.returncode}",
file=sys.stderr,
)
sys.exit(result.returncode)
def main():
run(["docker", "compose", "--env-file", ".env", "down"])
run([
"docker", "compose", "--env-file", ".env",
"up", "-d", "--build", "--scale", "api=2",
])
print("[restart] waiting for /health/ready", flush=True)
deadline = time.monotonic() + 90
while time.monotonic() < deadline:
try:
with urllib.request.urlopen(
f"{BASE_URL}/health/ready", timeout=2
) as r:
if r.status == 200:
print("[restart] api is ready", flush=True)
return 0
except Exception:
time.sleep(1)
print(
"[restart] api did not become ready in 90 seconds",
file=sys.stderr,
)
return 1
if __name__ == "__main__":
sys.exit(main())
+22
View File
@@ -0,0 +1,22 @@
#!/usr/bin/env bash
set -euo pipefail
cd "$(dirname "$0")/.."
echo "[restart.sh] docker compose down (volumes preserved)"
docker compose --env-file .env down
echo "[restart.sh] docker compose up -d --build --scale api=2"
docker compose --env-file .env up -d --build --scale api=2
echo "[restart.sh] waiting for /health/ready"
for i in $(seq 1 90); do
if curl -fsS http://localhost:8080/health/ready >/dev/null 2>&1; then
echo "[restart.sh] api is ready"
exit 0
fi
sleep 1
done
echo "[restart.sh] api did not become ready in 90 seconds" >&2
exit 1
+111
View File
@@ -0,0 +1,111 @@
#!/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())
+194
View File
@@ -0,0 +1,194 @@
#!/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())
+2
View File
@@ -0,0 +1,2 @@
@echo off
python "%~dp0stop-one.py" %*
+10
View File
@@ -0,0 +1,10 @@
#!/bin/sh
set -eu
# TODO: copy to stop-one.sh, map TARGET_INSTANCE_ID to ONE API container/task.
# Stop that instance and leave it stopped; no stop of DB/LB/volumes.
# Return quickly (within 10 s); the test sends requests during this script.
# Restore the replica explicitly after the test. Never print credentials.
: "${TARGET_INSTANCE_ID:?test provides target API instance alias}"
printf '%s
' 'TODO: implement stop-one.sh for your infrastructure' >&2
exit 2
+34
View File
@@ -0,0 +1,34 @@
#!/usr/bin/env python3
import os
import subprocess
import sys
def main():
target = os.environ.get("TARGET_INSTANCE_ID")
if not target:
print("[stop-one] TARGET_INSTANCE_ID not set", file=sys.stderr)
return 1
names = subprocess.run(
["docker", "ps", "--filter", "name=mtusi2026lab4-api-", "--format", "{{.Names}}"],
capture_output=True, text=True, check=True,
).stdout.split()
for container in names:
logs = subprocess.run(
["docker", "logs", container],
capture_output=True, text=True, check=False,
)
combined = logs.stdout + logs.stderr
if f"INSTANCE_ID={target}" in combined:
print(f"[stop-one] stopping {container} (instance={target})", flush=True)
subprocess.run(["docker", "stop", "--time=2", container], check=True)
return 0
print(f"[stop-one] no container found for INSTANCE_ID={target}", file=sys.stderr)
return 1
if __name__ == "__main__":
sys.exit(main())
+15
View File
@@ -0,0 +1,15 @@
#!/usr/bin/env bash
set -euo pipefail
TARGET="${TARGET_INSTANCE_ID:?TARGET_INSTANCE_ID not set}"
for container in $(docker ps --filter "name=mtusi2026lab4-api-" --format "{{.Names}}"); do
if docker logs "$container" 2>&1 | grep -q "INSTANCE_ID=$TARGET"; then
echo "[stop-one] stopping $container (instance=$TARGET)"
docker stop "$container"
exit 0
fi
done
echo "[stop-one] no container found for INSTANCE_ID=$TARGET" >&2
exit 1
+105
View File
@@ -0,0 +1,105 @@
#!/usr/bin/env python3
import base64
import json
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" / "idempotency.json"
def register_and_login(client):
username = "idem_" + uuid.uuid4().hex[:10]
password = "Test-password-" + uuid.uuid4().hex[:8]
r = client.post(
f"{BASE}/api/v1/users",
json={"username": username, "display_name": "I", "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=10) as c:
alice = register_and_login(c)
bob = register_and_login(c)
chat = c.post(
f"{BASE}/api/v1/chats",
json={"title": "idem", "member_ids": [alice["id"], bob["id"]]},
headers={"Authorization": "Bearer " + alice["token"]},
).json()
key = uuid.uuid4().hex
text = "idempotency-test-" + uuid.uuid4().hex[:8]
results = []
lock = threading.Lock()
barrier = threading.Barrier(10)
def worker():
try:
with httpx.Client(timeout=10) as c:
barrier.wait()
r = c.post(
f"{BASE}/api/v1/chats/{chat['id']}/messages",
json={"text": text},
headers={
"Authorization": "Bearer " + alice["token"],
"Idempotency-Key": key,
},
)
with lock:
results.append((r.status_code, r.headers.get("x-instance-id")))
except Exception as e:
with lock:
results.append((0, str(e)))
threads = [threading.Thread(target=worker) for _ in range(10)]
for t in threads:
t.start()
for t in threads:
t.join()
time.sleep(1)
with httpx.Client(timeout=10) as c:
page = c.get(
f"{BASE}/api/v1/chats/{chat['id']}/messages?limit=100",
headers={"Authorization": "Bearer " + alice["token"]},
).json()
matching = [m for m in page["items"] if m["text"] == text]
message_ids = {m["id"] for m in matching}
instances_seen = list({r[1] for r in results if r[1] and r[0] > 0})
statuses = [r[0] for r in results]
report = {
"concurrent_requests": len(results),
"statuses": statuses,
"unique_instances": instances_seen,
"messages_created": len(matching),
"unique_message_ids": len(message_ids),
"idempotency_ok": len(matching) == 1,
"instances_count": len(instances_seen),
}
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())
+23
View File
@@ -0,0 +1,23 @@
#!/usr/bin/env python3
"""Full OpenAPI and JSON Schema checks; install requirements-dev.txt first."""
import json
from pathlib import Path
from openapi_spec_validator import validate
from jsonschema import Draft202012Validator
root = Path(__file__).resolve().parents[1]
spec = json.loads((root/'contracts/openapi.json').read_text())
validate(spec)
from referencing import Registry, Resource
# The base URI resolves local #/components references inside examples.
resource = dict(spec, **{'$schema': 'https://json-schema.org/draft/2020-12/schema'})
registry = Registry().with_resource('urn:mtusi:openapi', Resource.from_contents(resource))
for name, example in json.loads((root/'contracts/examples.json').read_text()).items():
schema = {'$ref': 'urn:mtusi:openapi#/components/schemas/' + name}
Draft202012Validator(schema, registry=registry).validate(example)
for path in (root/'contracts').glob('*.schema.json'):
schema = json.loads(path.read_text())
Draft202012Validator.check_schema(schema)
for example in schema.get('examples',[]):
Draft202012Validator(schema).validate(example)
print('PASS: full OpenAPI 3.1 and JSON Schema validation; application NOT tested.')