Files
mtusi2026lab3/scripts/track1_experiment.py
dawuduk 99ce1b1391 Измерить отказ каждой реплики и цену добавления процессов.
Один и тот же профиль нагрузки сравнивает 1, 2 и 4 реплики при фиксированном и растущем бюджете CPU.
2026-11-07 12:46:37 +03:00

218 lines
7.7 KiB
Python

#!/usr/bin/env python3
"""Track 1: fixed API budget, then more replicas with more CPU, then one DB change.
The load profile is the public closed-loop POST generator. Same duration and
concurrency on every point. Raw JSON is written under evidence/track1/.
"""
import json
import os
import subprocess
import sys
import threading
import time
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
OUT = ROOT / "evidence" / "track1"
BASE = os.environ.get("BASE_URL", "http://127.0.0.1:8080")
DURATION = float(os.environ.get("TRACK_DURATION", "15"))
WARMUP = float(os.environ.get("TRACK_WARMUP", "5"))
CONCURRENCY = int(os.environ.get("TRACK_CONCURRENCY", "8"))
def compose(*args):
cmd = ["docker", "compose"]
if (ROOT / ".env").is_file():
cmd.extend(["--env-file", str(ROOT / ".env")])
cmd.extend(args)
proc = subprocess.run(cmd, cwd=ROOT, capture_output=True, text=True, env=os.environ.copy())
if proc.returncode != 0:
raise SystemExit((proc.stderr or proc.stdout)[-1000:])
return proc
def wait_ready(seconds=90):
import urllib.request
deadline = time.monotonic() + seconds
while time.monotonic() < deadline:
try:
with urllib.request.urlopen(BASE + "/health/ready", timeout=2) as response:
if response.status == 200:
return
except Exception:
time.sleep(0.5)
raise SystemExit("ready timeout")
def apply_profile(replicas, api_cpus, api_mem, postgres_cpus, pool):
os.environ["API_CPUS"] = api_cpus
os.environ["API_MEM"] = api_mem
os.environ["POSTGRES_CPUS"] = postgres_cpus
os.environ["DB_POOL_MAX"] = str(pool)
compose("up", "-d", "--no-deps", "--force-recreate", "postgres")
deadline = time.monotonic() + 40
while time.monotonic() < deadline:
probe = subprocess.run(
["docker", "compose", "--env-file", str(ROOT / ".env"), "exec", "-T", "postgres", "pg_isready", "-U", "messenger", "-d", "messenger"],
cwd=ROOT,
capture_output=True,
text=True,
)
if probe.returncode == 0:
break
time.sleep(0.5)
else:
raise SystemExit("postgres did not become ready")
compose(
"up",
"-d",
"--no-deps",
"--force-recreate",
"--scale",
"api=%s" % replicas,
"api",
)
wait_ready()
running = [line for line in compose("ps", "-q", "api").stdout.splitlines() if line.strip()]
if len(running) != replicas:
raise SystemExit("expected %s api containers, saw %s" % (replicas, len(running)))
def run_load(tag, warmup=False):
args = [
sys.executable,
"-X",
"utf8",
str(ROOT / "tests" / "load.py"),
"--base-url",
BASE,
"--duration",
str(WARMUP if warmup else DURATION),
"--concurrency",
str(CONCURRENCY),
]
dest = None
if not warmup:
dest = OUT / ("%s.json" % tag)
args.extend(["--output", str(dest)])
proc = subprocess.run(args, cwd=ROOT, capture_output=True, text=True)
if proc.returncode != 0:
raise SystemExit("load %s failed: %s" % (tag, (proc.stderr or proc.stdout)[-800:]))
if dest is None:
return None
return json.loads(dest.read_text(encoding="utf-8"))
def sample_resources():
stats = subprocess.run(
["docker", "stats", "--no-stream", "--format", "{{.Name}}\t{{.CPUPerc}}\t{{.MemUsage}}"],
capture_output=True,
text=True,
)
activity = compose(
"exec",
"-T",
"postgres",
"psql",
"-U",
"messenger",
"-d",
"messenger",
"-tAc",
"SELECT count(*) FROM pg_stat_activity WHERE datname='messenger'",
)
return {
"docker_stats": [line for line in stats.stdout.splitlines() if line.strip()],
"postgres_sessions": activity.stdout.strip(),
}
def measure(tag, replicas, api_cpus, api_mem, postgres_cpus, pool):
print("profile %s" % tag, flush=True)
apply_profile(replicas, api_cpus, api_mem, postgres_cpus, pool)
run_load(tag, warmup=True)
box = {}
def snapshot():
time.sleep(DURATION / 2)
box["resources"] = sample_resources()
thread = threading.Thread(target=snapshot)
thread.start()
metrics = run_load(tag)
thread.join()
point = {
"tag": tag,
"replicas": replicas,
"api_cpus_each": api_cpus,
"api_mem_each": api_mem,
"api_cpu_total": round(replicas * float(api_cpus), 3),
"postgres_cpus": postgres_cpus,
"db_pool_max_each": pool,
"db_pool_max_total": replicas * pool,
"load": metrics,
"resources": box.get("resources"),
}
(OUT / ("%s.point.json" % tag)).write_text(json.dumps(point, ensure_ascii=False, indent=2) + "\n", encoding="utf-8")
print(json.dumps({"tag": tag, "rps": metrics["successful_rps"], "errors": metrics["failed_requests"], "p95": metrics["successful_latency_ms"]["p95"]}), flush=True)
return point
def svg(points, path):
width, height = 720, 320
labels = [p["tag"] for p in points]
values = [p["load"]["successful_rps"] for p in points]
peak = max(values) or 1
left, top, plot_w, plot_h = 48, 20, width - 70, height - 70
bar_w = plot_w / len(points) * 0.62
gap = plot_w / len(points)
parts = [
'<?xml version="1.0" encoding="UTF-8"?>',
'<svg xmlns="http://www.w3.org/2000/svg" width="%d" height="%d">' % (width, height),
'<rect width="100%" height="100%" fill="white"/>',
'<text x="48" y="16" font-family="sans-serif" font-size="14">Successful RPS, closed-loop POST, concurrency %d, %ss</text>'
% (CONCURRENCY, int(DURATION)),
]
for index, (label, value) in enumerate(zip(labels, values)):
h = plot_h * value / peak
x = left + index * gap + (gap - bar_w) / 2
y = top + 12 + (plot_h - h)
parts.append('<rect x="%.1f" y="%.1f" width="%.1f" height="%.1f" fill="#2f6f4e"/>' % (x, y, bar_w, h))
parts.append(
'<text x="%.1f" y="%.1f" font-family="sans-serif" font-size="12" text-anchor="middle">%.1f</text>'
% (x + bar_w / 2, y - 4, value)
)
parts.append(
'<text x="%.1f" y="%d" font-family="sans-serif" font-size="11" text-anchor="middle">%s</text>'
% (x + bar_w / 2, height - 18, label)
)
parts.append("</svg>")
path.write_text("\n".join(parts) + "\n", encoding="utf-8")
def main():
OUT.mkdir(parents=True, exist_ok=True)
points = []
# Series A: total API budget fixed at 1.0 CPU and 512 MiB. Pool total stays 8.
points.append(measure("A1", 1, "1.0", "512m", "1.0", 8))
points.append(measure("A2", 2, "0.50", "256m", "1.0", 4))
points.append(measure("A4", 4, "0.25", "128m", "1.0", 2))
# Series B: each new replica brings the same 0.50 CPU / 256 MiB. Postgres stays at 1 CPU.
points.append(measure("B1", 1, "0.50", "256m", "1.0", 4))
points.append(measure("B2", 2, "0.50", "256m", "1.0", 4))
points.append(measure("B4", 4, "0.50", "256m", "1.0", 4))
# Candidate improvement: give the database the extra CPU the API side already has.
points.append(measure("C4-pg2", 4, "0.50", "256m", "2.0", 4))
# Candidate improvement: stop the 4 pools from opening 16 server connections.
points.append(measure("D4-pool2", 4, "0.50", "256m", "1.0", 2))
(OUT / "summary.json").write_text(json.dumps(points, ensure_ascii=False, indent=2) + "\n", encoding="utf-8")
svg(points, OUT / "rps.svg")
# Return the stand to the documented 2 x 0.50 CPU layout.
apply_profile(2, "0.50", "256m", "1.0", 4)
print("wrote %s" % (OUT / "summary.json"))
if __name__ == "__main__":
main()