Files
mtusi2026lab2/student-tests/plot_research.py
T

75 lines
4.0 KiB
Python

"""Plot measured coursework results; this is data analysis, not application code."""
import argparse
import json
from pathlib import Path
from statistics import mean
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
parser = argparse.ArgumentParser()
parser.add_argument('summary', type=Path)
parser.add_argument('--pool-summary', type=Path)
args = parser.parse_args()
data = json.loads(args.summary.read_text())
if data.get('complete') is not True:
raise SystemExit('Only a completed, cleaned-up run can be plotted')
plt.rcParams.update({'svg.fonttype': 'none', 'font.size': 10})
if 'dataset' in data:
fig, axes = plt.subplots(1, 2, figsize=(9, 3.5), constrained_layout=True)
modes = ['chat_only', 'history']
for axis, key, title in zip(axes, ['read', 'write'], ['Deep history page (51 rows)', 'Insert 1,000 messages']):
values = [data['plans'][mode][key]['Execution Time'] for mode in modes]
axis.bar(['chat_id', 'chat_id + time + id'], values, color=['#6b7280', '#2563eb'])
axis.set_ylabel('Execution time, ms')
axis.set_title(title)
if key == 'read':
axis.set_yscale('log')
for index, value in enumerate(values):
axis.annotate(f'{value:.3f}', (index, value), xytext=(0, 4), textcoords='offset points', ha='center')
fig.suptitle('100,000 skewed messages; one EXPLAIN ANALYZE per index')
fig.savefig(args.summary.parent / 'comparison.svg')
plt.close(fig)
else:
if data.get('valid') is not True:
raise SystemExit('Invalid measurements')
rows = data['samples']
fig, axes = plt.subplots(1, 2, figsize=(10, 3.8), constrained_layout=True)
for phase, label, color in [('fixed', 'Fixed total API CPU/RAM', '#2563eb'), ('growing', 'Resources grow with replicas', '#c2410c')]:
groups = [[r for r in rows if r['phase'] == phase and r['replicas'] == count] for count in [1, 2, 4]]
if not all(groups):
continue
for axis, metric in zip(axes, ['rps', 'p95']):
values = [[r['metrics']['successful_rps'] if metric == 'rps' else r['metrics']['successful_latency_ms']['p95'] for r in group] for group in groups]
centers = [mean(value) for value in values]
errors = [[center - min(value) for center, value in zip(centers, values)], [max(value) - center for center, value in zip(centers, values)]]
axis.errorbar([1, 2, 4], centers, yerr=errors, marker='o', capsize=4, label=label, color=color)
for axis, title in zip(axes, ['Successful requests / second', 'p95 of successful requests, ms']):
axis.set_title(title)
axis.set_xlabel('API replicas')
axis.set_xticks([1, 2, 4])
axis.grid(alpha=.2)
axis.legend(fontsize=8)
fig.suptitle('Closed loop, concurrency 16, 5 seconds; mean and range of two runs')
fig.savefig(args.summary.parent / 'scaling.svg')
plt.close(fig)
pool_data = json.loads(args.pool_summary.read_text()) if args.pool_summary else data
if pool_data.get('complete') is not True:
raise SystemExit('Incomplete pool experiment')
pools = [[r for r in pool_data['samples'] if r['phase'] == 'pool' and r['pool'] == size] for size in [8, 1]]
fig, axes = plt.subplots(1, 2, figsize=(8, 3.5), constrained_layout=True)
for axis, metric in zip(axes, ['rps', 'p95']):
values = [[r['metrics']['successful_rps'] if metric == 'rps' else r['metrics']['successful_latency_ms']['p95'] for r in group] for group in pools]
centers = [mean(value) for value in values]
axis.bar(['pool = 8', 'pool = 1'], centers, color=['#6b7280', '#2563eb'])
axis.set_ylabel('Successful req/s' if metric == 'rps' else 'p95, ms')
axis.set_ylim(bottom=0)
for index, center in enumerate(centers):
axis.annotate(f'{center:.2f}', (index, center), xytext=(0, 4), textcoords='offset points', ha='center')
fig.suptitle('One hot sender; same single-replica CPU/RAM budget')
fig.savefig(args.summary.parent / 'pool.svg')
plt.close(fig)
print('Saved standalone SVG research figures')