#!/usr/bin/env python3 """Grade-4 load check: >=10 concurrent uploads, bounded API memory. Uploads N large valid PNGs in parallel through the balancer while sampling the memory of the API replicas, then prints the measured peak. Files are generated with the standard library only (a valid PNG with stored, not deflated, pixels). Run from the repository root (needs the docker CLI and a running lab4 stand): python3 scripts/check-upload-load.py --uploads 12 --megabytes 6 """ from __future__ import annotations import argparse import base64 import concurrent.futures import json import os import struct import subprocess import threading import time import urllib.error import urllib.request import uuid import zlib def _png_chunk(tag: bytes, data: bytes) -> bytes: return ( struct.pack(">I", len(data)) + tag + data + struct.pack(">I", zlib.crc32(tag + data) & 0xFFFFFFFF) ) def make_png(width: int, height: int) -> bytes: pixels = os.urandom(width * height * 3) raw = bytearray() stride = width * 3 for row in range(height): raw.append(0) # filter type 0 raw += pixels[row * stride : (row + 1) * stride] ihdr = struct.pack(">IIBBBBB", width, height, 8, 2, 0, 0, 0) return ( b"\x89PNG\r\n\x1a\n" + _png_chunk(b"IHDR", ihdr) + _png_chunk(b"IDAT", zlib.compress(bytes(raw), 0)) + _png_chunk(b"IEND", b"") ) def call(base, method, path, token=None, body=None, raw=None, content_type=None): headers = {"Accept": "application/json"} data = raw if body is not None: data = json.dumps(body).encode() headers["Content-Type"] = "application/json" if content_type: headers["Content-Type"] = content_type if token: headers["Authorization"] = "Bearer " + token request = urllib.request.Request(base + path, data=data, headers=headers, method=method) try: with urllib.request.urlopen(request, timeout=60) as response: payload = response.read() return response.status, payload except urllib.error.HTTPError as error: return error.code, error.read() def current_api_memory_mb() -> float: out = subprocess.run( ["docker", "stats", "--no-stream", "--format", "{{.Name}}|{{.MemUsage}}"], capture_output=True, text=True, ).stdout total = 0.0 for line in out.splitlines(): name, _, usage = line.partition("|") if "api" not in name: continue value = usage.split("/")[0].strip() number = "".join(ch for ch in value if ch.isdigit() or ch == ".") if value.endswith("GiB"): total += float(number) * 1024 elif value.endswith("MiB"): total += float(number) elif value.endswith("KiB"): total += float(number) / 1024 return total def main() -> None: parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("--base-url", default=os.environ.get("BASE_URL", "http://localhost:8080")) parser.add_argument("--uploads", type=int, default=12) parser.add_argument("--megabytes", type=int, default=6) args = parser.parse_args() base = args.base_url.rstrip("/") username = "load_" + uuid.uuid4().hex[:10] password = "T3st-" + uuid.uuid4().hex status, _ = call(base, "POST", "/api/v1/users", body={ "username": username, "display_name": "Load", "password": password}) assert status == 201, status credential = base64.b64encode(f"{username}:{password}".encode()).decode() request = urllib.request.Request( base + "/api/v1/auth/sessions", headers={"Authorization": "Basic " + credential, "Accept": "application/json"}, method="POST", ) with urllib.request.urlopen(request, timeout=15) as response: token = json.loads(response.read())["token"] status, user = call(base, "GET", "/api/v1/users/me", token=token) user_id = json.loads(user)["id"] status, chat_bytes = call(base, "POST", "/api/v1/chats", token=token, body={"title": "load", "member_ids": [user_id]}) assert status == 201, status chat_id = json.loads(chat_bytes)["id"] side = int((args.megabytes * 1024 * 1024 / 3) ** 0.5) payload = make_png(side, side) print(json.dumps({"file_bytes": len(payload), "uploads": args.uploads})) peak = {"value": current_api_memory_mb()} stop = threading.Event() def sampler(): while not stop.is_set(): peak["value"] = max(peak["value"], current_api_memory_mb()) time.sleep(0.2) thread = threading.Thread(target=sampler, daemon=True) thread.start() started = time.time() results = [] def upload(_): return call(base, "POST", f"/api/v1/chats/{chat_id}/attachments?filename=load.png", token=token, raw=payload, content_type="image/png") with concurrent.futures.ThreadPoolExecutor(max_workers=args.uploads) as pool: for status_code, body in pool.map(upload, range(args.uploads)): results.append(status_code) stop.set() thread.join(timeout=2) elapsed = time.time() - started print(json.dumps({ "ok": results.count(201), "errors": [s for s in results if s != 201], "seconds": round(elapsed, 2), "api_peak_mem_mb": round(peak["value"], 1), }, ensure_ascii=False)) if __name__ == "__main__": main()