#!/usr/bin/env python3 """Short real Docker experiment; restores original replica count and CPU/RAM limits.""" import json, subprocess as sp, time, pathlib, datetime, csv ROOT = pathlib.Path(__file__).resolve().parents[1] OUT = ROOT / 'evidence/track1' DC = ['docker', 'compose', '--env-file', '.env'] def call(args): return sp.check_output(args, cwd=ROOT, text=True) def compose(*args): return call(DC + list(args)) def ids(service): return compose('ps', '-q', service).split() def update(service, cpu, memory, swap=None): for cid in ids(service): call(['docker','update','--cpus',str(cpu),'--memory',str(memory),'--memory-swap',str(memory if swap is None else swap),cid]) def ready(n): for _ in range(90): raw=compose('exec','-T','lb','wget','-qO-','http://127.0.0.1:8404/stats;csv') rows=list(csv.reader(raw.splitlines())) if sum(len(r)>17 and r[0]=='api' and r[1]!='BACKEND' and r[17]=='UP' for r in rows)==n: return time.sleep(1) raise RuntimeError('LB not ready') def main(): OUT.mkdir(parents=True,exist_ok=True) original={s:json.loads(call(['docker','inspect',ids(s)[0]]))[0]['HostConfig'] for s in ['api','lb','postgres']} n0=len(ids('api')) (OUT/'environment.json').write_text(json.dumps({'utc':datetime.datetime.now(datetime.timezone.utc).isoformat(),'docker':json.loads(call(['docker','info','--format','{{json .}}'])),'original_limits':original},indent=2)) # Same 32-client 80/20 read/write workload; fixed infrastructure in both series. cases=[('fixed-1',1,2,1024,1,2),('fixed-2',2,1,512,1,2),('fixed-4',4,.5,256,1,2), ('grow-1',1,1,512,1,2),('grow-4',4,1,512,1,2), ('lb2-4',4,1,512,2,2),('db4-4',4,1,512,1,4)] try: for label,n,cpu,mem,lb,db in cases: compose('up','-d','--no-recreate','--scale',f'api={n}','api','lb') update('api',cpu,f'{mem}m'); update('lb',lb,'256m'); update('postgres',db,'1g') ready(n) limits=call(['docker','inspect','--format','{{.Name}} {{.HostConfig.NanoCpus}} {{.HostConfig.Memory}}']+ids('api')+ids('lb')+ids('postgres')) (OUT/f'{label}-limits.txt').write_text(limits) for repeat in [1,2]: name=f'{label}-r{repeat}' print('RUN',name,flush=True) compose('exec','-T','postgres','psql','-U','messenger','-d','messenger','-q','-c','ANALYZE;') with (OUT/f'{name}.log').open('w') as log: proc=sp.Popen(DC+['--profile','bench','run','--rm','-T','loadgen','-duration','20s','-warmup','5s','-concurrency','32','-label',name,'-out',f'/evidence/track1/{name}.json'],cwd=ROOT,stdout=log,stderr=sp.STDOUT) deadline=time.monotonic()+180 while 'measure_start' not in (OUT/f'{name}.log').read_text(): if proc.poll() is not None or time.monotonic()>deadline: raise RuntimeError((OUT/f'{name}.log').read_text()) time.sleep(.2) with (OUT/f'{name}-stats.jsonl').open('w') as stats: monitor=sp.Popen(['docker','stats','--format','{{json .}}']+ids('api')+ids('lb')+ids('postgres')+ids('valkey'),stdout=stats,stderr=sp.DEVNULL,text=True) time.sleep(18) monitor.terminate(); monitor.wait() rc=proc.wait(timeout=180) if rc: raise RuntimeError((OUT/f'{name}.log').read_text()) result=json.loads((OUT/f'{name}.json').read_text()) print(name,round(result['successful_rps']),result['error_share'],result['verify'],flush=True) finally: compose('up','-d','--no-recreate','--scale',f'api={n0}','api','lb') for s,h in original.items(): update(s,h['NanoCpus']/1e9,str(h['Memory']),h['MemorySwap']) ready(n0) print('Original limits and replicas restored',flush=True) if __name__=='__main__': main()