Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 39 additions & 9 deletions .github/scripts/id-bench-compare.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,16 @@
# A benchmark is flagged when its current median is more than
# BENCH_REGRESSION_THRESHOLD_PCT percent (default 20) above baseline.
# The report goes to stdout; the caller appends it to the GitHub job
# summary. This script ALWAYS exits 0 — slice 3 is non-blocking by
# design: the report informs, it never gates a PR.
# summary. This script exits 0 whenever it produces a report — slice 3
# is non-blocking by design: the report informs, it never gates a PR.
# The only non-zero exits are usage errors and a non-numeric
# BENCH_REGRESSION_THRESHOLD_PCT (both exit 2, before any report):
# a broken configuration must be loud, never silently read as
# "flag nothing" (#1619).
#
# Rows that cannot be compared — the unit changed between baseline
# and current, or the baseline median is zero — are reported as
# incomparable: no percent change, no ok/regression verdict (#1619).
set -euo pipefail

threshold="${BENCH_REGRESSION_THRESHOLD_PCT:-20}"
Expand All @@ -24,6 +32,11 @@ if [ "$#" -lt 1 ] || [ "$#" -gt 2 ]; then
exit 2
fi

if ! [[ "$threshold" =~ ^[0-9]+([.][0-9]+)?$ ]]; then
echo "id-bench-compare.sh: error: BENCH_REGRESSION_THRESHOLD_PCT must be a non-negative number, got '$threshold'" >&2
exit 2
fi

current_file="$1"
baseline_file="${2:-pkgs/id/benches/baseline.txt}"

Expand Down Expand Up @@ -70,18 +83,29 @@ awk -v threshold="$threshold" '
print "| benchmark | unit | baseline median | current median | change | status |"
print "| --- | --- | ---: | ---: | ---: | --- |"
regressions = 0
incomparable = 0
for (i = 1; i <= base_n; i++) {
name = base_order[i]
if (name in cur_median) {
pct = (cur_median[name] - base_median[name]) / base_median[name] * 100
if (pct > threshold) {
status = "⚠️ regression"
regressions++
reg_list[++reg_n] = sprintf("%s (+%.1f%%)", name, pct)
if (base_unit[name] != cur_unit[name]) {
incomparable++
inc_list[++inc_n] = sprintf("%s (unit changed: %s → %s)", name, base_unit[name], cur_unit[name])
printf "| %s | %s → %s | %.3f | %.3f | — | incomparable (unit changed) |\n", name, base_unit[name], cur_unit[name], base_median[name], cur_median[name]
} else if (base_median[name] == 0) {
incomparable++
inc_list[++inc_n] = sprintf("%s (zero baseline median)", name)
printf "| %s | %s | %.3f | %.3f | — | incomparable (zero baseline) |\n", name, base_unit[name], base_median[name], cur_median[name]
} else {
status = "ok"
pct = (cur_median[name] - base_median[name]) / base_median[name] * 100
if (pct > threshold) {
status = "⚠️ regression"
regressions++
reg_list[++reg_n] = sprintf("%s (+%.1f%%)", name, pct)
} else {
status = "ok"
}
printf "| %s | %s | %.3f | %.3f | %+.1f%% | %s |\n", name, base_unit[name], base_median[name], cur_median[name], pct, status
}
printf "| %s | %s | %.3f | %.3f | %+.1f%% | %s |\n", name, base_unit[name], base_median[name], cur_median[name], pct, status
} else {
printf "| %s | %s | %.3f | — | — | not run |\n", name, base_unit[name], base_median[name]
}
Expand All @@ -100,5 +124,11 @@ awk -v threshold="$threshold" '
} else {
printf "✅ No benchmark regressed more than %s%% against the committed baselines.\n", threshold
}
if (incomparable > 0) {
printf "⚠️ **%d benchmark(s) could not be compared:** ", incomparable
for (i = 1; i <= inc_n; i++) printf "%s%s", inc_list[i], (i < inc_n ? ", " : "")
print ""
print "Incomparable rows carry no percent change and no verdict: a unit change means the two medians measure different things, and a zero baseline has no meaningful ratio. Re-baseline (see `pkgs/id/benches/BASELINES.md`) to make them comparable again."
}
}
' "$baseline_file" "$current_file"
110 changes: 110 additions & 0 deletions .github/scripts/id-bench-compare.test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
#!/usr/bin/env bash
# id-bench-compare.test.sh — tests for id-bench-compare.sh (#1619).
#
# Covers the three defects fixed for #1619 — unit-mismatched rows and
# zero-baseline rows are reported as incomparable (no percent, no
# verdict), and a non-numeric BENCH_REGRESSION_THRESHOLD_PCT is a
# config error (non-zero exit) — plus the unchanged behaviour:
# same-unit comparisons, not-run / no-baseline rows, and the
# missing-files exit-0 path.
#
# Runs anywhere bash+awk exist:
# .github/scripts/id-bench-compare.test.sh
set -u

