forked from mtusi2026/mtusi2026lab4
148 lines
5.2 KiB
Python
Executable File
148 lines
5.2 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
"""Measure API memory under concurrent 9 MiB uploads (lab4 level 4).
|
|
|
|
Run against a live stand. Writes raw CSV samples and a summary under
|
|
evidence/lab4/memory/. The primary metric is cgroup anon memory: memory.current
|
|
also includes page cache from the temporary spool files.
|
|
|
|
python3 scripts/lab4_memory_experiment.py --base-url http://localhost:8080
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import argparse
|
|
import csv
|
|
import os
|
|
import struct
|
|
import sys
|
|
import threading
|
|
import time
|
|
import zlib
|
|
from concurrent.futures import ThreadPoolExecutor
|
|
from pathlib import Path
|
|
|
|
ROOT = Path(__file__).resolve().parent.parent
|
|
sys.path.insert(0, str(ROOT / 'tests'))
|
|
|
|
from client import Client # noqa: E402
|
|
|
|
OUTPUT = ROOT / 'evidence/lab4/memory'
|
|
|
|
|
|
def png_of_size(width: int, height: int, seed: int) -> bytes:
|
|
"""Build a valid, poorly compressible PNG (random RGB scanlines)."""
|
|
rng = __import__('random').Random(seed)
|
|
raw = bytearray()
|
|
for _ in range(height):
|
|
raw.append(0) # filter: None
|
|
raw.extend(rng.randbytes(width * 3))
|
|
|
|
def chunk(kind: bytes, data: bytes) -> bytes:
|
|
return struct.pack('>I', len(data)) + kind + data + struct.pack('>I', zlib.crc32(kind + data) & 0xFFFFFFFF)
|
|
|
|
ihdr = struct.pack('>IIBBBBB', width, height, 8, 2, 0, 0, 0)
|
|
return b'\x89PNG\r\n\x1a\n' + chunk(b'IHDR', ihdr) + chunk(b'IDAT', zlib.compress(bytes(raw), 6)) + chunk(b'IEND', b'')
|
|
|
|
|
|
def containers(service: str) -> list[str]:
|
|
output = os.popen(f'docker compose ps -q {service}').read().split()
|
|
if not output:
|
|
raise SystemExit(f'no running containers for service {service}')
|
|
return output
|
|
|
|
|
|
def read_number(container: str, path: str) -> int:
|
|
output = os.popen(f'docker exec {container} cat {path} 2>/dev/null').read().strip()
|
|
return int(output) if output.isdigit() else 0
|
|
|
|
|
|
def memory_snapshot() -> dict[str, int]:
|
|
api = containers('api')
|
|
anon = current = 0
|
|
for container in api:
|
|
stats = os.popen(f'docker exec {container} cat /sys/fs/cgroup/memory.stat 2>/dev/null').read()
|
|
for line in stats.splitlines():
|
|
if line.startswith('anon '):
|
|
anon += int(line.split()[1])
|
|
current += read_number(container, '/sys/fs/cgroup/memory.current')
|
|
return {'anon': anon, 'current': current}
|
|
|
|
|
|
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('--width', type=int, default=2100)
|
|
parser.add_argument('--height', type=int, default=1500)
|
|
parser.add_argument('--rounds', type=int, default=3)
|
|
args = parser.parse_args()
|
|
|
|
content = png_of_size(args.width, args.height, 42)
|
|
if len(content) > 10 * 1024 * 1024:
|
|
raise SystemExit(f'generated PNG is {len(content)} bytes, above the 10 MiB contract limit')
|
|
|
|
client = Client(args.base_url, timeout=120)
|
|
alice, _, _, chat = client.bootstrap()
|
|
|
|
OUTPUT.mkdir(parents=True, exist_ok=True)
|
|
samples: list[dict[str, int | float | str]] = []
|
|
stop = threading.Event()
|
|
|
|
def sample(phase: str = 'during') -> None:
|
|
started = time.monotonic()
|
|
while not stop.is_set():
|
|
snapshot = memory_snapshot()
|
|
samples.append({'elapsed': round(time.monotonic() - started, 3), 'phase': phase, **snapshot})
|
|
time.sleep(0.2)
|
|
|
|
baseline = memory_snapshot()
|
|
sampler = threading.Thread(target=sample, daemon=True)
|
|
sampler.start()
|
|
|
|
def upload(_: int) -> None:
|
|
path = '/api/v1/chats/' + chat['id'] + '/attachments?filename=memory.png'
|
|
client.call(
|
|
'POST', path, 201, actor=alice, raw=content,
|
|
headers={'Content-Type': 'image/png'}, schema='Attachment',
|
|
)
|
|
|
|
started = time.monotonic()
|
|
for _ in range(args.rounds):
|
|
with ThreadPoolExecutor(max_workers=args.concurrency) as pool:
|
|
list(pool.map(upload, range(args.concurrency)))
|
|
elapsed = time.monotonic() - started
|
|
|
|
stop.set()
|
|
sampler.join(timeout=2)
|
|
after = memory_snapshot()
|
|
|
|
peak_anon = max((int(item['anon']) for item in samples), default=after['anon'])
|
|
peak_current = max((int(item['current']) for item in samples), default=after['current'])
|
|
|
|
csv_path = OUTPUT / 'samples.csv'
|
|
with csv_path.open('w', newline='') as handle:
|
|
writer = csv.DictWriter(handle, fieldnames=['elapsed', 'phase', 'anon', 'current'])
|
|
writer.writeheader()
|
|
writer.writerows(samples)
|
|
|
|
summary = OUTPUT / 'summary.txt'
|
|
summary.write_text(
|
|
'Lab4 memory experiment\n'
|
|
f'concurrency: {args.concurrency}\n'
|
|
f'file_size_bytes: {len(content)}\n'
|
|
f'uploads: {args.concurrency * args.rounds}\n'
|
|
f'rounds: {args.rounds}\n'
|
|
f'elapsed_seconds: {elapsed:.3f}\n'
|
|
f'baseline_anon_bytes: {baseline["anon"]}\n'
|
|
f'peak_anon_bytes: {peak_anon}\n'
|
|
f'anon_delta_bytes: {peak_anon - baseline["anon"]}\n'
|
|
f'peak_memory_current_bytes: {peak_current}\n'
|
|
'note: memory.current includes page cache from temporary spool files; anon is the primary metric\n'
|
|
)
|
|
|
|
print(summary.read_text())
|
|
print('CSV:', csv_path)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main()
|