Files
mtusi2026lab3/research/plot_scaling.py
T

58 lines
3.1 KiB
Python
Raw 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.
#!/usr/bin/env python3
"""Графики трека 1 из evidence/scaling_*.json: python research/plot_scaling.py --out evidence"""
import argparse
import json
import os
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt # noqa: E402
def load(out, series, tag):
path = os.path.join(out, "scaling_%s_%s.json" % (series, tag))
return json.load(open(path, encoding="utf-8")) if os.path.exists(path) else None
def main():
ap = argparse.ArgumentParser()
ap.add_argument("--out", default="evidence")
out = ap.parse_args().out
fig, ax = plt.subplots(1, 3, figsize=(17, 5))
# A: фиксированный суммарный CPU
for tag, color, shift in (("base", "#1565c0", -0.2), ("improved", "#2e7d32", 0.2)):
d = load(out, "A", tag)
if not d:
continue
names = [r["config"] for r in d["results"]]
ax[0].bar([i + shift for i in range(len(names))], [r["total_rps"] for r in d["results"]], width=0.4, color=color,
label={"base": "до (несколько запросов)", "improved": "после (функция БД)"}[tag])
ax[0].set_xticks(range(len(names)))
ax[0].set_xticklabels(["%s" % n.replace("x", " × ") + " CPU" for n in names])
ax[0].set_title("A: суммарно 2.0 CPU на API (число реплик × CPU)"); ax[0].set_ylabel("успешных запросов/с"); ax[0].legend()
# B: рост ресурсов
for tag, color, label in (("base", "#1565c0", "БД 2 CPU, до"), ("base_pg4", "#ef6c00", "БД 4 CPU, до"), ("improved", "#2e7d32", "БД 2 CPU, после")):
d = load(out, "B", tag)
if not d:
continue
ax[1].plot([r["replicas"] for r in d["results"]], [r["total_rps"] for r in d["results"]], marker="o", color=color, label=label)
ax[1].set_xscale("log", base=2); ax[1].set_xticks([1, 2, 4, 8]); ax[1].set_xticklabels(["1", "2", "4", "8"])
ax[1].set_title("B: реплики по 1.0 CPU — насыщение"); ax[1].set_xlabel("число API-реплик"); ax[1].set_ylabel("успешных запросов/с"); ax[1].legend()
# CPU компонентов в серии B (до)
d = load(out, "B", "base")
if d:
reps = [r["replicas"] for r in d["results"]]
for svc, color in (("postgres", "#c62828"), ("api", "#1565c0"), ("lb", "#6a1b9a")):
ax[2].plot(reps, [r["docker_stats"].get(svc, {}).get("avg_cpu_percent_per_container") for r in d["results"]], marker="s", color=color,
label={"postgres": "PostgreSQL (лимит 200 %)", "api": "API, на реплику (лимит 100 %)", "lb": "HAProxy (лимит 200 %)"}[svc])
ax[2].set_xscale("log", base=2); ax[2].set_xticks([1, 2, 4, 8]); ax[2].set_xticklabels(["1", "2", "4", "8"])
ax[2].set_title("B (до): загрузка CPU компонентов, % одного ядра"); ax[2].set_xlabel("число API-реплик"); ax[2].legend()
fig.tight_layout()
fig.savefig(os.path.join(out, "scaling.png"), dpi=110)
print("saved")
if __name__ == "__main__":
main()