|
| 1 | +"""Render bar charts from pytest-benchmark JSON output. |
| 2 | +
|
| 3 | +Usage (run from this directory): |
| 4 | + ./run.sh # full pipeline |
| 5 | + uv run --with-requirements requirements.txt python chart.py results.json |
| 6 | +""" |
| 7 | + |
| 8 | +import json |
| 9 | +import sys |
| 10 | +from pathlib import Path |
| 11 | + |
| 12 | +import matplotlib.pyplot as plt |
| 13 | +import pandas as pd |
| 14 | +import seaborn as sns |
| 15 | +from matplotlib.ticker import FuncFormatter |
| 16 | + |
| 17 | +BASELINE = 'dag_cbor' |
| 18 | +HUE_ORDER = ['libipld', 'cbrrr', 'py-ipld-dag', 'dag_cbor'] |
| 19 | + |
| 20 | + |
| 21 | +def load(path): |
| 22 | + with open(path) as f: |
| 23 | + data = json.load(f) |
| 24 | + |
| 25 | + rows = [] |
| 26 | + for b in data['benchmarks']: |
| 27 | + info = b['extra_info'] |
| 28 | + lib = info['lib'] |
| 29 | + ver = info.get('version') |
| 30 | + rows.append( |
| 31 | + { |
| 32 | + 'op': info['op'], |
| 33 | + 'fixture': info['fixture'], |
| 34 | + 'lib': lib, |
| 35 | + 'lib_label': f'{lib} {ver}' if ver else lib, |
| 36 | + 'ops_per_sec': 1.0 / b['stats']['mean'], |
| 37 | + } |
| 38 | + ) |
| 39 | + |
| 40 | + return pd.DataFrame(rows) |
| 41 | + |
| 42 | + |
| 43 | +def add_relative(df, baseline): |
| 44 | + out = [] |
| 45 | + for (_, _), group in df.groupby(['op', 'fixture']): |
| 46 | + # baseline ops/sec for this (op, fixture); fall back to slowest lib if missing |
| 47 | + if baseline in group['lib'].values: |
| 48 | + base = group.loc[group['lib'] == baseline, 'ops_per_sec'].iloc[0] |
| 49 | + else: |
| 50 | + base = group['ops_per_sec'].min() |
| 51 | + |
| 52 | + for _, r in group.iterrows(): |
| 53 | + out.append({**r.to_dict(), 'rel': r['ops_per_sec'] / base}) |
| 54 | + |
| 55 | + return pd.DataFrame(out) |
| 56 | + |
| 57 | + |
| 58 | +def plot(df, op, baseline, title, outfile): |
| 59 | + sub = df[df['op'] == op].copy() |
| 60 | + if sub.empty: |
| 61 | + print(f'skipping {outfile}: no {op} results') |
| 62 | + return |
| 63 | + |
| 64 | + # preserve canonical lib ordering, but resolve to versioned labels for the legend |
| 65 | + label_for_lib = dict(zip(sub['lib'], sub['lib_label'])) |
| 66 | + labels_present = [label_for_lib[lib] for lib in HUE_ORDER if lib in label_for_lib] |
| 67 | + |
| 68 | + sns.set_theme(style='darkgrid') |
| 69 | + fig, ax = plt.subplots(figsize=(10, 7)) |
| 70 | + sns.barplot( |
| 71 | + data=sub, |
| 72 | + x='fixture', |
| 73 | + y='rel', |
| 74 | + hue='lib_label', |
| 75 | + hue_order=labels_present, |
| 76 | + ax=ax, |
| 77 | + ) |
| 78 | + |
| 79 | + ax.axhline(1.0, color='gray', linestyle='--', linewidth=1) |
| 80 | + ax.set_title(title) |
| 81 | + ax.set_xlabel('Document') |
| 82 | + ax.set_ylabel(f'Operations/second relative to {baseline}') |
| 83 | + ax.yaxis.set_major_formatter(FuncFormatter(lambda y, _: f'{int(y)}x')) |
| 84 | + ax.legend(title='library') |
| 85 | + |
| 86 | + fig.tight_layout() |
| 87 | + fig.savefig(outfile, dpi=150) |
| 88 | + print(f'wrote {outfile}') |
| 89 | + |
| 90 | + |
| 91 | +def main(): |
| 92 | + path = Path(sys.argv[1] if len(sys.argv) > 1 else 'results.json') |
| 93 | + |
| 94 | + df = load(path) |
| 95 | + baseline = BASELINE if BASELINE in df['lib'].values else df['lib'].iloc[0] |
| 96 | + df = add_relative(df, baseline) |
| 97 | + |
| 98 | + here = Path(__file__).parent |
| 99 | + plot(df, 'decode', baseline, 'deserialization', here / 'deserialization.png') |
| 100 | + plot(df, 'encode', baseline, 'serialization', here / 'serialization.png') |
| 101 | + |
| 102 | + |
| 103 | +if __name__ == '__main__': |
| 104 | + main() |
0 commit comments