179 lines
7.2 KiB
Python
Executable File
179 lines
7.2 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
"""Measure bounded API memory during concurrent, valid attachment uploads.
|
|
|
|
The script uses only the standard library and Docker CLI. It creates isolated
|
|
test data, submits N uploads concurrently, samples both API containers' memory,
|
|
and prints a token-free JSON summary suitable for evidence.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import argparse
|
|
import base64
|
|
import json
|
|
import re
|
|
import statistics
|
|
import subprocess
|
|
import threading
|
|
import time
|
|
import urllib.error
|
|
import urllib.request
|
|
import uuid
|
|
from concurrent.futures import ThreadPoolExecutor
|
|
from pathlib import Path
|
|
from typing import Any
|
|
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
PNG = (ROOT / "tests" / "fixtures" / "sample.png").read_bytes()
|
|
MEMORY_VALUE = re.compile(r"([0-9.]+)\s*([KMGT]?i?B)")
|
|
UNIT_BYTES = {"B": 1, "KB": 1000, "MB": 1000**2, "GB": 1000**3,
|
|
"TB": 1000**4, "KiB": 1024, "MiB": 1024**2,
|
|
"GiB": 1024**3, "TiB": 1024**4}
|
|
|
|
|
|
def request(method: str, base_url: str, path: str, *, body: dict[str, Any] | None = None,
|
|
raw: bytes | None = None, headers: dict[str, str] | None = None,
|
|
timeout: float = 90) -> tuple[int, bytes]:
|
|
data = raw
|
|
request_headers = {"Accept": "application/json"}
|
|
if body is not None:
|
|
data = json.dumps(body).encode("utf-8")
|
|
request_headers["Content-Type"] = "application/json"
|
|
request_headers.update(headers or {})
|
|
prepared = urllib.request.Request(base_url + path, data=data, headers=request_headers, method=method)
|
|
try:
|
|
response = urllib.request.urlopen(prepared, timeout=timeout)
|
|
except urllib.error.HTTPError as error:
|
|
response = error
|
|
with response:
|
|
return response.status, response.read()
|
|
|
|
|
|
def require_json(status: int, data: bytes, expected: int) -> dict[str, Any]:
|
|
if status != expected:
|
|
raise RuntimeError("expected HTTP %s, received %s: %s" % (expected, status, data[:300]))
|
|
return json.loads(data)
|
|
|
|
|
|
def docker_api_containers(env_file: str) -> list[str]:
|
|
completed = subprocess.run(
|
|
["docker", "compose", "--env-file", env_file, "ps", "-q", "api-1", "api-2"],
|
|
cwd=ROOT, check=True, text=True, capture_output=True,
|
|
)
|
|
containers = completed.stdout.split()
|
|
if len(containers) != 2:
|
|
raise RuntimeError("both API replicas must be running before the experiment")
|
|
return containers
|
|
|
|
|
|
def memory_bytes(containers: list[str]) -> int:
|
|
completed = subprocess.run(
|
|
["docker", "stats", "--no-stream", "--format", "{{.MemUsage}}", *containers],
|
|
check=True, text=True, capture_output=True,
|
|
)
|
|
total = 0
|
|
for line in completed.stdout.splitlines():
|
|
match = MEMORY_VALUE.search(line)
|
|
if match is None:
|
|
raise RuntimeError("cannot parse docker memory output: " + line)
|
|
total += int(float(match.group(1)) * UNIT_BYTES[match.group(2)])
|
|
return total
|
|
|
|
|
|
def percentile(values: list[float], fraction: float) -> float:
|
|
values = sorted(values)
|
|
return values[min(len(values) - 1, max(0, round((len(values) - 1) * fraction)))]
|
|
|
|
|
|
def main() -> None:
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
parser.add_argument("--base-url", default="http://127.0.0.1:8083")
|
|
parser.add_argument("--concurrency", type=int, default=10)
|
|
parser.add_argument("--payload-mib", type=int, default=8)
|
|
parser.add_argument("--env-file", default=".env")
|
|
parser.add_argument("--evidence", type=Path)
|
|
args = parser.parse_args()
|
|
if not 10 <= args.concurrency <= 40:
|
|
raise SystemExit("concurrency must be 10..40")
|
|
if not 2 <= args.payload_mib <= 10:
|
|
raise SystemExit("payload-mib must be 2..10")
|
|
base_url = args.base_url.rstrip("/")
|
|
payload = PNG + b"\0" * (args.payload_mib * 1024 * 1024 - len(PNG))
|
|
registration = {
|
|
"username": "load_" + uuid.uuid4().hex[:16],
|
|
"display_name": "Attachment load test",
|
|
"password": "Load-" + uuid.uuid4().hex,
|
|
}
|
|
user = require_json(*request("POST", base_url, "/api/v1/users", body=registration), 201)
|
|
basic = base64.b64encode((registration["username"] + ":" + registration["password"]).encode()).decode()
|
|
session = require_json(*request("POST", base_url, "/api/v1/auth/sessions",
|
|
headers={"Authorization": "Basic " + basic}), 201)
|
|
token = session["token"]
|
|
chat = require_json(*request("POST", base_url, "/api/v1/chats",
|
|
body={"title": "Attachment memory experiment", "member_ids": [user["id"]]},
|
|
headers={"Authorization": "Bearer " + token}), 201)
|
|
path = "/api/v1/chats/%s/attachments?filename=memory.png" % chat["id"]
|
|
containers = docker_api_containers(args.env_file)
|
|
baseline = memory_bytes(containers)
|
|
samples: list[int] = []
|
|
monitor_error: list[str] = []
|
|
stop = threading.Event()
|
|
|
|
def monitor() -> None:
|
|
while not stop.is_set():
|
|
try:
|
|
samples.append(memory_bytes(containers))
|
|
except (OSError, subprocess.SubprocessError, RuntimeError) as exc:
|
|
monitor_error.append(type(exc).__name__)
|
|
return
|
|
stop.wait(0.1)
|
|
|
|
thread = threading.Thread(target=monitor, daemon=True)
|
|
thread.start()
|
|
time.sleep(0.2)
|
|
|
|
def upload(_: int) -> tuple[int, float]:
|
|
started = time.monotonic()
|
|
status, _ = request("POST", base_url, path, raw=payload, headers={
|
|
"Authorization": "Bearer " + token, "Content-Type": "image/png",
|
|
})
|
|
return status, time.monotonic() - started
|
|
|
|
started = time.monotonic()
|
|
with ThreadPoolExecutor(max_workers=args.concurrency) as executor:
|
|
results = list(executor.map(upload, range(args.concurrency)))
|
|
elapsed = time.monotonic() - started
|
|
stop.set()
|
|
thread.join(timeout=3)
|
|
statuses = [status for status, _ in results]
|
|
durations_ms = [duration * 1000 for _, duration in results]
|
|
if monitor_error:
|
|
raise RuntimeError("memory sampling failed: " + monitor_error[0])
|
|
if not samples:
|
|
raise RuntimeError("memory sampling returned no samples")
|
|
summary = {
|
|
"concurrency": args.concurrency,
|
|
"payload_bytes_each": len(payload),
|
|
"successful_uploads": statuses.count(201),
|
|
"failed_uploads": len(statuses) - statuses.count(201),
|
|
"elapsed_seconds": round(elapsed, 3),
|
|
"successful_rps": round(statuses.count(201) / elapsed, 3),
|
|
"latency_ms": {"p50": round(percentile(durations_ms, .50), 1),
|
|
"p95": round(percentile(durations_ms, .95), 1),
|
|
"p99": round(percentile(durations_ms, .99), 1)},
|
|
"api_memory_bytes": {"baseline_before_test": baseline, "peak_sample": max([baseline, *samples]),
|
|
"growth": max([baseline, *samples]) - baseline, "samples": len(samples)},
|
|
"memory_method": "sum of docker stats for api-1 and api-2; baseline before requests, then repeat after a 100 ms wait",
|
|
}
|
|
text = json.dumps(summary, ensure_ascii=False, sort_keys=True)
|
|
print(text)
|
|
if args.evidence is not None:
|
|
args.evidence.parent.mkdir(parents=True, exist_ok=True)
|
|
args.evidence.write_text(text + "\n", encoding="utf-8")
|
|
if summary["failed_uploads"]:
|
|
raise SystemExit(1)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|