Files
mtusi2026lab3/scripts/db_bottleneck.py
T

44 lines
1.9 KiB
Python

"""Causal DB budget intervention, then alternate limited/restored budget at fixed API resources."""
import json
import argparse
from scaling import OUT, run, setup
from stack import ROOT, docker
results=[]
configs=[]
def database(cpu):
cid=docker('compose','ps','-q','db')
# Change only the CPU quota, not container identity, connections or storage.
# Recreating a throttled DB here adds recovery/startup as an unwanted variable.
docker('update','--cpus',str(cpu),cid)
actual=int(docker('inspect','--format','{{.HostConfig.NanoCpus}}',cid))
assert actual==int(cpu*1e9)
return {'container':cid,'nano_cpus':actual}
def record(label,n,cpu,repeat):
db=database(cpu)
api=setup(n,.5,192,5)
configs.append({'label':label,'repeat':repeat,'db':db,'api':api})
results.append(run(label,24,repeat,12))
(OUT/'db-intervention.json').write_text(json.dumps({'configurations':configs,'results':results},indent=2)+'\n',encoding='utf-8')
parser=argparse.ArgumentParser()
parser.add_argument('--paired-only',action='store_true',help='Keep completed 1/2/4 DB-limited series; repeat only alternating budgets')
args=parser.parse_args()
if args.paired_only:
previous=json.loads((OUT/'db-intervention.json').read_text(encoding='utf-8'))
results=[r for r in previous['results'] if r['label'].startswith('db010-api')]
configs=[r for r in previous['configurations'] if r['label'].startswith('db010-api')]
try:
# With the same deliberately constrained DB, more API resources cannot remove its CPU ceiling.
if not args.paired_only:
for n in (1,2,4):
for repeat in (1,2): record(f'db010-api{n}',n,.1,repeat)
# Alternating budgets controls time/order drift better than a single before/after sample.
for repeat in (1,2,3):
record('paired-db010',2,.1,repeat)
record('paired-db100',2,1,repeat)
finally:
database(1)
docker('compose','up','-d','--no-deps','--wait','--scale','api=2','api')