From 95f15c1e9b498288cb6b15230ebaa2d4923b107d Mon Sep 17 00:00:00 2001 From: Samuel Chen <119898205+sacchen@users.noreply.github.com> Date: Sat, 11 Apr 2026 03:51:58 -0700 Subject: [PATCH] chore: add scripts/bench.sh for startup latency measurement Measures warm p50/p95 over 12 runs for four representative invocations. Use before/after any startup optimization to capture baselines. Closes #21 investigation phase. See issue for profiling findings. Co-Authored-By: Claude Sonnet 4.6 --- scripts/bench.sh | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100755 scripts/bench.sh diff --git a/scripts/bench.sh b/scripts/bench.sh new file mode 100755 index 0000000..0019f7b --- /dev/null +++ b/scripts/bench.sh @@ -0,0 +1,40 @@ +#!/usr/bin/env bash +# Measure warm startup latency for the installed phil binary. +# +# Usage: +# scripts/bench.sh # uses 'phil' on PATH +# scripts/bench.sh /path/to/phil +# +# Outputs p50 and p95 over 12 warm runs for three representative invocations. +# Run before and after any startup optimization to capture before/after. +set -euo pipefail + +PHIL="${1:-phil}" + +if ! command -v "$PHIL" &>/dev/null && [ ! -x "$PHIL" ]; then + echo "error: '$PHIL' not found — install with: uv tool install philcalc" >&2 + exit 1 +fi + +python3 - "$PHIL" <<'EOF' +import subprocess, sys, time + +def bench(label, cmd, n=12): + subprocess.run(cmd, capture_output=True) # warm-up + results = [] + for _ in range(n): + t0 = time.perf_counter() + subprocess.run(cmd, capture_output=True) + results.append(time.perf_counter() - t0) + results.sort() + p50 = results[n // 2] * 1000 + p95 = results[min(int(n * 0.95), n - 1)] * 1000 + print(f" {label:<28} p50={p50:5.0f}ms p95={p95:5.0f}ms") + +phil = sys.argv[1] +print(f"[bench] {phil}") +bench(":version", [phil, ":version"]) +bench("2+2", [phil, "2+2"]) +bench("d(x^2, x)", [phil, "d(x^2, x)"]) +bench("solve(x^2-4, x)", [phil, "solve(x^2-4, x)"]) +EOF