#!/usr/bin/env python3 """Plot track-1 JSON files written by scripts/run_track1.py.""" import json from pathlib import Path import matplotlib matplotlib.use("Agg") import matplotlib.pyplot as plt ROOT = Path(__file__).resolve().parents[1] EVIDENCE = ROOT / "evidence" def load_series(letter): rows = [] for path in sorted(EVIDENCE.glob("track1-*.json")): if path.name.endswith(".load.json"): continue data = json.loads(path.read_text()) if data.get("series") != letter: continue load = data["load"] lat = load["successful_latency_ms"] rows.append( { "replicas": data["api_replicas"], "db_cpu": data["db_cpu_limit"], "pool": data["pg_pool"], "nginx": str(data["nginx_worker_processes"]), "rps": load["successful_rps"], "p50": lat["p50"], "p95": lat["p95"], "p99": lat["p99"], "errors": load["failed_requests"], } ) return rows def dual_axis(ax, xs, rps, p99, xlabel, title): ax.plot(xs, rps, marker="o", color="#1f4e79", label="successful RPS") ax.set_xlabel(xlabel) ax.set_ylabel("successful RPS") ax.set_title(title) twin = ax.twinx() twin.plot(xs, p99, marker="s", color="#b85c38", label="p99, ms") twin.set_ylabel("p99, ms") lines = ax.get_lines() + twin.get_lines() ax.legend(lines, [line.get_label() for line in lines], loc="best") ax.grid(True, axis="y", alpha=0.3) return twin def main(): series_a = sorted(load_series("A"), key=lambda row: row["replicas"]) series_b = sorted(load_series("B"), key=lambda row: row["db_cpu"]) repeat = load_series("R") tuned = load_series("P") if not series_a or not series_b: raise SystemExit("need series A and B JSON in evidence/") fig, ax = plt.subplots(figsize=(7.2, 4.4)) dual_axis( ax, [row["replicas"] for row in series_a], [row["rps"] for row in series_a], [row["p99"] for row in series_a], "API replicas (total CPU = 1.0, DB CPU = 2.0)", "Серия A: реплики API при фиксированном суммарном CPU", ) fig.tight_layout() fig.savefig(EVIDENCE / "api_scaling.png", dpi=140) plt.close(fig) fig, ax = plt.subplots(figsize=(7.2, 4.4)) twin = dual_axis( ax, [row["db_cpu"] for row in series_b], [row["rps"] for row in series_b], [row["p99"] for row in series_b], "PostgreSQL CPU limit (4 API × 0.25 CPU)", "Серия B: CPU базы и парный прогон тюнинга", ) if repeat and tuned: base, point = repeat[-1], tuned[-1] ax.scatter([base["db_cpu"]], [base["rps"]], marker="x", s=80, color="#1f4e79", zorder=5, label="repeat baseline RPS") ax.scatter([point["db_cpu"] + 0.08], [point["rps"]], marker="*", s=160, color="#1f4e79", zorder=5, label="tuned RPS") twin.scatter([base["db_cpu"]], [base["p99"]], marker="x", s=80, color="#b85c38", zorder=5, label="repeat baseline p99") twin.scatter([point["db_cpu"] + 0.08], [point["p99"]], marker="*", s=160, color="#b85c38", zorder=5, label="tuned p99") handles = ax.get_lines() + twin.get_lines() + list(ax.collections) + list(twin.collections) ax.legend(handles, [h.get_label() for h in handles], loc="best", fontsize=8) fig.tight_layout() fig.savefig(EVIDENCE / "db_scaling.png", dpi=140) plt.close(fig) print("wrote evidence/api_scaling.png evidence/db_scaling.png") if __name__ == "__main__": main()