script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
compare="$script_dir/id-bench-compare.sh"

tmp="$(mktemp -d)"
trap 'rm -rf "$tmp"' EXIT

failures=0

pass() { echo "ok - $1"; }
fail() { echo "FAIL - $1" >&2; failures=$((failures + 1)); }

assert_contains() { # <desc> <haystack> <needle>
case "$2" in
*"$3"*) pass "$1" ;;
*) fail "$1 (missing: $3)" ;;
esac
}

assert_not_contains() { # <desc> <haystack> <needle>
case "$2" in
*"$3"*) fail "$1 (unexpected: $3)" ;;
*) pass "$1" ;;
esac
}

assert_exit() { # <desc> <expected> <actual>
if [ "$2" -eq "$3" ]; then pass "$1"; else fail "$1 (exit $3, want $2)"; fi
}

# --- unit mismatch: no percent, no verdict, both units shown --------
printf 'bench alpha unit=us_per_event median=100.000 min=99.000 reps=5\n' > "$tmp/base-unit.txt"
printf 'bench alpha unit=ms_per_event median=0.200 min=0.190 reps=5\n' > "$tmp/cur-unit.txt"
out="$("$compare" "$tmp/cur-unit.txt" "$tmp/base-unit.txt")"
code=$?
assert_exit "unit mismatch exits 0" 0 "$code"
assert_contains "unit mismatch is incomparable" "$out" "incomparable (unit changed)"
assert_contains "unit mismatch shows baseline unit" "$out" "us_per_event"
assert_contains "unit mismatch shows current unit" "$out" "ms_per_event"
assert_not_contains "unit mismatch prints no percent" "$out" "-99.8%"
assert_contains "summary calls out incomparable rows" "$out" "could not be compared"
assert_contains "summary names the unit change" "$out" "unit changed: us_per_event"

# --- zero baseline: incomparable, never inf/nan ----------------------
printf 'bench beta unit=us_per_event median=0.000 min=0.000 reps=5\n' > "$tmp/base-zero.txt"
printf 'bench beta unit=us_per_event median=50.000 min=49.000 reps=5\n' > "$tmp/cur-zero.txt"
out="$("$compare" "$tmp/cur-zero.txt" "$tmp/base-zero.txt")"
code=$?
assert_exit "zero baseline exits 0" 0 "$code"
assert_contains "zero baseline is incomparable" "$out" "incomparable (zero baseline)"
assert_not_contains "zero baseline prints no inf" "$out" "inf"
assert_not_contains "zero baseline prints no nan" "$out" "nan"

# --- bad threshold: config error, non-zero exit, clear message -------
BENCH_REGRESSION_THRESHOLD_PCT=abc "$compare" "$tmp/cur-zero.txt" "$tmp/base-zero.txt" \
> "$tmp/bad-out.txt" 2> "$tmp/bad-err.txt"
code=$?
if [ "$code" -ne 0 ]; then pass "bad threshold exits non-zero"; else fail "bad threshold exits non-zero (exit 0)"; fi
err="$(cat "$tmp/bad-err.txt")"
assert_contains "bad threshold names the variable" "$err" "BENCH_REGRESSION_THRESHOLD_PCT"

