{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "1066510e-b0a7-4068-8f6a-d1bc9c4381be",
"metadata": {},
"outputs": [],
"source": [
"%pip install pandas numpy matplotlib tabulate"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "512abd4b2392d6ee",
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"import re\n",
"import glob\n",
"from pathlib import Path\n",
"import pandas as pd\n",
"import matplotlib.pyplot as plt\n",
"from matplotlib.ticker import ScalarFormatter, FuncFormatter\n",
"import numpy as np\n",
"import math\n",
"from IPython.display import display\n",
"\n",
"BASELINE = \"gsobol\"\n",
"VALUE_PER_GAS = 100\n",
"GAS_PER_US = 1_000_000\n",
"\n",
"files = glob.glob(\"results_stress_test/*.log\")\n",
"if not files:\n",
" raise SystemExit(\"No files matched\")\n",
"files = sorted(files)\n",
"\n",
"rx = re.compile(r\"Value burned for [`\\w]+: *(?P<gas>\\d+)\")\n",
"def extract_value_numbers(path: str) -> list[int]:\n",
" out = []\n",
" with open(path, \"r\", encoding=\"utf-8\", errors=\"ignore\") as f:\n",
" for line in f:\n",
" if (m := rx.search(line)):\n",
" out.append(int(m[\"gas\"]))\n",
" return out\n",
"\n",
"names = [Path(f).stem for f in files]\n",
"prefix = os.path.commonprefix(names)\n",
"labels = [n[len(prefix):] for n in names]\n",
"\n",
"data = {label: pd.Series(extract_value_numbers(f), dtype=\"Int64\") \n",
" for f, label in zip(files, labels)}\n",
"\n",
"df = pd.DataFrame(data).astype(\"Float64\").div(VALUE_PER_GAS)\n",
"\n",
"def _winsorize_df(df: pd.DataFrame, k: float = 1.5) -> pd.DataFrame:\n",
" df_num = df.apply(pd.to_numeric, errors=\"coerce\").astype(\"float64\")\n",
" q1 = df_num.quantile(0.25)\n",
" q3 = df_num.quantile(0.75)\n",
" iqr = q3 - q1\n",
" lower = (q1 - k * iqr).fillna(-np.inf)\n",
" upper = (q3 + k * iqr).fillna(np.inf)\n",
" return df_num.clip(lower=lower, upper=upper, axis=1)\n",
"\n",
"def table():\n",
" desc = df.describe(percentiles=[0.05, 0.25, 0.5, 0.75, 0.95]).T\n",
" desc = desc.rename(columns={\"std\": \"std_samp\", \"50%\": \"median\"})\n",
" desc = desc.drop(columns=['count'])\n",
" \n",
" # Add a few extras\n",
" desc[\"std_pop\"] = df.std(ddof=0) # population std\n",
" desc[\"sum\"] = df.sum()\n",
" desc[\"cv\"] = df.std(ddof=1) / df.mean() # coefficient of variation\n",
" \n",
" # Reorder/round columns for a tidy view\n",
" cols = [\"mean\", \"std_samp\", \"std_pop\", \"min\", \"25%\", \"median\", \"75%\", \"max\", \"sum\", \"cv\"]\n",
" stats = desc[cols].round(3)\n",
"\n",
" nice = stats.copy()\n",
" scale_cols = ['mean', 'std_samp', 'std_pop', 'min', '25%', 'median', '75%', 'max', 'sum']\n",
" nice[scale_cols] = nice[scale_cols] / GAS_PER_US\n",
" \n",
" # Rename & order columns\n",
" nice = nice.rename(columns={\n",
" 'mean':'Mean', 'std_samp':'Std (S)', 'std_pop':'Std (P)',\n",
" 'min':'Min', '25%':'Q1', 'median':'Median', '75%':'Q3', 'max':'Max',\n",
" 'sum':'Sum', 'cv':'CV'\n",
" })\n",
" \n",
" # Add delta to a baseline\n",
" baseline_mean = stats.loc[BASELINE, 'mean']\n",
" nice['ΔMean (%)'] = (stats['mean'] - baseline_mean) / baseline_mean * 100\n",
" \n",
" # Pretty formatting (thousands separators, fixed decimals)\n",
" fmt = {\n",
" 'Mean': '{:,.1f}', 'Std (S)': '{:,.1f}', 'Std (P)': '{:,.1f}',\n",
" 'Min': '{:,.1f}', 'Q1': '{:,.1f}', 'Median': '{:,.1f}',\n",
" 'Q3': '{:,.1f}', 'Max': '{:,.1f}', 'Sum': '{:,.1f}',\n",
" 'CV': '{:.3f}', 'ΔMean (%)': '{:+.1f}%'\n",
" }\n",
" \n",
" styled = (nice.style\n",
" .format(fmt)\n",
" .set_caption('Summary - values in microseconds (µs)')\n",
" .set_properties(**{'text-align':'right', 'white-space':'nowrap'})\n",
" # Helpers for scanning\n",
" .background_gradient(subset=['Mean','Median'], cmap='Blues')\n",
" .bar(subset=['ΔMean (%)'], align='mid') if 'ΔMean (%)' in nice.columns else nice.style.format(fmt)\n",
" )\n",
" with open(\"stats.html\", \"w\", encoding=\"utf-8\") as f:\n",
" f.write(styled.to_html())\n",
" display(styled)\n",
"\n",
"def boxplot_graph():\n",
" fig, axes = plt.subplots(1, 2, figsize=(16, 6))\n",
"\n",
" # Left: fliers hidden\n",
" df.boxplot(ax=axes[0], showfliers=False)\n",
" axes[0].set_title(\"General gas consumption - fliers hidden\")\n",
"\n",
" # Right: fliers shown (default)\n",
" df.boxplot(ax=axes[1], showfliers=True)\n",
" axes[1].set_title(\"General gas consumption - fliers shown\")\n",
"\n",
" for ax in axes:\n",
" ax.set_ylabel(\"Gas units\")\n",
" ax.yaxis.set_major_formatter(ScalarFormatter(useMathText=False))\n",
" ax.ticklabel_format(axis=\"y\", style=\"plain\") # plain numbers, not 1e6\n",
" ax.tick_params(axis=\"x\", labelrotation=30)\n",
" for lbl in ax.get_xticklabels():\n",
" lbl.set_ha(\"right\")\n",
"\n",
" plt.tight_layout()\n",
" plt.savefig(\"boxplot.png\", dpi=200)\n",
" plt.show()\n",
"\n",
"def init_entrypoint_graph():\n",
" first_values = df.iloc[0] # Series\n",
" baseline = first_values[BASELINE]\n",
" \n",
" # --- order by absolute value (ascending) ---\n",
" vals = first_values.sort_values()\n",
" labels = vals.index\n",
" rel_pct = (vals / baseline - 1.0) * 100.0 # % vs baseline\n",
" \n",
" # formatters\n",
" fmt_int = FuncFormatter(lambda x, _: f\"{int(x):,}\".replace(\",\", \" \"))\n",
" fmt_pct = FuncFormatter(lambda x, _: f\"{x:+.1f}%\")\n",
" \n",
" fig, (ax1, ax2) = plt.subplots(\n",
" 2, 1, figsize=(14, 8),\n",
" gridspec_kw={\"height_ratios\": [2.0, 1.2], \"hspace\": 0.25},\n",
" )\n",
" \n",
" # ========== Absolute (gas units) ==========\n",
" bars1 = ax1.barh(labels, vals.values, edgecolor=\"black\", alpha=0.85)\n",
" ax1.set_title(\"Program upload gas consumption (sorted)\")\n",
" ax1.set_xlabel(\"Gas units\")\n",
" ax1.xaxis.set_major_formatter(fmt_int)\n",
" ax1.grid(axis=\"x\", linestyle=\"--\", alpha=0.5)\n",
" ax1.invert_yaxis() # smallest at top\n",
" # absolute values\n",
" ax1.bar_label(\n",
" bars1,\n",
" labels=[fmt_int(v) for v in vals.values],\n",
" padding=3, # gap from bar end\n",
" clip_on=False,\n",
" )\n",
"\n",
" # highlight baseline bar\n",
" for b, lab in zip(bars1, labels):\n",
" if lab == BASELINE:\n",
" b.set_alpha(1.0)\n",
" b.set_linewidth(2.0)\n",
" break\n",
" \n",
" # ========== Relative (% vs baseline) ==========\n",
" bars2 = ax2.barh(labels, rel_pct.values, edgecolor=\"black\", alpha=0.85)\n",
" ax2.set_title(f\"Δ vs baseline [{BASELINE}]\")\n",
" ax2.set_xlabel(\"Percent difference\")\n",
" ax2.xaxis.set_major_formatter(fmt_pct)\n",
" ax2.grid(axis=\"x\", linestyle=\"--\", alpha=0.5)\n",
" ax2.axvline(0, color=\"black\", linewidth=1)\n",
" ax2.invert_yaxis()\n",
" # percentages\n",
" ax2.bar_label(\n",
" bars2,\n",
" labels=[f\"{p:+.2f}%\" for p in rel_pct.values],\n",
" padding=3,\n",
" clip_on=False,\n",
" )\n",
"\n",
" plt.savefig(\"init_entrypoint.png\", dpi=200, bbox_inches=\"tight\")\n",
" plt.show()\n",
"\n",
"def heatmap():\n",
" # numeric only, keep float for imshow\n",
" df_num = df.apply(pd.to_numeric, errors=\"coerce\").astype(\"float64\")\n",
" df_win = _winsorize_df(df_num)\n",
" M_win = df_win.to_numpy().T\n",
"\n",
" fig, ax = plt.subplots(figsize=(16, 6), constrained_layout=True)\n",
"\n",
" im = ax.imshow(M_win, aspect=\"auto\", origin=\"upper\", interpolation=\"nearest\", cmap=\"coolwarm\")\n",
" ax.set_title(\"Heatmap - Winsorized (IQR)\")\n",
" ax.set_xlabel(\"Entrypoint call #\")\n",
" ax.set_ylabel(\"Configuration\")\n",
" ax.set_yticks(range(len(df_num.columns)))\n",
" ax.set_yticklabels(df_num.columns)\n",
"\n",
" # Shared colorbar\n",
" cbar = fig.colorbar(im, ax=ax)\n",
" cbar.set_label(\"Gas\")\n",
"\n",
" plt.savefig(\"heatmap.png\", dpi=200)\n",
" plt.show()\n",
"\n",
"table()\n",
"boxplot_graph()\n",
"init_entrypoint_graph()\n",
"heatmap()\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.5"
}
},
"nbformat": 4,
"nbformat_minor": 5
}