Files
mtusi2026lab3-fork/scripts/calibrate-scaling-observer.py
qrtx c1fec73ccc perf(bench): add scaling experiments and saturation diagnostics
Compare API and PostgreSQL CPU budgets and connection pool limits. Collect repeatable measurements and generate report aggregates and graphs.
2026-10-29 11:49:51 +03:00

68 lines
3.1 KiB
Python

#!/usr/bin/env python3
"""Measure idle PostgreSQL CPU with/without the observer after a scaling sweep."""
import argparse
import json
from pathlib import Path
import subprocess
import time
import uuid
ROOT = Path(__file__).resolve().parents[1]
def docker(*args):
return subprocess.check_output(["docker", *args], cwd=ROOT, text=True, encoding="utf-8", timeout=30)
def snapshot(container):
values = {k: int(v) for k, v in (line.split() for line in docker("exec", container, "cat", "/sys/fs/cgroup/cpu.stat").splitlines())}
return {"time": time.time(), "cpu": values}
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--seconds", type=int, default=30)
parser.add_argument("--output", type=Path, required=True)
args = parser.parse_args()
args.output.mkdir(parents=True, exist_ok=False)
container = docker("compose", "ps", "-q", "postgres").strip()
results = []
for phase in ("without-before", "with-observer", "without-after"):
child = None
files = []
stop = f"/tmp/scaling-calibration-{uuid.uuid4().hex}.stop"
try:
if phase == "with-observer":
files = [(args.output / "postgres.jsonl").open("wb"), (args.output / "postgres.stderr").open("wb")]
child = subprocess.Popen(["docker", "exec", "-i", container, "sh", "-s", "postgres", stop],
cwd=ROOT, stdin=subprocess.PIPE, stdout=files[0], stderr=files[1])
child.stdin.write((ROOT / "scripts/diagnostics/observer.sh").read_bytes())
child.stdin.close()
time.sleep(3)
before = snapshot(container)
time.sleep(args.seconds)
after = snapshot(container)
elapsed = after["time"] - before["time"]
row = {"phase": phase, "before": before, "after": after,
"mean_cpu_cores": (after["cpu"]["usage_usec"]-before["cpu"]["usage_usec"])/elapsed/1e6}
results.append(row)
print(f"{phase}: CPU={row['mean_cpu_cores']:.4f} cores", flush=True)
finally:
if child is not None:
docker("exec", container, "touch", stop)
child.wait(timeout=15)
docker("cp", f"{container}:{stop}.pg.jsonl", str(args.output / "postgres-sql.jsonl"))
docker("exec", container, "rm", "-f", stop + ".pg.jsonl")
for file in files:
file.close()
overhead = results[1]["mean_cpu_cores"] - (results[0]["mean_cpu_cores"] + results[2]["mean_cpu_cores"])/2
report = {"phases": results, "estimated_observer_cpu_cores": overhead,
"notes": ["Idle calibration with the running stack's normal readiness probes; no HTTP load generator.",
"Difference includes host variation, monitor clients and monitoring SQL; not a correction factor for loaded runs."]}
(args.output / "summary.json").write_text(json.dumps(report, indent=2), encoding="utf-8")
print(f"Estimated observer CPU={overhead:.4f} cores", flush=True)
if __name__ == "__main__":
main()