# a decimal threshold is valid and still flags regressions
printf 'bench gamma unit=us_per_event median=100.000 min=99.000 reps=5\n' > "$tmp/base-ok.txt"
printf 'bench gamma unit=us_per_event median=106.000 min=105.000 reps=5\n' > "$tmp/cur-ok.txt"
out="$(BENCH_REGRESSION_THRESHOLD_PCT=5.5 "$compare" "$tmp/cur-ok.txt" "$tmp/base-ok.txt")"
code=$?
assert_exit "decimal threshold exits 0" 0 "$code"
assert_contains "decimal threshold flags regression" "$out" "⚠️ regression"

# --- same-unit behaviour unchanged ------------------------------------
out="$("$compare" "$tmp/cur-ok.txt" "$tmp/base-ok.txt")"
assert_contains "same-unit prints percent" "$out" "+6.0%"
assert_contains "same-unit under default threshold is ok" "$out" "| ok |"

printf 'bench gamma unit=us_per_event median=130.000 min=129.000 reps=5\n' > "$tmp/cur-reg.txt"
out="$("$compare" "$tmp/cur-reg.txt" "$tmp/base-ok.txt")"
assert_contains "same-unit regression percent" "$out" "+30.0%"
assert_contains "same-unit regression flagged" "$out" "⚠️ regression"

# --- not-run / no-baseline rows unchanged ------------------------------
printf 'bench delta unit=us_per_event median=10.000 min=9.000 reps=5\n' >> "$tmp/base-ok.txt"
printf 'bench epsilon unit=us_per_event median=5.000 min=4.000 reps=5\n' >> "$tmp/cur-ok.txt"
out="$("$compare" "$tmp/cur-ok.txt" "$tmp/base-ok.txt")"
assert_contains "missing current row is not run" "$out" "not run"
assert_contains "missing baseline row is no baseline" "$out" "no baseline"

# --- missing files still exit 0 ----------------------------------------
out="$("$compare" "$tmp/does-not-exist.txt" "$tmp/base-ok.txt")"
code=$?
assert_exit "missing current file exits 0" 0 "$code"
assert_contains "missing current file is reported" "$out" "not found"

if [ "$failures" -gt 0 ]; then
echo "$failures test(s) failed" >&2
exit 1
fi
echo "all id-bench-compare tests passed"
7 changes: 7 additions & 0 deletions .github/workflows/id-bench.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ on:
- "pkgs/id/**"
- ".github/workflows/id-bench.yml"
- ".github/scripts/id-bench-compare.sh"
- ".github/scripts/id-bench-compare.test.sh"
workflow_dispatch:

permissions:
Expand Down Expand Up @@ -62,6 +63,12 @@ jobs:
use-flakehub: false
use-gha-cache: true

# The compare script is the only regression signal for these
# benchmarks; its own tests (#1619) are plain bash+awk and run
# before the bench build so a broken script fails fast.
- name: Test the compare script
run: .github/scripts/id-bench-compare.test.sh

# Full-size workloads, exactly as the baselines were taken
# (BASELINES.md): --quick numbers are smoke sizes and are never
# comparable to the committed baselines.
Expand Down
7 changes: 6 additions & 1 deletion pkgs/id/benches/BASELINES.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,12 @@ journals; 32 fan-out clients x 100 rounds; 5 reps each) and prints one
per benchmark. To re-baseline: replace the four `bench` lines in
`baseline.txt`, update the machine/date header there and the runs
above, and say why in the commit message (hardware change, deliberate
performance change, or harness change). `--quick` shrinks every
performance change, or harness change). A harness change that
alters a benchmark's `unit=` is a re-baseline event too: until
`baseline.txt` is updated, `id-bench-compare.sh` reports that
benchmark as incomparable (unit changed) with no percent and no
verdict, rather than comparing medians across units (#1619).
`--quick` shrinks every
workload to a smoke size — never commit `--quick` numbers as
baselines.

Expand Down