Files
mtusi2026lab3/scripts/plot_scaling.py
T

75 lines
3.0 KiB
Python

import json
import statistics
from pathlib import Path
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
ROOT = Path(__file__).resolve().parents[1]
RAW = ROOT / "results" / "scaling" / "raw.json"
OUT = ROOT / "results" / "scaling"
OUT.mkdir(parents=True, exist_ok=True)
data = json.loads(RAW.read_text(encoding="utf-8"))
def points(cfg, key):
agg = {}
for p in cfg["points"]:
agg.setdefault(p["concurrency"], []).append(p["metrics"])
xs = sorted(agg)
ys = []
for x in xs:
vals = [m[key] if key in m else m["successful_latency_ms"][key] for m in agg[x]]
ys.append(statistics.median(vals))
return xs, ys
def plot_series(series, fname, title):
cfgs = [c for c in data["configs"] if c["series"] == series]
if not cfgs:
return
fig, ax = plt.subplots(1, 3, figsize=(16, 4.2))
for c in cfgs:
label = f"{c['name']} (r={c['replicas']}, Σcpu={c['replicas']*c['api_cpu']:.1f})"
x, y = points(c, "successful_rps"); ax[0].plot(x, y, "o-", label=label)
x, y = points(c, "p95"); ax[1].plot(x, y, "o-", label=label)
x, y = points(c, "error_ratio"); ax[2].plot(x, y, "o-", label=label)
ax[0].set(xlabel="concurrency", ylabel="successful RPS", title="Throughput")
ax[1].set(xlabel="concurrency", ylabel="p95 latency, ms", title="Tail latency")
ax[2].set(xlabel="concurrency", ylabel="error ratio", title="Errors")
for a in ax:
a.grid(alpha=.3); a.legend(fontsize=7)
fig.suptitle(title)
fig.tight_layout()
fig.savefig(OUT / fname, dpi=140)
print("saved", OUT / fname)
plot_series("A", "serie_a_fixed_budget.png",
"Serie A: fixed total API budget (2.0 CPU / 1024 MiB), 1 vs 2 vs 4 replicas")
plot_series("B", "serie_b_resource_growth.png",
"Serie B: growing API CPU, Postgres fixed")
plot_series("C", "serie_c_keepalive_ab.png",
"Serie C: nginx upstream keepalive A/B (identical resources)")
plot_series("D", "serie_d_pool_saturation.png",
"Serie D: connection-pool saturation at identical CPU (2x1.0)")
lines = ["| config | replicas | Σcpu API | conc | median RPS | p50 | p95 | p99 | err |",
"|---|---|---|---|---|---|---|---|---|"]
for c in data["configs"]:
agg = {}
for p in c["points"]:
agg.setdefault(p["concurrency"], []).append(p["metrics"])
for conc in sorted(agg):
ms = agg[conc]
med = lambda f: round(statistics.median([f(m) for m in ms]), 3)
lines.append(
f"| {c['name']} | {c['replicas']} | {c['replicas']*c['api_cpu']:.1f} | {conc} | "
f"{med(lambda m: m['successful_rps'])} | "
f"{med(lambda m: m['successful_latency_ms']['p50'])} | "
f"{med(lambda m: m['successful_latency_ms']['p95'])} | "
f"{med(lambda m: m['successful_latency_ms']['p99'])} | "
f"{med(lambda m: m['error_ratio'])} |")
(OUT / "summary_table.md").write_text("\n".join(lines), encoding="utf-8")
print("saved", OUT / "summary_table.md")