#!/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()