forked from mtusi2026/mtusi2026lab4
lab4: s3 attachments, private bucket, healthchecks, tests and report
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
"""10 simultaneous real uploads + Docker RSS observations (NO invented metrics)."""
|
||||
import concurrent.futures
|
||||
import json
|
||||
import subprocess
|
||||
import sys
|
||||
import threading
|
||||
import time
|
||||
from pathlib import Path
|
||||
from statistics import median
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
sys.path.insert(0, str(ROOT / 'tests'))
|
||||
from client import Client, check, arguments, run
|
||||
|
||||
|
||||
def mib(value):
|
||||
value = value.strip()
|
||||
for suffix, multiplier in (("GiB",1024),("MiB",1),("KiB",1/1024),("B",1/1048576)):
|
||||
if value.endswith(suffix):
|
||||
return float(value[:-len(suffix)]) * multiplier
|
||||
return None
|
||||
|
||||
|
||||
def main():
|
||||
args = arguments('Concurrent uploads + observed API container memory')
|
||||
client = Client(args.base_url, args.ca_file, timeout=30)
|
||||
alice, bob, eve, chat = client.bootstrap()
|
||||
file = (ROOT / 'tests/fixtures/sample.png').read_bytes()
|
||||
results, memory = [], []
|
||||
stop = threading.Event()
|
||||
def observe():
|
||||
while not stop.is_set():
|
||||
r = subprocess.run(['docker','stats','--no-stream','--format','{{.Name}}|{{.MemUsage}}'],
|
||||
capture_output=True, text=True, timeout=10)
|
||||
if r.returncode == 0:
|
||||
for line in r.stdout.splitlines():
|
||||
if '|api1-' in line or '|api2-' in line:
|
||||
pass
|
||||
parts = line.split('|')
|
||||
if len(parts)==2 and ('api1-' in parts[0] or 'api2-' in parts[0]):
|
||||
amount = mib(parts[1].split('/')[0])
|
||||
if amount is not None:
|
||||
memory.append({'container': parts[0], 'mem_mib': round(amount,3)})
|
||||
stop.wait(0.2)
|
||||
monitor = threading.Thread(target=observe, daemon=True)
|
||||
monitor.start()
|
||||
try:
|
||||
def worker(i):
|
||||
local = Client(args.base_url,args.ca_file,timeout=30)
|
||||
start=time.perf_counter()
|
||||
resp=local.call('POST', f'/api/v1/chats/{chat["id"]}/attachments?filename=run{i}.png',
|
||||
201, actor=alice, raw=file, headers={'Content-Type':'image/png'}, schema='Attachment')
|
||||
return {'elapsed_ms':round((time.perf_counter()-start)*1000,2), 'instance': resp['headers'].get('X-Instance-Id')}
|
||||
with concurrent.futures.ThreadPoolExecutor(max_workers=10) as executor:
|
||||
futures=[executor.submit(worker,i) for i in range(10)]
|
||||
results=[f.result() for f in concurrent.futures.as_completed(futures)]
|
||||
finally:
|
||||
stop.set(); monitor.join(timeout=12)
|
||||
output={'concurrency':10,'file_bytes':len(file),'completed':len(results),
|
||||
'median_latency_ms':round(median(x['elapsed_ms'] for x in results),2),
|
||||
'api_peak_mem_mib_observed':max([x['mem_mib'] for x in memory],default=None),
|
||||
'memory_samples':len(memory),'instances':sorted(set(x['instance'] for x in results))}
|
||||
target=ROOT / 'evidence' / 'uploads-10.json'
|
||||
target.write_text(json.dumps(output,ensure_ascii=False,indent=2),encoding='utf-8')
|
||||
check(len(results)==10, 'not all uploads completed')
|
||||
print('PASS: 10 parallel uploads; metrics written:',target)
|
||||
print(json.dumps(output, ensure_ascii=False, indent=2))
|
||||
if not memory:
|
||||
print('WARNING: no Docker memory samples; run docker stats separately for a valid grade-4 experiment')
|
||||
|
||||
if __name__ == '__main__':
|
||||
run(main)
|
||||
Reference in New Issue
Block a user