import json, os, subprocess, struct, sys, threading, time, urllib.request, urllib.error, uuid, zlib, base64 BASE = "http://localhost:8080"; PWD = "correct-horse-battery-staple" SIZE = int(sys.argv[1]) if len(sys.argv) > 1 else 9 * 1024 * 1024 WORKERS, DURATION = 12, 20 def chunk(t, d): c = struct.pack(">I", len(d)) + t + d return c + struct.pack(">I", zlib.crc32(t + d) & 0xFFFFFFFF) def noise_png(target): w = 1000 rows = max(1, target // (3 * w + 1)) raw = b"".join(b"\x00" + os.urandom(3 * w) for _ in range(rows)) return (b"\x89PNG\r\n\x1a\n" + chunk(b"IHDR", struct.pack(">IIBBBBB", w, rows, 8, 2, 0, 0, 0)) + chunk(b"IDAT", zlib.compress(raw, 0)) + chunk(b"IEND", b"")) def call(method, path, body=None, headers=None): req = urllib.request.Request(BASE + path, data=body, method=method, headers=headers or {}) try: with urllib.request.urlopen(req, timeout=60) as r: return r.status, r.read() except urllib.error.HTTPError as e: return e.code, e.read() name = "mem_" + uuid.uuid4().hex[:8] _, raw = call("POST", "/api/v1/users", json.dumps({"username": name, "display_name": "m", "password": PWD}).encode(), {"Content-Type": "application/json"}) user = json.loads(raw) _, raw = call("POST", "/api/v1/auth/sessions", None, {"Authorization": "Basic " + base64.b64encode(f"{name}:{PWD}".encode()).decode()}) tok = json.loads(raw)["token"] _, raw = call("POST", "/api/v1/chats", json.dumps({"title": "m", "member_ids": [user["id"]]}).encode(), {"Content-Type": "application/json", "Authorization": "Bearer " + tok}) chat = json.loads(raw) data = noise_png(SIZE) print(f"payload: {len(data)} bytes, workers={WORKERS}, duration={DURATION}s") statuses, lock, stop = {}, threading.Lock(), threading.Event() def worker(): while not stop.is_set(): st, _ = call("POST", f"/api/v1/chats/{chat['id']}/attachments?filename=m.png", data, {"Content-Type": "image/png", "Authorization": "Bearer " + tok}) with lock: statuses[st] = statuses.get(st, 0) + 1 def mib(s): n, unit = float(s[:-3]) if s.endswith("iB") else 0, s[-3:] return n * {"KiB": 1 / 1024, "MiB": 1, "GiB": 1024}.get(unit, 0) peaks = {} def sampler(): while not stop.is_set(): out = subprocess.run(["docker", "stats", "--no-stream", "--format", "{{.Name}} {{.MemUsage}}"], capture_output=True, text=True).stdout for line in out.splitlines(): parts = line.split() if len(parts) >= 2 and "-api-" in parts[0]: peaks[parts[0]] = max(peaks.get(parts[0], 0), mib(parts[1])) base = {} out = subprocess.run(["docker", "stats", "--no-stream", "--format", "{{.Name}} {{.MemUsage}}"], capture_output=True, text=True).stdout for line in out.splitlines(): p = line.split() if len(p) >= 2 and "-api-" in p[0]: base[p[0]] = mib(p[1]) ths = [threading.Thread(target=worker) for _ in range(WORKERS)] + [threading.Thread(target=sampler)] t0 = time.time() [t.start() for t in ths] time.sleep(DURATION) stop.set() [t.join() for t in ths] print(json.dumps({"payload_bytes": len(data), "statuses": statuses, "idle_mib": base, "peak_mib": {k: round(v, 1) for k, v in peaks.items()}, "limit_mib": 256}, indent=2))