#!/usr/bin/env python3 """Repeatable local scaling study: fixed aggregate budget, then increased budget.""" import json, os, platform, random, subprocess, sys, time, urllib.request from statistics import median from pathlib import Path ROOT = Path(__file__).resolve().parents[1] COMPOSE = ["docker", "compose", "--env-file", ".env"] OUT = ROOT / "evidence" LOAD = [sys.executable, "tests/load.py", "--base-url", "http://127.0.0.1:8080", "--duration", os.getenv("BENCH_SECONDS", "30"), "--concurrency", os.getenv("BENCH_CONCURRENCY", "16")] def run(args, env=None, check=True): subprocess.run(args, cwd=ROOT, env=env, check=check) def docker_usage(): ids=[] for service in ("api","postgres","haproxy"): found=subprocess.run(COMPOSE+["ps","-q",service],cwd=ROOT,text=True,capture_output=True,check=True).stdout.split() ids.extend(found) if not ids: return {} raw=subprocess.run(["docker","stats","--no-stream","--format","{{json .}}",*ids],cwd=ROOT,text=True,capture_output=True,check=True).stdout grouped={"api":{"cpu_percent":[],"memory_mib":[]},"postgres":{"cpu_percent":[],"memory_mib":[]},"haproxy":{"cpu_percent":[],"memory_mib":[]}} for line in raw.splitlines(): try: item=json.loads(line) except ValueError: continue name=item.get("Name","") role="api" if "-api-" in name else "postgres" if "-postgres-" in name else "haproxy" if "-haproxy-" in name else None if not role: continue try: grouped[role]["cpu_percent"].append(float(item["CPUPerc"].rstrip("%"))) except (KeyError,ValueError): pass try: size=item["MemUsage"].split("/")[0].strip() if size.endswith("GiB"): value=float(size[:-3])*1024 elif size.endswith("MiB"): value=float(size[:-3]) elif size.endswith("KiB"): value=float(size[:-3])/1024 else: value=float(size[:-1])/(1024*1024) grouped[role]["memory_mib"].append(value) except (KeyError,ValueError,IndexError): pass return grouped def row_count(): env={} for line in (ROOT/".env").read_text().splitlines(): if "=" in line: k,v=line.split("=",1); env[k]=v cmd=COMPOSE+["exec","-T","postgres","psql","-U",env.get("POSTGRES_USER","messenger"),"-d",env.get("POSTGRES_DB","messenger"),"-Atc","SELECT count(*) FROM messages"] result=subprocess.run(cmd,cwd=ROOT,text=True,capture_output=True,check=True) return int(result.stdout.strip()) def reset_synthetic_messages(): """Use an identical empty message/idempotency dataset for every sample.""" env={} for line in (ROOT/".env").read_text().splitlines(): if "=" in line: k,v=line.split("=",1); env[k]=v cmd=COMPOSE+["exec","-T","postgres","psql","-U",env.get("POSTGRES_USER","messenger"),"-d",env.get("POSTGRES_DB","messenger"),"-v","ON_ERROR_STOP=1","-c","TRUNCATE TABLE messages, idempotency_keys; ANALYZE messages; ANALYZE idempotency_keys;"] subprocess.run(cmd,cwd=ROOT,stdout=subprocess.DEVNULL,stderr=subprocess.DEVNULL,check=True) def benchmark_load(dest, env): reset_synthetic_messages() before=row_count(); samples=[] proc=subprocess.Popen(LOAD+["--output",str(dest)],cwd=ROOT,env=env,stdout=subprocess.PIPE,stderr=subprocess.STDOUT,text=True) while proc.poll() is None: stats=docker_usage() if stats: samples.append(stats) time.sleep(1) output=proc.communicate()[0] print(output,end="") if proc.returncode: raise subprocess.CalledProcessError(proc.returncode,LOAD) after=row_count() flat={} for role in ("api","postgres","haproxy"): cpus=[x for sample in samples for x in sample[role]["cpu_percent"]] mems=[x for sample in samples for x in sample[role]["memory_mib"]] flat[role]={"cpu_percent_mean":round(sum(cpus)/len(cpus),2) if cpus else None,"cpu_percent_peak":round(max(cpus),2) if cpus else None,"memory_mib_peak":round(max(mems),2) if mems else None} return {"message_rows_before":before,"message_rows_after":after,"docker_stats_samples":len(samples),"container_resource_usage":flat} def wait_ready(timeout=90): deadline=time.monotonic()+timeout while time.monotonic()','','API scaling: successful requests per second'] for tick in range(5): y=top+ploth-tick*ploth/4; val=vmax*tick/4 out += [f'',f'{val:.1f}'] colors=["#2563eb","#f97316"] for gi,(label,items) in enumerate(groups): pts=[] for i,r in enumerate(items): x=left+plotw*({1:1,2:2,4:3,8:4}[r["replicas"]])/5; y=top+ploth-(r["successful_rps"]/vmax)*ploth pts.append(f"{x:.1f},{y:.1f}"); out.append(f''); out.append(f'{r["successful_rps"]:.1f}') out.append(f'') positions={1:1,2:2,4:3,8:4} for n,pos in positions.items(): x=left+plotw*pos/5; out.append(f'{n} replicas') out += [f'{groups[0][0]}',f'{groups[1][0]}',''] path.write_text("\n".join(out)+"\n") if __name__=="__main__": main()