Files
mtusi2026lab3/scripts/plot.py

109 lines
4.5 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import csv
import statistics as st
from pathlib import Path
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
ROOT = Path("evidence/track1")
OUT = ROOT / "plots"
CONFIGS = ["n1_cpu2.0", "n2_cpu1.0", "n4_cpu0.5"]
NAMES = {"n1_cpu2.0": "1 реплика", "n2_cpu1.0": "2 реплики", "n4_cpu0.5": "4 реплики"}
QUOTA = {"postgres": 100, "redis": 25, "load_balancer": 100, "api": 50}
SVC_NAMES = {"postgres": "PostgreSQL", "redis": "Redis", "load_balancer": "Nginx", "api": "API (реплика)"}
def load(d):
"""(label, rate) -> {"target": int, "rps": [...], "p95": [...]}"""
data = {}
with open(ROOT / d / "bench.csv", newline="") as f:
for r in csv.DictReader(f):
e = data.setdefault((r["label"], int(r["rate"])),
{"target": int(r["target"]), "rps": [], "p95": []})
e["rps"].append(float(r["rps"]))
e["p95"].append(float(r["p95_ms"]))
return data
def scaling(data):
"""RPS и p95 от нагрузки, линия на конфигурацию."""
fig, axes = plt.subplots(1, 2, figsize=(11, 4.2))
for ax, field, ylabel in zip(axes, ("rps", "p95"), ("успешные RPS", "p95, мс")):
for lb in CONFIGS:
pts = sorted((e["target"], e[field]) for (l, _), e in data.items() if l == lb)
x = [t for t, _ in pts]
ax.plot(x, [st.median(v) for _, v in pts], marker="o", label=NAMES[lb])
ax.fill_between(x, [min(v) for _, v in pts], [max(v) for _, v in pts], alpha=0.15)
ax.set(xlabel="целевая нагрузка, req/s", ylabel=f"{ylabel} (медиана, min–max)")
ax.grid(alpha=0.3)
axes[0].legend()
fig.suptitle("1/2/4 реплики при бюджете API 2 CPU (Nginx 0,5 CPU)")
fig.tight_layout()
fig.savefig(OUT / "scaling.png", dpi=130)
plt.close(fig)
def before_after(before, after, rate=80):
"""Nginx 0,5 CPU против 1,0 CPU на ступени rate."""
fig, axes = plt.subplots(1, 2, figsize=(11, 4.2))
w = 0.38
for ax, field, ylabel in zip(axes, ("rps", "p95"), ("успешные RPS", "p95, мс")):
for i, (data, title) in enumerate(((before, "Nginx 0,5 CPU"), (after, "Nginx 1,0 CPU"))):
vals = [data[(lb, rate)][field] for lb in CONFIGS]
med = [st.median(v) for v in vals]
err = [[m - min(v) for m, v in zip(med, vals)], [max(v) - m for m, v in zip(med, vals)]]
ax.bar([j + (i - 0.5) * w for j in range(len(CONFIGS))], med, w,
yerr=err, capsize=3, label=title)
ax.set_xticks(range(len(CONFIGS)))
ax.set_xticklabels([NAMES[c] for c in CONFIGS])
ax.set_ylabel(f"{ylabel} (медиана, min–max)")
ax.grid(axis="y", alpha=0.3)
axes[0].legend()
fig.suptitle(f"Улучшение: квота Nginx 0,5 → 1,0 CPU (ступень {rate} req/s на воркера)")
fig.tight_layout()
fig.savefig(OUT / "lb_before_after.png", dpi=130)
plt.close(fig)
def cpu_at_limit(d="generator_check", label="n4_cpu0.5", rate=80):
"""CPU контейнеров на пределе 4 реплик: медиана замеров docker stats."""
samples = {s: [] for s in QUOTA}
for f in sorted((ROOT / d / "stats").glob(f"{label}_r{rate}_run*.csv")):
with open(f, newline="") as fh:
for row in csv.reader(fh):
if len(row) < 3:
continue
for svc in QUOTA:
if svc in row[1]:
samples[svc].append(float(row[2].rstrip("%")))
break
svcs = [s for s in QUOTA if samples[s]]
med = [st.median(samples[s]) for s in svcs]
names = [SVC_NAMES[s] for s in svcs]
fig, ax = plt.subplots(figsize=(7.5, 4.2))
bars = ax.bar(names, med, 0.5)
ax.bar_label(bars, fmt="%.0f%%")
ax.plot(names, [QUOTA[s] for s in svcs], "k_", markersize=40, mew=2, label="квота CPU")
ax.set_ylabel("CPU, % одного ядра (медиана замеров)")
ax.set_title(f"CPU контейнеров на пределе: {NAMES[label]}, 40 воркеров, ступень {rate}")
ax.legend()
ax.grid(axis="y", alpha=0.3)
fig.tight_layout()
fig.savefig(OUT / "cpu_at_limit.png", dpi=130)
plt.close(fig)
def main():
OUT.mkdir(parents=True, exist_ok=True)
before, after = load("before"), load("after")
scaling(before)
before_after(before, after)
cpu_at_limit()
print(f"Графики записаны в {OUT}")
if __name__ == "__main__":
main()