#!/usr/bin/env python3 """Upload 10+ valid images concurrently and sample API container memory.""" import argparse from concurrent.futures import ThreadPoolExecutor, as_completed import json import re import subprocess import threading import time from client import Client, check from image_fixture import noisy_rgb_png def memory_bytes(value): match = re.match(r"\s*([\d.]+)\s*([KMG]i?B)", value) if not match: return 0 amount = float(match.group(1)) unit = match.group(2).upper() return int( amount * { "KB": 1000, "MB": 1000**2, "GB": 1000**3, "KIB": 1024, "MIB": 1024**2, "GIB": 1024**3, }[unit] ) def compose_api_ids(): result = subprocess.run( ["docker", "compose", "ps", "-q", "api"], check=True, capture_output=True, text=True, ) healthy_ids = [] for container_id in result.stdout.split(): health = subprocess.run( ["docker", "inspect", "--format", "{{.State.Health.Status}}", container_id], check=True, capture_output=True, text=True, ) if health.stdout.strip() == "healthy": healthy_ids.append(container_id) check(len(healthy_ids) >= 2, "need at least two healthy API containers for the measurement") return healthy_ids def sample_memory(ids, stop, peak): while not stop.is_set(): for container_id in ids: result = subprocess.run( ["docker", "stats", "--no-stream", "--format", "{{.MemUsage}}", container_id], capture_output=True, text=True, check=False, ) if result.returncode == 0: used = memory_bytes(result.stdout.split("/", 1)[0]) peak[container_id] = max(peak.get(container_id, 0), used) stop.wait(0.5) def main(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("--base-url", default="http://localhost:8080") parser.add_argument("--concurrency", type=int, default=10) parser.add_argument("--output", default=None) args = parser.parse_args() if args.concurrency < 10: parser.error("--concurrency must be at least 10") bootstrap = Client(args.base_url) alice, _, _, chat = bootstrap.bootstrap() content = noisy_rgb_png(1200, 1200) check(0 < len(content) <= 10 * 1024 * 1024, "generated PNG must be within 10 MiB") api_ids = compose_api_ids() stop = threading.Event() peak = {} sampler = threading.Thread(target=sample_memory, args=(api_ids, stop, peak), daemon=True) sampler.start() started = time.monotonic() attachment_ids = [] try: with ThreadPoolExecutor(max_workers=args.concurrency) as executor: tasks = [ executor.submit( Client(args.base_url).call, "POST", "/api/v1/chats/" + chat["id"] + "/attachments?filename=load.png", 201, None, alice, {"Content-Type": "image/png"}, content, "Attachment", ) for _ in range(args.concurrency) ] for task in as_completed(tasks): attachment_ids.append(task.result()["json"]["id"]) finally: stop.set() sampler.join() result = { "concurrency": args.concurrency, "upload_bytes_each": len(content), "uploads_succeeded": len(attachment_ids), "elapsed_seconds": round(time.monotonic() - started, 3), "api_container_peak_memory_bytes": peak, "api_container_ids": api_ids, "notes": "Peak sampled every 0.5s using docker stats; excludes database, LB, and client memory.", } if len(attachment_ids) != args.concurrency: raise SystemExit("not every upload succeeded") output = json.dumps(result, indent=2, sort_keys=True) if args.output: with open(args.output, "w", encoding="utf-8") as file: file.write(output + "\n") print(output) if __name__ == "__main__": main()