"""10 одновременных загрузок 10 MiB: все 202, все ready, RAM API. Запуск: python3 student_tests/test_concurrent_uploads.py --base-url http://localhost:8080 """ from __future__ import annotations import base64 import io import json import subprocess import sys import threading import time import urllib.error import urllib.request import uuid from concurrent.futures import ThreadPoolExecutor BASE = ( sys.argv[sys.argv.index("--base-url") + 1] if "--base-url" in sys.argv else "http://localhost:8080" ) CONCURRENCY = 10 SIZE_MB = 10 API_CONTAINER = "mtusi2026lab5-api-1" def call(method, path, headers=None, body=None, raw=None, timeout=120): data = None h = dict(headers or {}) if body is not None: data = json.dumps(body, ensure_ascii=False).encode("utf-8") h["Content-Type"] = "application/json" elif raw is not None: data = raw req = urllib.request.Request(BASE + path, data=data, headers=h, method=method) try: resp = urllib.request.urlopen(req, timeout=timeout) payload = resp.read() return resp.status, json.loads(payload) if payload else None except urllib.error.HTTPError as e: payload = e.read() try: return e.code, json.loads(payload) if payload else None except Exception: return e.code, None def make_user(label): username = f"conc_{label}_{uuid.uuid4().hex[:10]}" password = "conc-pass-1234567890" code, user = call("POST", "/api/v1/users", body={ "username": username, "display_name": f"Conc {label}", "password": password, }) assert code == 201, f"register failed: {code} {user}" cred = base64.b64encode(f"{username}:{password}".encode()).decode() code, session = call("POST", "/api/v1/auth/sessions", headers={"Authorization": "Basic " + cred}) assert code == 201, f"login failed: {code}" code, chat = call("POST", "/api/v1/chats", headers={"Authorization": "Bearer " + session["token"]}, body={"title": "Conc", "member_ids": [user["id"]]}) assert code == 201, f"chat failed: {code}" return {"token": session["token"], "chat_id": chat["id"]} def measure_ram_mb(container): try: result = subprocess.run( ["docker", "stats", "--no-stream", "--format", "{{.MemUsage}}", container], capture_output=True, text=True, timeout=5, ) if result.returncode != 0: return None mem = result.stdout.strip().split("/")[0].strip() if "MiB" in mem: return float(mem.replace("MiB", "").strip()) if "GiB" in mem: return float(mem.replace("GiB", "").strip()) * 1024 if "KiB" in mem: return float(mem.replace("KiB", "").strip()) / 1024 return None except Exception: return None def make_png_of_size(size_bytes): from PIL import Image side = int((size_bytes / 3) ** 0.5) img = Image.new("RGB", (side, side), color=(200, 100, 50)) buf = io.BytesIO() img.save(buf, format="PNG", compress_level=0) return buf.getvalue() def main(): print(f"=== concurrent uploads: {CONCURRENCY} × {SIZE_MB} MiB ===") user = make_user("user") png = make_png_of_size(SIZE_MB * 1024 * 1024) print(f"fixture size: {len(png)} bytes ({len(png) / 1024 / 1024:.2f} MiB)") baseline = measure_ram_mb(API_CONTAINER) print(f"baseline RAM: {baseline:.1f} MiB" if baseline else "baseline RAM: unknown") peak = baseline or 0.0 stop = threading.Event() def monitor(): nonlocal peak while not stop.is_set(): ram = measure_ram_mb(API_CONTAINER) if ram is not None: peak = max(peak, ram) time.sleep(0.2) t = threading.Thread(target=monitor, daemon=True) t.start() results = [] lock = threading.Lock() barrier = threading.Barrier(CONCURRENCY) def worker(i): barrier.wait() code, body = call( "POST", f"/api/v1/chats/{user['chat_id']}/attachments?filename=conc-{i}.png", headers={ "Authorization": "Bearer " + user["token"], "Content-Type": "image/png", }, raw=png, ) with lock: results.append((i, code, body)) started = time.monotonic() with ThreadPoolExecutor(max_workers=CONCURRENCY) as pool: list(pool.map(worker, range(CONCURRENCY))) elapsed = time.monotonic() - started stop.set() t.join(timeout=2) codes = {} for _, code, _ in results: codes[code] = codes.get(code, 0) + 1 print(f"\nUpload results:") print(f" duration: {elapsed:.2f}s") print(f" status codes: {codes}") print(f" peak RAM: {peak:.1f} MiB (baseline {baseline:.1f} MiB)") assert all(c == 202 for _, c, _ in results), f"not all 202: {codes}" # Ждём все ready att_ids = [body["id"] for _, _, body in results] deadline = time.monotonic() + 120 ready_count = 0 while time.monotonic() < deadline: ready_count = 0 for att_id in att_ids: code, att = call("GET", f"/api/v1/attachments/{att_id}", headers={"Authorization": "Bearer " + user["token"]}) if code == 200 and att["status"] == "ready": ready_count += 1 elif code == 200 and att["status"] == "failed": raise AssertionError(f"att {att_id} failed: {att.get('error_code')}") if ready_count == len(att_ids): break time.sleep(1) assert ready_count == len(att_ids), f"only {ready_count}/{len(att_ids)} ready" print(f" all {len(att_ids)} ready in {time.monotonic() - (deadline - 120):.1f}s") print(f"\nPASS: {CONCURRENCY} concurrent uploads 202→ready, peak RAM {peak:.1f} MiB") if __name__ == "__main__": main()