Files
mtusi2026lab4/scripts/attachment-load.py
T

85 lines
3.0 KiB
Python

#!/usr/bin/env python3
"""Ten simultaneous binary uploads and sampled API cgroup memory, without tokens in output."""
import concurrent.futures
import hashlib
import json
import os
from pathlib import Path
import random
import struct
import subprocess
import sys
import threading
import time
import zlib
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT / 'tests'))
from client import Client, check
def chunk(kind, data):
return struct.pack('!I', len(data)) + kind + data + struct.pack('!I', zlib.crc32(kind + data))
def fixture():
width = height = 1600
rng = random.Random(42)
pixels = b''.join(b'\0' + rng.randbytes(width * 3) for _ in range(height))
return (b'\x89PNG\r\n\x1a\n' + chunk(b'IHDR', struct.pack('!IIBBBBB', width, height, 8, 2, 0, 0, 0))
+ chunk(b'IDAT', zlib.compress(pixels, 1)) + chunk(b'IEND', b''))
def memory(ids):
result = {}
for container in ids:
value = subprocess.check_output(['docker', 'exec', container, 'cat', '/sys/fs/cgroup/memory.current'], text=True)
result[container[:12]] = int(value)
return result
def main():
c = Client(timeout=60)
alice, bob, eve, chat = c.bootstrap()
data = fixture()
expected = hashlib.sha256(data).hexdigest()
ids = subprocess.check_output(['docker', 'compose', 'ps', '-q', 'api'], cwd=ROOT, text=True).split()
baseline = memory(ids)
samples = []
stop = threading.Event()
start = time.monotonic()
def sample():
while not stop.is_set():
samples.append({'seconds': round(time.monotonic() - start, 3), 'bytes': memory(ids)})
stop.wait(0.2)
barrier = threading.Barrier(10)
def upload(index):
client = Client(timeout=60)
barrier.wait(timeout=30)
result = client.call('POST', '/api/v1/chats/' + chat['id'] + '/attachments?filename=load.png',
201, actor=alice, raw=data, headers={'Content-Type': 'image/png'}, schema='Attachment')
check(result['json']['sha256'] == expected, 'uploaded checksum mismatch')
downloaded = client.call('GET', '/api/v1/attachments/' + result['json']['id'] + '/content', actor=bob)
check(hashlib.sha256(downloaded['bytes']).hexdigest() == expected, 'download checksum mismatch')
return {'upload': index, 'seconds': round(result['elapsed'], 3), 'instance': result['headers']['X-Instance-Id']}
sampler = threading.Thread(target=sample)
sampler.start()
try:
with concurrent.futures.ThreadPoolExecutor(max_workers=10) as pool:
results = list(pool.map(upload, range(10)))
finally:
stop.set()
sampler.join()
peak = {key: max([baseline[key]] + [s['bytes'][key] for s in samples]) for key in baseline}
print(json.dumps({'concurrency': 10, 'file_bytes': len(data), 'pixels': 1600 * 1600,
'sha256': expected, 'baseline_bytes': baseline, 'sampled_peak_bytes': peak,
'results': results, 'samples': samples}, indent=2))
if __name__ == '__main__':
main()