#!/usr/bin/env python3 """Ten concurrent uploads with Docker API memory sampling (local evidence only).""" import argparse import json import re import subprocess import sys import threading import time from concurrent.futures import ThreadPoolExecutor from pathlib import Path ROOT = Path(__file__).resolve().parents[1] sys.path.insert(0, str(ROOT / "tests")) from client import Client, Failure def stats(): values = [] try: output = subprocess.check_output( ["docker", "stats", "--no-stream", "--format", "{{.Name}}|{{.MemUsage}}"], text=True, stderr=subprocess.DEVNULL) for line in output.splitlines(): if "api-" not in line or " / " not in line: continue used = line.split("|", 1)[1].split(" / ", 1)[0].strip() match = re.match(r"([0-9.]+)\s*(MiB|GiB|MB|GB)", used) if match: value = float(match.group(1)) * (1024 if match.group(2) in ("GiB", "GB") else 1) values.append(value) except (OSError, subprocess.CalledProcessError): pass return values def main(): parser = argparse.ArgumentParser() parser.add_argument("--base-url", default="http://localhost:8080") parser.add_argument("--output", default="evidence/upload-memory.json") args = parser.parse_args() client = Client(args.base_url) alice, _, _, chat = client.bootstrap() fixture = (ROOT / "tests/fixtures/sample.png").read_bytes() peak = max(stats() or [0.0]) stop = threading.Event() def sampler(): nonlocal peak while not stop.is_set(): peak = max([peak] + stats()) time.sleep(0.1) thread = threading.Thread(target=sampler, daemon=True) thread.start() started = time.monotonic() with ThreadPoolExecutor(max_workers=10) as pool: futures = [pool.submit(client.call, "POST", f"/api/v1/chats/{chat['id']}/attachments?filename=parallel-{i}.png", 201, None, alice, {"Content-Type": "image/png"}, fixture, "Attachment") for i in range(10)] results = [future.result() for future in futures] stop.set() thread.join(timeout=2) report = { "concurrency": 10, "successful_uploads": len(results), "fixture_bytes": len(fixture), "elapsed_seconds": round(time.monotonic() - started, 3), "api_peak_memory_mib": round(peak, 2), "note": "Docker stats samples API containers every 100ms; local measurement, not a capacity benchmark." } output = Path(args.output) output.parent.mkdir(parents=True, exist_ok=True) output.write_text(json.dumps(report, indent=2) + "\n") print(json.dumps(report, ensure_ascii=False)) if __name__ == "__main__": try: main() except (Failure, AssertionError, OSError) as error: print("FAIL:", error) raise SystemExit(1)