#!/usr/bin/env python3 """Render the three-repeat scale matrix as small, dependency-free SVG plots.""" import argparse import csv import json from pathlib import Path import statistics from benchmark import validate_matrix COLORS = {1: "#2563eb", 2: "#16a34a", 4: "#dc2626"} def svg(mode, metric, rows, output): width, height = 720, 420 left, right, top, bottom = 75, 25, 35, 65 plot_w, plot_h = width - left - right, height - top - bottom rates = sorted({row["rate"] for row in rows}) max_y = max((row[metric] or 0 for row in rows), default=0) * 1.1 or 1 x = lambda rate: left + (rate - rates[0]) / (rates[-1] - rates[0]) * plot_w y = lambda value: top + plot_h * (1 - value / max_y) label = "Успешные запросы/с" if metric == "successful_rps" else "p95, мс" parts = [f'', '', f'{mode}: {label}'] for step in range(5): value = max_y * step / 4 py = y(value) parts += [f'', f'{value:.0f}'] for rate in rates: px = x(rate) parts += [f'{rate}'] for replicas in (1, 2, 4): points = [(row["rate"], row[metric]) for row in rows if row["replicas"] == replicas and row[metric] is not None] path = " ".join(f'{"M" if index == 0 else "L"}{x(rate):.1f},{y(value):.1f}' for index, (rate, value) in enumerate(points)) parts.append(f'') for rate, value in points: parts.append(f'') legend_x = left + 180 * (replicas == 2) + 360 * (replicas == 4) parts.append(f'{replicas} API') parts += [f'Заданная интенсивность, запросов/с', ''] output.write_text("\n".join(parts) + "\n", encoding="utf-8") def main(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("input", type=Path) args = parser.parse_args() rows = [json.loads(line) for line in args.input.read_text().splitlines() if line.strip()] validate_matrix(rows) summary = [] for mode in ("fixed", "growth"): for replicas in (1, 2, 4): for rate in sorted({row["rate"] for row in rows}): group = [row for row in rows if (row["mode"], row["replicas"], row["rate"]) == (mode, replicas, rate)] def latency(p): values = [row["successful_latency_ms"][p] for row in group if row["successful_latency_ms"][p] is not None] return round(statistics.median(values), 3) if values else None summary.append({"mode": mode, "replicas": replicas, "rate": rate, "successful_rps": round(statistics.median(row["successful_rps"] for row in group), 3), "error_ratio": round(statistics.median(row["error_ratio"] for row in group), 6), "p50_ms": latency("p50"), "p95_ms": latency("p95"), "p99_ms": latency("p99"), "api_cpu_percent": round(statistics.median(row["docker"]["api"]["avg_cpu_percent"] for row in group), 3), "postgres_cpu_percent": round(statistics.median(row["docker"]["postgres"]["avg_cpu_percent"] for row in group), 3), "lb_cpu_percent": round(statistics.median(row["docker"]["lb"]["avg_cpu_percent"] for row in group), 3), "api_memory_mib": round(statistics.median(row["docker"]["api"]["max_memory_mib"] for row in group), 3), "postgres_memory_mib": round(statistics.median(row["docker"]["postgres"]["max_memory_mib"] for row in group), 3), "lb_memory_mib": round(statistics.median(row["docker"]["lb"]["max_memory_mib"] for row in group), 3), "pool_wait_ms": round(statistics.median(row["pool"]["wait_ms"] for row in group), 3), "queue_dropped": statistics.median(row["queue_dropped"] for row in group)}) with (args.input.parent / "summary.csv").open("w", newline="") as file: writer = csv.DictWriter(file, fieldnames=summary[0].keys(), lineterminator="\n") writer.writeheader() writer.writerows(summary) for mode in ("fixed", "growth"): group = [row for row in summary if row["mode"] == mode] svg(mode, "successful_rps", group, args.input.parent / f"{mode}-rps.svg") svg(mode, "p95_ms", group, args.input.parent / f"{mode}-p95.svg") if __name__ == "__main__": main()