forked from mtusi2026/mtusi2026lab3
91 lines
2.8 KiB
Python
91 lines
2.8 KiB
Python
#!/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"])
|
||
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, total RAM = 512 MiB, DB CPU = 2.0)",
|
||
"Серия A: реплики API при фиксированных суммарных CPU и RAM",
|
||
)
|
||
fig.tight_layout()
|
||
fig.savefig(EVIDENCE / "api_scaling.png", dpi=140)
|
||
plt.close(fig)
|
||
|
||
fig, ax = plt.subplots(figsize=(7.2, 4.4))
|
||
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, total API RAM = 512 MiB)",
|
||
"Серия B: RPS и p99 от CPU базы",
|
||
)
|
||
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()
|