Files
mtusi2026lab3/scripts/plot_scale.py
T

87 lines
5.3 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
"""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'<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 {width} {height}" role="img" aria-label="{mode}: {label}">',
'<rect width="100%" height="100%" fill="white"/>',
f'<text x="{left}" y="23" font-family="sans-serif" font-size="16">{mode}: {label}</text>']
for step in range(5):
value = max_y * step / 4
py = y(value)
parts += [f'<line x1="{left}" x2="{width-right}" y1="{py:.1f}" y2="{py:.1f}" stroke="#ddd"/>',
f'<text x="{left-8}" y="{py+4:.1f}" text-anchor="end" font-family="sans-serif" font-size="11">{value:.0f}</text>']
for rate in rates:
px = x(rate)
parts += [f'<text x="{px:.1f}" y="{height-35}" text-anchor="middle" font-family="sans-serif" font-size="11">{rate}</text>']
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'<path d="{path}" fill="none" stroke="{COLORS[replicas]}" stroke-width="2.5"/>')
for rate, value in points:
parts.append(f'<circle cx="{x(rate):.1f}" cy="{y(value):.1f}" r="4" fill="{COLORS[replicas]}"/>')
legend_x = left + 180 * (replicas == 2) + 360 * (replicas == 4)
parts.append(f'<text x="{legend_x}" y="{height-10}" fill="{COLORS[replicas]}" font-family="sans-serif" font-size="12">{replicas} API</text>')
parts += [f'<text x="{left+plot_w/2}" y="{height-48}" text-anchor="middle" font-family="sans-serif" font-size="11">Заданная интенсивность, запросов/с</text>', '</svg>']
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()