#!/usr/bin/env python3
"""Render track-1 summary as dependency-free SVG charts.
Input: evidence/track1/summary.json produced by scripts/track1.py.
Output: evidence/track1/plots/*.svg. The charts plot medians of the recorded
runs; raw run files remain the source of truth.
"""
import json
import math
import sys
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
SUMMARY = ROOT / 'evidence/track1/summary.json'
OUT = ROOT / 'evidence/track1/plots'
COLORS = ['#1f77b4', '#d62728', '#2ca02c', '#ff7f0e', '#9467bd']
def esc(value):
return (str(value).replace('&', '&').replace('<', '<')
.replace('>', '>').replace('"', '"'))
def svg_header(width, height, title, subtitle):
return ['',
'')
Path(path).write_text('\n'.join(part) + '\n')
def bar_chart(path, title, subtitle, xlabels, series, ylabel, ymax=None, value_suffix=''):
width, height = 960, 560
top, bottom = 90, 100
all_values = [value for _, values in series for value in values if value is not None]
if ymax is None:
ymax = max(all_values) * 1.2 if all_values else 1.0
part, positions, plot_w, plot_h = axes(width, height, xlabels, ymax, ylabel)
group_width = plot_w / max(1, len(positions))
bar_width = group_width * 0.7 / max(1, len(series))
for series_index, (name, values) in enumerate(series):
color = COLORS[series_index % len(COLORS)]
for index, value in enumerate(values):
if value is None:
continue
x = positions[index] - group_width * 0.35 + series_index * bar_width
bar_h = value / ymax * plot_h
y = top + plot_h - bar_h
part.append('' % (x, y, bar_width - 3, bar_h, color))
part.append('%s%s' % (x + bar_width / 2, y - 6, esc(('%.1f' % value)), value_suffix))
legend_y = height - 46
legend_x = 110 + series_index * 230
part.append('' % (legend_x, legend_y - 9, color))
part.append('%s' % (legend_x + 24, legend_y + 1, esc(name)))
part.append('')
Path(path).write_text('\n'.join(part) + '\n')
def get(group, key, field, default=None):
if key not in group:
return default
value = group[key].get(field, default)
return default if value is None else value
def main():
if not SUMMARY.exists():
raise SystemExit('missing %s; run scripts/track1.py first' % SUMMARY)
data = json.loads(SUMMARY.read_text())
OUT.mkdir(parents=True, exist_ok=True)
fixed = data['medians']['fixed_total']
growth = data['medians']['resource_growth']
improvement = data['medians']['improvement']
fixed_keys = ['1', '2', '4']
growth_keys = ['0.4', '0.7', '0.9']
revisions = sorted({run['config'].get('revision') for run in data['runs']['fixed_total']})
subtitle = 'Colima 2 vCPU / 2 GiB, one Docker host; medians of %d alternating runs; revision %s' % (
len(data['runs']['fixed_total']) // max(1, len(fixed_keys)), ', '.join(filter(None, revisions))[:12])
line_chart(OUT / 'fixed-rps.svg',
'Fixed total API CPU/RAM: 1 vs 2 vs 4 replicas',
subtitle + '; total API 0.8 vCPU / 512 MiB',
[key + ' replica' + ('' if key == '1' else 's') for key in fixed_keys],
[('successful RPS', [get(fixed, key, 'successful_rps', 0) for key in fixed_keys])],
'HTTP 2xx acknowledgements / second', value_suffix='')
line_chart(OUT / 'fixed-latency.svg',
'Latency at fixed total CPU: more replicas, same budget',
subtitle + '; successful requests only',
[key + ' replica' + ('' if key == '1' else 's') for key in fixed_keys],
[('p50 ms', [get(fixed, key, 'p50_ms', 0) for key in fixed_keys]),
('p95 ms', [get(fixed, key, 'p95_ms', 0) for key in fixed_keys]),
('p99 ms', [get(fixed, key, 'p99_ms', 0) for key in fixed_keys])],
'milliseconds', value_suffix=' ms')
bar_chart(OUT / 'saturation.svg',
'Where the fixed CPU budget goes (median utilization)',
subtitle + '; cgroup CPU usage / cgroup CPU limit',
[key + ' replica' + ('' if key == '1' else 's') for key in fixed_keys],
[('API', [100 * get(fixed, key, 'api_utilization', 0) for key in fixed_keys]),
('PostgreSQL', [100 * get(fixed, key, 'db_utilization', 0) for key in fixed_keys]),
('HAProxy', [100 * (get(fixed, key, 'lb_used_cores', 0) / 0.2) for key in fixed_keys])],
'percent of container CPU limit', ymax=110, value_suffix='%')
line_chart(OUT / 'growth-rps.svg',
'Resource-growth series at 4 replicas',
subtitle + '; shared PostgreSQL/Valkey/LB limits fixed',
['API total %.1f vCPU' % float(key) for key in growth_keys],
[('successful RPS', [get(growth, key, 'successful_rps', 0) for key in growth_keys])],
'HTTP 2xx acknowledgements / second')
bar_chart(OUT / 'growth-utilization.svg',
'Resource-growth series: utilization of the fixed shared backend',
subtitle + '; cgroup CPU usage / cgroup CPU limit',
['API total %.1f vCPU' % float(key) for key in growth_keys],
[('API', [100 * get(growth, key, 'api_utilization', 0) for key in growth_keys]),
('PostgreSQL', [100 * get(growth, key, 'db_utilization', 0) for key in growth_keys])],
'percent of container CPU limit', ymax=110, value_suffix='%')
base = get(improvement, 'baseline', 'successful_rps', 0)
opt = get(improvement, 'optimized', 'successful_rps', 0)
bar_chart(OUT / 'improvement-rps.svg',
'Confirmed improvement at 4x0.2 vCPU: one atomic statement',
'Baseline: explicit transaction + message INSERT + idempotency INSERT. Final: one data-modifying CTE. '
'Median of alternating runs; +%.1f%% RPS.' % (((opt - base) / base * 100.0) if base else 0.0),
['baseline', 'optimized'],
[('successful RPS', [base, opt])],
'HTTP 2xx acknowledgements / second')
bar_chart(OUT / 'improvement-latency.svg',
'Confirmed improvement: p95 latency at the same limit',
'Successful POST requests; lower is better; same alternating runs',
['baseline', 'optimized'],
[('p95 ms', [get(improvement, 'baseline', 'p95_ms', 0),
get(improvement, 'optimized', 'p95_ms', 0)])],
'milliseconds', value_suffix=' ms')
print('wrote', OUT)
if __name__ == '__main__':
main()