import json
import pathlib
import sys
REPO_ROOT = pathlib.Path(__file__).parent.parent
RESULTS = REPO_ROOT / "bench" / "results.json"
OUT = REPO_ROOT / "bench" / "perf-table.md"
def fmt_us(value_us: int) -> str:
if value_us >= 1000:
ms = value_us / 1000
return f"{ms:.1f} ms"
return f"{value_us} µs"
def ratio(value: int, baseline: int) -> str:
r = value / baseline
if abs(r - round(r)) < 0.05:
return f"×{r:.0f}"
return f"×{r:.1f}"
def main() -> None:
if not RESULTS.exists():
print(f"ERROR: {RESULTS} not found", file=sys.stderr)
sys.exit(1)
data = json.loads(RESULTS.read_text())
generated = data.get("generated", "unknown")
benchmarks = data["benchmarks"]
all_impls: list[str] = []
for times in benchmarks.values():
for impl in times:
if impl not in all_impls:
all_impls.append(impl)
preferred = ["ilo-jit", "ilo-vm"]
ordered = [i for i in preferred if i in all_impls] + sorted(
i for i in all_impls if i not in preferred
)
lines: list[str] = [
f"<!-- generated by scripts/gen-perf-table.py from bench/results.json ({generated}) -->",
"",
"## Performance",
"",
"Wall-clock time (lower is better). Baseline = Node.",
"",
]
header = "| Benchmark | " + " | ".join(ordered) + " |"
separator = "| --- | " + " | ".join(["---"] * len(ordered)) + " |"
lines += [header, separator]
for bench, times in sorted(benchmarks.items()):
baseline = times.get("Node") or next(iter(times.values()))
cells: list[str] = []
for impl in ordered:
if impl not in times:
cells.append("—")
else:
t = times[impl]
r = ratio(t, baseline)
cells.append(f"{fmt_us(t)} ({r})")
lines.append("| " + bench + " | " + " | ".join(cells) + " |")
lines += [""]
OUT.write_text("\n".join(lines))
print(f"Wrote {OUT}")
if __name__ == "__main__":
main()