# -*- 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'', '', f'{_esc(title)}', ] for i in range(ticks + 1): y = bottom - plot_h * i / ticks val = ymax * i / ticks lines.append(f'') lines.append( f'{val:.0f}' ) lines.append(f'') lines.append(f'') 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'') lines.append( f'{value:.1f}' ) lines.append( f'{_esc(label)}' ) lines.append( f'{_esc(xlabel)}' ) lines.append( f'successful RPS' ) lines.append("\n") path.write_text("\n".join(lines), encoding="utf-8") def _esc(text: str) -> str: return text.replace("&", "&").replace("<", "<").replace(">", ">") 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()