Files
mtusi2026lab3/research/report.py
T

54 lines
2.9 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
"""Сводит итоги k6 (*.json) и снимки CPU (*.cpu) в Markdown-таблицу и SVG-график."""
import json
import re
import sys
from pathlib import Path
raw = Path(sys.argv[1] if len(sys.argv) > 1 else 'evidence/track1/raw')
rows = []
for f in sorted(raw.glob('*.json')):
m = re.match(r'n(\d+)_c([\d.]+)_p(\d+)_r(\d+)', f.stem)
if not m or not f.read_text().strip():
continue
d = json.loads(f.read_text())
cpu = {}
for line in (f.with_suffix('.cpu').read_text().splitlines() if f.with_suffix('.cpu').exists() else []):
name, pct = line.split()
role = name.split('-')[2]
cpu[role] = cpu.get(role, 0) + float(pct.rstrip('%'))
rows.append((f'{m[1]}×{m[2]} CPU, pool {m[3]}', int(m[4]), d, cpu))
rows.sort(key=lambda r: (r[0], r[1]))
print('| Конфигурация API | Offered RPS | Успешные RPS | Ошибки | Dropped | p50 мс | p95 мс | p99 мс | CPU api % | CPU postgres % | CPU lb % |')
print('|---|---|---|---|---|---|---|---|---|---|---|')
for cfg, rate, d, cpu in rows:
l = d['latency_ms']
print(f"| {cfg} | {rate} | {d['successful_rps']} | {d['failed']} | {d['dropped_iterations']} | {l['p50']:.1f} | {l['p95']:.1f} | {l['p99']:.1f} | "
f"{cpu.get('api', 0):.0f} | {cpu.get('postgres', 0):.0f} | {cpu.get('lb', 0):.0f} |")
# SVG: успешные RPS от поданной нагрузки, по линии на конфигурацию.
W, H, P = 640, 380, 50
series = {}
for cfg, rate, d, _ in rows:
series.setdefault(cfg, []).append((rate, d['successful_rps']))
xmax = max(r for _, r, _, _ in rows)
ymax = xmax
sx = lambda x: P + x / xmax * (W - 2 * P)
sy = lambda y: H - P - y / ymax * (H - 2 * P)
colors = ['#2a6fdb', '#d9480f', '#2b8a3e', '#862e9c', '#495057']
svg = [f'<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 {W} {H}" font-family="sans-serif" font-size="12">',
'<rect width="100%" height="100%" fill="white"/>',
f'<line x1="{P}" y1="{H-P}" x2="{W-P}" y2="{H-P}" stroke="black"/><line x1="{P}" y1="{P}" x2="{P}" y2="{H-P}" stroke="black"/>',
f'<line x1="{sx(0)}" y1="{sy(0)}" x2="{sx(xmax)}" y2="{sy(xmax)}" stroke="#aaa" stroke-dasharray="4"/>',
f'<text x="{W/2}" y="{H-12}" text-anchor="middle">offered RPS</text>',
f'<text x="14" y="{H/2}" transform="rotate(-90 14 {H/2})" text-anchor="middle">successful RPS</text>']
for t in range(0, xmax + 1, 400):
svg.append(f'<text x="{sx(t)}" y="{H-P+16}" text-anchor="middle">{t}</text><text x="{P-6}" y="{sy(t)+4}" text-anchor="end">{t}</text>')
for i, (cfg, pts) in enumerate(series.items()):
c = colors[i % len(colors)]
svg.append(f'<polyline fill="none" stroke="{c}" stroke-width="2" points="{" ".join(f"{sx(x):.0f},{sy(y):.0f}" for x, y in pts)}"/>')
svg.append(f'<text x="{P+10}" y="{P+16*i}" fill="{c}">{cfg}</text>')
svg.append('</svg>')
(raw.parent / 'throughput.svg').write_text('\n'.join(svg))