#!/bin/sh # Уровень 4: ≥10 одновременных загрузок + пик RSS API-реплик. set -eu ROOT=$(CDPATH= cd -- "$(dirname "$0")/.." && pwd) cd "$ROOT" export BASE_URL=${BASE_URL:-http://127.0.0.1:8080} export N=${N:-12} export OUT=${OUT:-evidence/concurrent-uploads.txt} mkdir -p evidence python3 - <<'PY' import base64, binascii, json, os, struct, subprocess, sys, threading, time, urllib.request, uuid, zlib base = os.environ["BASE_URL"] n = int(os.environ["N"]) out = os.environ["OUT"] def req(method, path, data=None, headers=None, timeout=60): h = dict(headers or {}) body = data if isinstance(data, (dict, list)): body = json.dumps(data).encode() h.setdefault("Content-Type", "application/json") r = urllib.request.Request(base + path, data=body, headers=h, method=method) with urllib.request.urlopen(r, timeout=timeout) as resp: return resp.status, resp.read() def chunk(tag, data): return struct.pack(">I", len(data)) + tag + data + struct.pack(">I", binascii.crc32(tag + data) & 0xffffffff) # 1024×1024 RGB, zlib level 0 → ~3 MiB: заметнее для RSS, всё ещё ≪ 10 MiB лимита. w = h = 1024 ihdr = struct.pack(">IIBBBBB", w, h, 8, 2, 0, 0, 0) raw = b"".join(b"\x00" + bytes(w * 3) for _ in range(h)) big = b"\x89PNG\r\n\x1a\n" + chunk(b"IHDR", ihdr) + chunk(b"IDAT", zlib.compress(raw, 0)) + chunk(b"IEND", b"") print(f"upload_size_bytes={len(big)}", flush=True) uname = "cu_" + uuid.uuid4().hex[:10] password = "correct-horse-1" st, body = req("POST", "/api/v1/users", {"username": uname, "display_name": "CU", "password": password}) assert st == 201, body uid = json.loads(body)["id"] basic = base64.b64encode(f"{uname}:{password}".encode()).decode() st, body = req("POST", "/api/v1/auth/sessions", headers={"Authorization": f"Basic {basic}"}) assert st == 201, body token = json.loads(body)["token"] st, body = req("POST", "/api/v1/chats", {"title": "concurrent", "member_ids": [uid]}, headers={"Authorization": f"Bearer {token}"}) assert st == 201, body chat = json.loads(body)["id"] def api_rss_kb(): try: ids = subprocess.check_output( ["docker", "compose", "--env-file", ".env", "ps", "-q", "api"], text=True ).split() total = 0 for i in ids: line = subprocess.check_output( ["docker", "stats", "--no-stream", "--format", "{{.MemUsage}}", i], text=True ).strip() part = line.split("/")[0].strip() num = float("".join(ch for ch in part if ch.isdigit() or ch == ".")) if "GiB" in part: total += int(num * 1024 * 1024) elif "MiB" in part: total += int(num * 1024) else: total += int(num) return total except Exception as e: print("rss_err", e, flush=True) return -1 rss_before = api_rss_kb() peak = {"v": rss_before} stop = threading.Event() def monitor(): while not stop.is_set(): v = api_rss_kb() if v > peak["v"]: peak["v"] = v time.sleep(0.2) mon = threading.Thread(target=monitor, daemon=True) mon.start() results = [None] * n barrier = threading.Barrier(n) def worker(i): barrier.wait(60) try: r = urllib.request.Request( f"{base}/api/v1/chats/{chat}/attachments?filename=c{i}.png", data=big, headers={"Authorization": f"Bearer {token}", "Content-Type": "image/png"}, method="POST", ) with urllib.request.urlopen(r, timeout=120) as resp: results[i] = resp.status except Exception as e: results[i] = str(e) threads = [threading.Thread(target=worker, args=(i,)) for i in range(n)] t0 = time.time() for th in threads: th.start() for th in threads: th.join() elapsed = time.time() - t0 stop.set() mon.join(timeout=1) rss_after = api_rss_kb() ok = sum(1 for r in results if r == 201) text = "\n".join([ f"concurrent={n}", f"upload_bytes={len(big)}", f"ok_201={ok}", f"results={results}", f"elapsed_s={elapsed:.3f}", f"api_rss_kb_before={rss_before}", f"api_rss_kb_peak={peak['v']}", f"api_rss_kb_after={rss_after}", ]) + "\n" print(text, end="") open(out, "w").write(text) sys.exit(0 if ok == n else 1) PY echo "wrote $OUT"