Files
mtusi2026lab4/scripts/measure-upload-memory.py
T

110 lines
3.0 KiB
Python
Executable File

#!/usr/bin/env python3
"""Run concurrent uploads while sampling API container memory with docker stats."""
from __future__ import annotations
import argparse
import json
from pathlib import Path
import re
import subprocess
import sys
import threading
ROOT = Path(__file__).resolve().parents[1]
UNITS = {
'B': 1,
'kB': 1000,
'KB': 1000,
'KiB': 1024,
'MB': 1000**2,
'MiB': 1024**2,
'GB': 1000**3,
'GiB': 1024**3,
}
def bytes_value(value: str) -> int:
match = re.fullmatch(r'([0-9.]+)([A-Za-z]+)', value.strip())
if match is None or match.group(2) not in UNITS:
raise ValueError(f'unsupported docker memory value: {value}')
return round(float(match.group(1)) * UNITS[match.group(2)])
def main() -> None:
parser = argparse.ArgumentParser()
parser.add_argument('--base-url', default='http://localhost:8080')
parser.add_argument('--concurrency', type=int, default=10)
parser.add_argument('--uploads', type=int, default=30)
args = parser.parse_args()
container_ids = subprocess.check_output(
['docker', 'compose', '--env-file', '.env', 'ps', '-q', 'api'],
cwd=ROOT,
text=True,
).split()
if len(container_ids) < 2:
raise SystemExit('expected at least two API containers')
peaks: dict[str, int] = {}
stop = threading.Event()
ready = threading.Event()
def collect() -> None:
while not stop.is_set():
output = subprocess.check_output(
['docker', 'stats', '--no-stream', '--format', '{{json .}}', *container_ids],
cwd=ROOT,
text=True,
)
for line in output.splitlines():
sample = json.loads(line)
name, usage = sample['Name'], sample['MemUsage']
current = bytes_value(usage.split('/', 1)[0])
peaks[name] = max(peaks.get(name, 0), current)
ready.set()
reader = threading.Thread(target=collect, daemon=True)
reader.start()
if not ready.wait(timeout=15):
raise SystemExit('docker stats did not produce an initial sample')
try:
load = subprocess.run(
[
sys.executable,
'tests/upload_load.py',
'--base-url',
args.base_url,
'--concurrency',
str(args.concurrency),
'--uploads',
str(args.uploads),
],
cwd=ROOT,
check=True,
capture_output=True,
text=True,
)
finally:
stop.set()
reader.join(timeout=15)
if not peaks:
raise SystemExit('docker stats produced no samples; increase --uploads')
print(load.stdout.strip())
print(
json.dumps(
{
'peak_memory_bytes': peaks,
'peak_memory_mib': {
name: round(value / 1024 / 1024, 2) for name, value in peaks.items()
},
},
ensure_ascii=False,
)
)
if __name__ == '__main__':
main()