Files
mtusi2026lab3/scripts/render_track1_svg.py

111 lines
4.4 KiB
Python

# -*- coding: utf-8 -*-
"""Перерисовать графики трека 1 из evidence/track1.json."""
from __future__ import annotations
import json
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
EVIDENCE = ROOT / "evidence"
JSON_PATH = EVIDENCE / "track1.json"
def bar_chart(path: Path, title: str, xlabel: str, bars: list[tuple[str, float]], color: str) -> None:
width, height = 820, 420
left, right, top, bottom = 70, 790, 50, 350
plot_w = right - left
plot_h = bottom - top
values = [v for _, v in bars] or [1.0]
ymax = max(values) * 1.15 if max(values) else 1.0
n = len(bars)
gap = plot_w / n
bar_w = gap * 0.48
ticks = 4
lines = [
f'<svg xmlns="http://www.w3.org/2000/svg" width="{width}" height="{height}" viewBox="0 0 {width} {height}">',
'<rect width="100%" height="100%" fill="white"/>',
f'<text x="{width/2:.1f}" y="28" text-anchor="middle" font-size="16" font-weight="700" font-family="Segoe UI, Arial">{_esc(title)}</text>',
]
for i in range(ticks + 1):
y = bottom - plot_h * i / ticks
val = ymax * i / ticks
lines.append(f'<line x1="{left}" y1="{y:.1f}" x2="{right}" y2="{y:.1f}" stroke="#e2e8f0"/>')
lines.append(
f'<text x="{left-8}" y="{y+4:.1f}" text-anchor="end" font-size="11" fill="#4a5568" font-family="Segoe UI, Arial">{val:.0f}</text>'
)
lines.append(f'<line x1="{left}" y1="{top}" x2="{left}" y2="{bottom}" stroke="#2d3748"/>')
lines.append(f'<line x1="{left}" y1="{bottom}" x2="{right}" y2="{bottom}" stroke="#2d3748"/>')
for i, (label, value) in enumerate(bars):
h = 0 if ymax <= 0 else plot_h * value / ymax
x = left + gap * i + (gap - bar_w) / 2
y = bottom - h
lines.append(f'<rect x="{x:.1f}" y="{y:.1f}" width="{bar_w:.1f}" height="{h:.1f}" fill="{color}"/>')
lines.append(
f'<text x="{x + bar_w/2:.1f}" y="{y-8:.1f}" text-anchor="middle" font-size="13" font-family="Segoe UI, Arial">{value:.1f}</text>'
)
lines.append(
f'<text x="{x + bar_w/2:.1f}" y="392" text-anchor="middle" font-size="12" font-family="Segoe UI, Arial">{_esc(label)}</text>'
)
lines.append(
f'<text x="{width/2:.1f}" y="412" text-anchor="middle" font-size="12" fill="#4a5568" font-family="Segoe UI, Arial">{_esc(xlabel)}</text>'
)
lines.append(
f'<text x="16" y="200" text-anchor="middle" font-size="12" fill="#4a5568" font-family="Segoe UI, Arial" transform="rotate(-90 16 200)">successful RPS</text>'
)
lines.append("</svg>\n")
path.write_text("\n".join(lines), encoding="utf-8")
def _esc(text: str) -> str:
return text.replace("&", "&amp;").replace("<", "&lt;").replace(">", "&gt;")
def rps(item: dict) -> float:
return float(item["successful_rps"] or 0)
def main() -> None:
data = json.loads(JSON_PATH.read_text(encoding="utf-8"))
scale = data.get("scale_out_own_cpu") or []
half = [x for x in scale if abs(x["cpus_each"] - 0.5) < 1e-9]
full = [x for x in scale if abs(x["cpus_each"] - 1.0) < 1e-9]
if half:
bar_chart(
EVIDENCE / "track1_scale_out.svg",
"Each replica keeps 0.50 CPU (like another server)",
"Replicas; total CPU grows 0.5 → 1.0 → 2.0",
[(str(x["replicas"]), rps(x)) for x in half],
"#276749",
)
elif full:
bar_chart(
EVIDENCE / "track1_scale_out.svg",
"Each replica keeps 1.0 CPU (like another server)",
"Replicas; total CPU grows with copies",
[(str(x["replicas"]), rps(x)) for x in full],
"#276749",
)
fixed = data.get("fixed_total_budget") or []
if fixed:
bar_chart(
EVIDENCE / "track1_fixed_budget.svg",
"Fixed total 1.0 CPU split across replicas",
"Replica count at the same total CPU",
[(str(x["replicas"]), rps(x)) for x in fixed],
"#c53030",
)
growing = data.get("growing_resources") or []
if growing:
bar_chart(
EVIDENCE / "track1_growing_cpu.svg",
"Two API replicas, growing CPU/RAM budget",
"Total CPU for two replicas",
[(("%.2f CPU" % x["total_cpu"]).replace(".", ","), rps(x)) for x in growing],
"#2c5282",
)
print("wrote SVGs next to", JSON_PATH)
if __name__ == "__main__":
main()