import argparse import collections import concurrent.futures import hashlib import json from pathlib import Path import subprocess import sys import threading import time from urllib.parse import quote root = Path.cwd() sys.path.insert(0, str(root / "tests")) from client import Client, check parser = argparse.ArgumentParser(description="Measure memory on two disposable API replicas") parser.add_argument("--base-url", required=True) parser.add_argument("--project", required=True) parser.add_argument("--fixtures", type=Path, required=True, help="Directory containing generated load.png and load.jpg") parser.add_argument("--output", type=Path, required=True) args = parser.parse_args() out = args.output out.mkdir(parents=True, exist_ok=True) base, project, fixture_dir = args.base_url, args.project, args.fixtures def docker(*args): return subprocess.check_output(["docker", *args], text=True, timeout=20).strip() def api_ids(): return docker("ps", "-q", "--filter", "label=com.docker.compose.project=" + project, "--filter", "label=com.docker.compose.service=api").splitlines() def memory(cid): values = docker("exec", cid, "sh", "-c", "cd /sys/fs/cgroup && cat memory.current memory.peak memory.max memory.events").splitlines() return dict(zip(("current", "peak", "limit"), map(int, values[:3]))) | { "events": {k: int(v) for k, v in (line.split() for line in values[3:])}, "state": json.loads(docker("inspect", "--format", "{{json .State}}", cid)), "restarts": int(docker("inspect", "--format", "{{.RestartCount}}", cid)), } def profile(name, files): c = Client(base) actor = c.user("load") chat = c.call("POST", "/api/v1/chats", 201, actor=actor, body={ "title": "Load " + name, "member_ids": [actor["id"]]}, schema="Chat")["json"] ids = api_ids() check(len(ids) == 2, "expected both API replicas") before = {cid: memory(cid) for cid in ids} workers = len(files) gate = threading.Barrier(workers + 1) samplers = [] stop = threading.Event() sampled = {cid: [] for cid in ids} def sample(cid): proc = subprocess.Popen(["docker", "exec", cid, "sh", "-c", "for i in $(seq 1 240); do read -r m = 10, "normal profile did not complete ten uploads") normal = (root / "tests/fixtures/sample.png").read_bytes() png = (fixture_dir / "load.png").read_bytes() jpeg = (fixture_dir / "load.jpg").read_bytes() profile("normal", [("sample.png", "image/png", normal)] * 12) profile("heavy", [("load.png", "image/png", png), ("load.jpg", "image/jpeg", jpeg)] * 6)