import io import json import os import struct import sys import threading import time import zlib from concurrent.futures import ThreadPoolExecutor from hashlib import sha256 from pathlib import Path from PIL import Image ROOT = Path(__file__).resolve().parents[2] sys.path.insert(0, str(ROOT / "scripts")) import lab4_seed as seed import upload_memory as memory out = Path(__file__).with_name("high-depth.json") assert not out.exists() compressor = zlib.compressobj(9) row = bytes(1 + 8000 * 8) compressed = b"".join(compressor.compress(row) for _ in range(5000)) + compressor.flush() body = ( b"\x89PNG\r\n\x1a\n" + seed.chunk(b"IHDR", struct.pack(">IIBBBBB", 8000, 5000, 16, 6, 0, 0, 0)) + seed.chunk(b"IDAT", compressed) + seed.chunk(b"IEND", b"") ) with Image.open(io.BytesIO(body)) as decoded: decoded.load() assert decoded.size == (8000, 5000) digest = sha256(body).hexdigest() memory.recreate(3) api = seed.Http(os.environ["BASE_URL"], 60) alice, token = seed.login(api, "high_alice") bob, bob_token = seed.login(api, "high_bob") status, _, data = api.call( "POST", "/api/v1/chats", {"title": "high-depth", "member_ids": [alice, bob]}, token=token ) assert status == 201 chat = json.loads(data)["id"] containers = seed.run(["docker", "compose", "ps", "-q", "api"]).split() idle = {cid: int(seed.cgroup(cid, "memory.current")) for cid in containers} barrier = threading.Barrier(10) def upload(index): barrier.wait() started = time.monotonic() status, headers, data = api.call( "POST", f"/api/v1/chats/{chat}/attachments?filename=rgba16-{index}.png", body, {"Content-Type": "image/png"}, token, ) return { "status": status, "seconds": round(time.monotonic() - started, 3), "instance": headers.get("x-instance-id"), "response": json.loads(data) if data else None, } with ThreadPoolExecutor(max_workers=10) as workers: results = list(workers.map(upload, range(10))) peaks = { cid[:12]: { "idle_bytes": idle[cid], "peak_bytes": int(seed.cgroup(cid, "memory.peak")), "limit_bytes": int(seed.cgroup(cid, "memory.max")), "events": dict(row.split() for row in seed.cgroup(cid, "memory.events").splitlines()), } for cid in containers } for result in results: if result["status"] == 201: attachment = result["response"] status, headers, downloaded = api.call( "GET", f"/api/v1/attachments/{attachment['id']}/content", token=bob_token ) result["download"] = { "status": status, "sha256_matches": sha256(downloaded).hexdigest() == digest, "bytes": len(downloaded), "content_type": headers.get("content-type"), } report = { "date": time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime()), "commit": seed.run(["git", "rev-parse", "HEAD"]).strip(), "width": 8000, "height": 5000, "bit_depth": 16, "color_type": 6, "decompressed_bytes": len(row) * 5000, "body_bytes": len(body), "sha256": digest, "independent_pillow_load": True, "concurrency": 10, "upload_slots_per_worker": 3, "results": results, "memory": peaks, "lb": memory.balancer(), } report["ok"] = all( r["status"] == 201 and r.get("download", {}).get("sha256_matches") for r in results ) and all( p["peak_bytes"] < p["limit_bytes"] and int(p["events"]["oom_kill"]) == 0 for p in peaks.values() ) out.write_text(json.dumps(report, ensure_ascii=False, indent=2) + "\n") print( json.dumps({k: report[k] for k in ("ok", "body_bytes", "decompressed_bytes", "memory", "lb")}) ) sys.exit(0 if report["ok"] else 1)