Files
mtusi2026lab3/scripts/saturation.py
2026-11-07 10:41:21 +03:00

43 lines
2.2 KiB
Python

#!/usr/bin/env python3
"""Controlled LB-resource probe with a larger client in-flight bound."""
import argparse,json,os,subprocess,threading,time,hashlib
from pathlib import Path
from metrics import ROOT,containers,snapshot
from research import configure,run
def main():
p=argparse.ArgumentParser();p.add_argument('--base-url',default='http://localhost:18080');a=p.parse_args()
run(['make','build'])
env=configure(2,1,512)
try:
for lb_cpu in (.5,1):
run(['docker','update','--cpus',str(lb_cpu),run(['./scripts/compose.sh','ps','-q','lb'])])
time.sleep(3)
for repeat in (1,2,3):
label=f'lbprobe-cpu{lb_cpu}-n{repeat}';dest=ROOT/'evidence/research'/f'{label}.json'
if dest.exists():continue
rows=containers();stop=threading.Event()
proc=subprocess.Popen([str(ROOT/'bin/benchmark'),'-base-url',a.base_url,'-rate','8100','-duration','30s','-warmup','5s','-seed','1000','-max-in-flight','1024','-label',label,'-output',str(dest)],cwd=ROOT,stdout=subprocess.DEVNULL)
print(label,flush=True)
applied=json.loads(run(['docker','inspect',run(['./scripts/compose.sh','ps','-q','lb'])]))[0]
dest.with_suffix('.limits.json').write_text(json.dumps({'lb_nano_cpus':applied['HostConfig']['NanoCpus'],'max_in_flight':1024,'api_replicas':2,'api_cpu_total':2,'pool_total':16,'generator_sha256':hashlib.sha256((ROOT/'bin/benchmark').read_bytes()).hexdigest()},indent=2)+'\n')
def collect():
with dest.with_suffix('.metrics.jsonl').open('w') as f:
while not stop.is_set():
r=snapshot(rows)
host=subprocess.run(['ps','-p',str(proc.pid),'-o','%cpu=,rss='],capture_output=True,text=True)
try:cpu,rss=host.stdout.split();r['generator_host']={'cpu_percent':float(cpu),'rss_kib':int(rss)}
except ValueError:pass
f.write(json.dumps(r)+'\n');f.flush();stop.wait(3)
thread=threading.Thread(target=collect);thread.start()
try:
code=proc.wait(timeout=180)
if code and not dest.exists():raise RuntimeError(label+' failed')
finally:stop.set();thread.join()
time.sleep(3)
finally:
run(['docker','update','--cpus','0.5',run(['./scripts/compose.sh','ps','-q','lb'])])
configure(2,1,512)
print('LB probe complete; restored default resource limits.',flush=True)
if __name__=='__main__':main()