#!/usr/bin/env python3 """Run ten concurrent bounded uploads and sample API RSS via Docker.""" import concurrent.futures import subprocess import threading import time from pathlib import Path from client import Client, arguments, check, run def rss_mb() -> float: output = subprocess.check_output( ["docker", "stats", "--no-stream", "--format", "{{.Name}} {{.MemUsage}}"], text=True, ) values = [] for line in output.splitlines(): if "api1" not in line and "api2" not in line: continue used = line.split()[1].split("/")[0] factor = {"B": 1 / 1024 / 1024, "KiB": 1 / 1024, "MiB": 1, "GiB": 1024} values.append(float(used[:-3]) * factor[used[-3:]]) return max(values, default=0.0) def main(): args = arguments("lab4 concurrent bounded upload experiment") client = Client(args.base_url, args.ca_file) alice, _, _, chat = client.bootstrap() content = Path(__file__).with_name("fixtures").joinpath("sample.png").read_bytes() stop = threading.Event() samples = [rss_mb()] def sample(): while not stop.is_set(): try: samples.append(rss_mb()) except (OSError, ValueError, subprocess.SubprocessError): pass time.sleep(0.1) sampler = threading.Thread(target=sample, daemon=True) sampler.start() started = time.monotonic() with concurrent.futures.ThreadPoolExecutor(max_workers=10) as pool: futures = [ pool.submit( client.call, "POST", f"/api/v1/chats/{chat['id']}/attachments?filename=load-{index}.png", 201, None, alice, {"Content-Type": "image/png"}, content, "Attachment", ) for index in range(10) ] [future.result() for future in futures] elapsed = time.monotonic() - started stop.set() sampler.join(timeout=1) samples.append(rss_mb()) check(len(samples) > 0, "RSS sampling did not produce data") print(f"uploads=10 concurrency=10 elapsed_seconds={elapsed:.3f} peak_api_rss_mb={max(samples):.1f}") if __name__ == "__main__": run(main)