#!/usr/bin/env python3 """Concurrent real PNG uploads with Docker API-container memory sampling.""" import concurrent.futures import json import os import struct import subprocess import threading import time import uuid import zlib from client import Client, check, run def png_chunk(kind, payload): return struct.pack(">I", len(payload)) + kind + payload + struct.pack(">I", zlib.crc32(kind + payload)) def noisy_png(width=2048, height=1024): rows = bytearray() for _ in range(height): rows.append(0) rows.extend(os.urandom(width * 3)) return ( b"\x89PNG\r\n\x1a\n" + png_chunk(b"IHDR", struct.pack(">IIBBBBB", width, height, 8, 2, 0, 0, 0)) + png_chunk(b"IDAT", zlib.compress(bytes(rows), level=0)) + png_chunk(b"IEND", b"") ) def bytes_from_docker(value): number, unit = value.split()[0], value.split()[0][-3:] if unit in {"KiB", "MiB", "GiB"}: number = value.split()[0][:-3] else: unit = "B" factor = {"B": 1, "KiB": 1024, "MiB": 1024**2, "GiB": 1024**3}[unit] return int(float(number) * factor) def api_container_ids(): result = subprocess.run( ["docker", "compose", "ps", "-q", "api"], check=True, text=True, capture_output=True, ) return [line for line in result.stdout.splitlines() if line] def sample_memory(container_ids, stop_event, peaks): while not stop_event.is_set(): result = subprocess.run( ["docker", "stats", "--no-stream", "--format", "{{.Name}}\t{{.MemUsage}}", *container_ids], check=False, text=True, capture_output=True, ) if result.returncode == 0: for line in result.stdout.splitlines(): name, usage = line.split("\t", 1) current = bytes_from_docker(usage.split(" / ", 1)[0]) peaks[name] = max(peaks.get(name, 0), current) stop_event.wait(0.05) def cleanup(chat_id, user_ids): object_script = ( 'root_user="$(cat /run/secrets/minio_root_user)"\n' 'root_password="$(cat /run/secrets/minio_root_password)"\n' 'mc alias set local http://minio:9000 "$root_user" "$root_password" --api S3v4 >/dev/null\n' 'mc rm --recursive --force "local/${S3_BUCKET}/attachments/' + chat_id + '/" >/dev/null 2>&1 || true\n' ) subprocess.run( ["docker", "compose", "run", "--rm", "--entrypoint", "/bin/sh", "minio-bootstrap", "-ec", object_script], check=True, ) quoted_users = ",".join("'%s'" % value for value in user_ids) sql = f""" BEGIN; DELETE FROM messages WHERE chat_id = '{chat_id}'; DELETE FROM attachments WHERE chat_id = '{chat_id}'; DELETE FROM sessions WHERE user_id IN ({quoted_users}); DELETE FROM chat_members WHERE chat_id = '{chat_id}'; DELETE FROM chats WHERE id = '{chat_id}'; DELETE FROM users WHERE id IN ({quoted_users}); COMMIT; """ subprocess.run( ["docker", "compose", "exec", "-T", "postgres", "psql", "-v", "ON_ERROR_STOP=1", "-U", "messenger", "-d", "messenger"], input=sql, text=True, check=True, ) def main(): concurrency = 12 base_url = os.environ.get("BASE_URL", "http://127.0.0.1:8080") client = Client(base_url, None, timeout=30) alice, bob, eve, chat = client.bootstrap() users = [alice["id"], bob["id"], eve["id"]] payload = noisy_png() stop_event = threading.Event() peaks = {} sampler = threading.Thread( target=sample_memory, args=(api_container_ids(), stop_event, peaks), daemon=True, ) sampler.start() started = time.monotonic() def upload(index): response = client.call( "POST", "/api/v1/chats/%s/attachments?filename=memory-%02d.png" % (chat["id"], index), 201, actor=alice, raw=payload, headers={"Content-Type": "image/png", "X-Load-Id": uuid.uuid4().hex}, schema="Attachment", ) return response["headers"]["X-Instance-Id"] try: with concurrent.futures.ThreadPoolExecutor(max_workers=concurrency) as pool: instances = list(pool.map(upload, range(concurrency))) elapsed = time.monotonic() - started stop_event.set() sampler.join(timeout=10) check(len(set(instances)) >= 2, "uploads did not exercise both API replicas") check(len(peaks) >= 2, "memory was not sampled for both API replicas") summary = { "concurrency": concurrency, "payload_bytes": len(payload), "total_uploaded_bytes": len(payload) * concurrency, "elapsed_seconds": round(elapsed, 3), "successful_uploads": len(instances), "api_instances_observed": len(set(instances)), "peak_memory_bytes_by_container": peaks, } print(json.dumps(summary, ensure_ascii=False, sort_keys=True)) print("PASS: concurrent attachment uploads remained within configured API memory limits.") finally: stop_event.set() sampler.join(timeout=10) cleanup(chat["id"], users) if __name__ == "__main__": run(main)