#!/usr/bin/env python3 """10+ одновременных upload для замера пика памяти.""" import argparse import io import sys 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 PIL import Image # noqa: E402 from client import Client, Failure, arguments, check, run # noqa: E402 def make_png(size_mib: float) -> bytes: buf = io.BytesIO() Image.new("RGB", (64, 64), (128, 128, 128)).save(buf, "PNG") data = buf.getvalue() target = int(size_mib * 1024 * 1024) check(len(data) <= target, "fixture bigger than target") return data + b"\x00" * (target - len(data)) def main(): def configure(p): p.add_argument("--concurrency", type=int, default=10) p.add_argument("--size-mib", type=float, default=9.5) args = arguments("Concurrent upload memory probe", configure) check(1 <= args.concurrency <= 64, "concurrency 1..64") check(0.1 <= args.size_mib <= 10, "size 0.1..10 MiB") c = Client(args.base_url, args.ca_file, timeout=60) alice, bob, eve, chat = c.bootstrap() blob = make_png(args.size_mib) def upload(i): local = Client(args.base_url, args.ca_file, timeout=60) t = time.monotonic() att = local.call( "POST", "/api/v1/chats/%s/attachments?filename=load-%d.png" % (chat["id"], i), 201, actor=alice, raw=blob, headers={"Content-Type": "image/png"}, schema="Attachment", )["json"] return time.monotonic() - t, att["size_bytes"] started = time.monotonic() with ThreadPoolExecutor(max_workers=args.concurrency) as pool: results = list(pool.map(upload, range(args.concurrency))) elapsed = time.monotonic() - started lat = sorted(r[0] for r in results) print("concurrency=%d size=%.2fMiB total=%.2fs p50=%.2fs p95=%.2fs" % (args.concurrency, args.size_mib, elapsed, lat[len(lat) // 2], lat[max(0, int(len(lat) * 0.95) - 1)])) if __name__ == "__main__": run(main)