Files
mtusi2026lab7/student-tests/workload.py
T

16 lines
1.6 KiB
Python

#!/usr/bin/env python3
"""One bounded HTTPS workload with real resources and dataset counts."""
import importlib.util,json,subprocess,sys,time
from pathlib import Path
ROOT=Path(__file__).resolve().parents[1]
s=importlib.util.spec_from_file_location('sec',ROOT/'student-tests/security.py');sec=importlib.util.module_from_spec(s);s.loader.exec_module(sec)
def counts():return json.loads(sec.sql_value("SELECT json_build_object('users',(SELECT count(*) FROM users),'chats',(SELECT count(*) FROM chats),'messages',(SELECT count(*) FROM messages),'attachments',(SELECT count(*) FROM attachments))"))
start=time.monotonic();before=counts();rows=[];ids=sec.dc('ps','-q','api','worker','collector-store').splitlines()
p=subprocess.Popen([sys.executable,'tests/load.py','--base-url',sec.BASE,'--ca-file',sec.CA,'--duration','15','--concurrency','4','--output','evidence/load.json'],cwd=ROOT)
while p.poll() is None:
stats=subprocess.check_output(['docker','stats','--no-stream','--format','{{json .}}',*ids],text=True)
rows.append({'elapsed_seconds':round(time.monotonic()-start,3),'containers':[json.loads(x) for x in stats.splitlines()]});time.sleep(.5)
assert p.returncode==0 and (ROOT/'evidence/load.json').exists()
result={'requested_duration_seconds':15,'concurrency':4,'warmup_seconds':0,'before':before,'after':counts(),'resource_observations':rows,'limits':'closed-loop/coordinated omission, success-only latency percentiles, shared VM, growing dataset; no capacity or TLS off/on comparison'}
(ROOT/'evidence/load-resources.json').write_text(json.dumps(result,indent=2)+'\n');print('MEASURED: verified HTTPS workload, errors, percentiles, CPU/RAM and dataset counts',flush=True)