137 lines
4.1 KiB
Python
137 lines
4.1 KiB
Python
#!/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() |