#!/usr/bin/env python3 """Recreate publication/export-ready figures from the measured JSON files.""" import json from pathlib import Path import statistics import matplotlib matplotlib.use('Agg') import matplotlib.pyplot as plt ROOT = Path(__file__).resolve().parents[1] def main(): research = json.loads((ROOT/'evidence'/'research.json').read_text()) out = ROOT/'evidence'/'figures' out.mkdir(exist_ok=True) plt.rcParams.update({'font.size': 10, 'axes.spines.top': False, 'axes.spines.right': False}) fig, axes = plt.subplots(1, 3, figsize=(15, 4.6)) for series, title, color in [('fixed', 'Fixed API budget: 1 CPU / 512 MiB', '#2563eb'), ('growing', 'Growing API budget', '#0f766e')]: groups = [research['groups'][series+'-'+str(n)] for n in (1, 2, 4)] medians = [g['median_rps'] for g in groups] errors = [[m-g['min_rps'] for m,g in zip(medians,groups)], [g['max_rps']-m for m,g in zip(medians,groups)]] axes[0].errorbar([1,2,4], medians, yerr=errors, label=title, color=color, marker='o', capsize=5) axes[0].set(xlabel='API replicas', ylabel='Successful requests / second', title=f"Scale: median and min/max ({research['repetitions']} runs)", xticks=[1,2,4]) axes[0].legend(fontsize=8) runs = sorted([r for r in research['runs'] if r['group']=='saturation'], key=lambda r:r['concurrency']) axes[1].plot([r['concurrency'] for r in runs], [r['rps'] for r in runs], 'o-', color='#2563eb') right = axes[1].twinx() right.plot([r['concurrency'] for r in runs], [r['latency']['p95'] for r in runs], 's--', color='#b45309') axes[1].set(xlabel='Concurrent workers (4 API replicas)', ylabel='Successful requests / second', title='Saturation / increasing queueing') axes[1].set_xscale('log', base=2) axes[1].set_xticks([r['concurrency'] for r in runs], labels=[str(r['concurrency']) for r in runs]) right.set_ylabel('p95 of all attempts, ms', color='#b45309') groups = [research['groups']['cache-'+name] for name in ('off', 'on')] vals = [g['median_rps'] for g in groups] axes[2].bar(['Cache off','Cache on'], vals, color=['#64748b','#0f766e'], width=.6) axes[2].errorbar([0,1],vals,yerr=[[v-g['min_rps'] for v,g in zip(vals,groups)], [g['max_rps']-v for v,g in zip(vals,groups)]],fmt='none',ecolor='#111827',capsize=5) for i, val in enumerate(vals): axes[2].text(i,groups[i]['max_rps']*1.04,f'{val:.1f}',ha='center') axes[2].set(ylabel='Successful requests / second', title=f"Controlled A/B: +{(research['cache_improvement_ratio']-1)*100:.1f}% median RPS") axes[2].set_ylim(0,max(vals)*1.25) for ax in axes: ax.grid(axis='y',alpha=.2) fig.suptitle('Lab 3 / track 1: 80% history reads, 20% durable message writes', fontsize=13) fig.tight_layout() fig.savefig(out/'scaling.png',dpi=180) fig.savefig(out/'scaling.svg') plt.close(fig) # Resource evidence for the saturation series; quota share, not host CPU %. fig, ax = plt.subplots(figsize=(9,4.6)) for service,color in [('postgres','#b45309'),('lb','#64748b'),('api','#2563eb')]: values=[] for run in runs: raw=json.loads((ROOT/'evidence'/'runs'/(run['label']+'.json')).read_text()) matching=[v['quota_utilization_ratio'] for v in raw['cgroup_deltas'].values() if '-'+service+'-' in v['name']] values.append(statistics.mean(matching)*100) ax.plot([r['concurrency'] for r in runs],values,'o-',label=service,color=color) ax.set(xlabel='Concurrent workers',ylabel='CPU quota used, %',title='Shared dependency saturation (warmup + measured interval)') ax.set_xscale('log', base=2) ax.set_xticks([r['concurrency'] for r in runs], labels=[str(r['concurrency']) for r in runs]) ax.set_ylim(0, 105) ax.axhline(100,color='#111827',linestyle='--',linewidth=.8) ax.legend(); ax.grid(alpha=.2); fig.tight_layout() fig.savefig(out/'resources.png',dpi=180); fig.savefig(out/'resources.svg') plt.close(fig) print('Saved scaling and resource figures (PNG + SVG).') if __name__ == '__main__': main()