Files
mtusi2026lab4/scripts/plot_research.py
T

60 lines
3.0 KiB
Python

#!/usr/bin/env python3
"""Rebuild CSV summaries and standalone plots from sanitized research artifacts."""
import csv
import json
import os
from pathlib import Path
import statistics
os.environ.setdefault('MPLCONFIGDIR','/tmp/mtusi-lab4-matplotlib')
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
ROOT=Path(__file__).resolve().parents[1]
EVIDENCE=ROOT/'evidence'
def main():
research=json.loads((EVIDENCE/'reconciliation.json').read_text())
rows=[]
for n in sorted({r['extra_orphans'] for r in research['scan_cost_runs']}):
group=[r for r in research['scan_cost_runs'] if r['extra_orphans']==n]
times=[1000*r['elapsed_seconds'] for r in group]
rows.append({'extra_orphans':n,'objects_scanned':group[0]['objects_scanned'],
'metadata_scanned':group[0]['metadata_scanned'],'repeats':len(group),
'mean_ms':round(statistics.mean(times),3),'min_ms':round(min(times),3),
'max_ms':round(max(times),3),'stdev_ms':round(statistics.stdev(times),3),
'list_calls':group[0]['s3_list_calls'],'head_calls':group[0]['s3_head_calls']})
with (EVIDENCE/'scan-cost.csv').open('w') as stream:
writer=csv.DictWriter(stream,fieldnames=list(rows[0]));writer.writeheader();writer.writerows(rows)
(EVIDENCE/'scan-cost-summary.json').write_text(json.dumps(rows,indent=2)+'\n')
plt.rcParams.update({'font.size':10,'axes.grid':True,'grid.alpha':.25})
fig,ax=plt.subplots(figsize=(7,4),layout='constrained')
ax.errorbar([r['objects_scanned'] for r in rows],[r['mean_ms'] for r in rows],
yerr=[r['stdev_ms'] for r in rows],marker='o',capsize=5)
ax.set(xlabel='S3 objects scanned',ylabel='Dry-run time, ms (mean ± sample SD)',
title='Reconciler scan cost: fixed metadata baseline, three repeats')
for suffix in ('png','svg'):
fig.savefig(EVIDENCE/('scan-cost.'+suffix),dpi=180)
plt.close(fig)
memory=json.loads((EVIDENCE/'upload-memory.json').read_text())
phases=memory['workload']['phases']
fig,axes=plt.subplots(1,2,figsize=(10,4),layout='constrained')
labels=[f"{r['size_bytes']/1048576:.2f} MiB" for r in phases]
for api in range(len(phases[0]['api_memory'])):
x=[i+(api-.5)*.3 for i in range(len(phases))]
axes[0].bar(x,[r['api_memory'][api]['sampled_peak_mib'] for r in phases],width=.3,label='API '+str(api+1))
axes[0].axhline(512,ls='--',color='tab:red',label='Per-API memory limit')
axes[0].set(xticks=range(len(labels)),xticklabels=labels,ylabel='Sampled cgroup memory peak, MiB',title='10 concurrent uploads, two rounds')
axes[0].legend(fontsize=8)
axes[1].bar(labels,[r['latency_ms']['p95'] for r in phases])
axes[1].set(ylabel='Successful upload p95, ms',title='20 successful uploads per size')
for suffix in ('png','svg'):
fig.savefig(EVIDENCE/('upload-memory.'+suffix),dpi=180)
plt.close(fig)
print(json.dumps({'scan_cost':rows,'upload_phases':[{k:v for k,v in p.items() if k!='individual_requests'} for p in phases]},indent=2))
if __name__=='__main__':
main()