Skip to main content

go/gobench

1#!/usr/bin/env bash
3# gobench helps me compare benchmarks between a local branch and main,
4# so I can see the difference (and hopefully the improvement!).
5#
6# It runs samples in the local branch and main, then compares the
7# results using benchstat.
8#
9# == Usage ==
11# Call `gobench` with the arguments you'd pass to `go test` to run
12# the benchmark. For example:
14# $ gobench ./control -bench=BenchmarkServerEncode
16# By default, it runs 6 samples. If you want to run more samples, set
17# the SAMPLE_COUNT environment variable. For example:
19# $ SAMPLE_COUNT=12 gobench ./control -bench=BenchmarkServerEncode
21# == Installation ==
23# You need to install benchstat to use this script.
25# $ go install golang.org/x/perf/cmd/benchstat@latest
28set -o errexit
29set -o nounset
31# 1. Get the current branch and commits to test
32CURRENT_BRANCH=$(git symbolic-ref --short -q HEAD || git rev-parse --short HEAD)
33MAIN_COMMIT=$(git rev-parse --short main)
34HEAD_COMMIT=$(git rev-parse --short HEAD)
36if [[ "$CURRENT_BRANCH" = "main" ]]; then
37 echo "gobench can only be run from non-main branches" >&2
38 exit 1
39fi
41if [[ "$MAIN_COMMIT" = "$HEAD_COMMIT" ]]; then
42 echo "Your branch is the same as main; gobench can only be run from distinct branches" >&2
43 exit 1
44fi
46# 2. Create a temporary log for output. Clean it up on exit or error,
47# and restore the starting branch/commit.
48TMP_LOG=$(mktemp)
49trap 'git checkout -q "$CURRENT_BRANCH"; rm -f "$TMP_LOG"' EXIT INT TERM
51# 3. Create a temporary directory to store output
52TMP_DIR=$(mktemp -d)
53echo "Output directory: $TMP_DIR"
55# 4. Determine the Go binary to use.
57# Prefer `./tool/go` if present (which is common in Tailscale repos);
58# fallback to `go` if not.
59GO_BIN="go"
60if [[ -x "./tool/go" ]]; then
61 GO_BIN="./tool/go"
62fi
64# 5. Run alternating benchmarks
65SAMPLE_COUNT="${SAMPLE_COUNT:-6}"
67MAIN_OUTPUT="$TMP_DIR/main (${MAIN_COMMIT:0:7})"
68HEAD_OUTPUT="$TMP_DIR/$CURRENT_BRANCH (${HEAD_COMMIT:0:7})"
70mkdir -p "$(dirname "$MAIN_OUTPUT")"
71mkdir -p "$(dirname "$HEAD_OUTPUT")"
73for ((N=1; N<=SAMPLE_COUNT; N++)); do
74 # Benchmark main
75 echo "[$N/$SAMPLE_COUNT] Running on main ($MAIN_COMMIT)"
76 git checkout -q "$MAIN_COMMIT"
77 if ! "$GO_BIN" test -run=^$ "$@" > "$TMP_LOG" 2>&1; then
78 echo "ERROR: Benchmark failed:" >&2
79 cat "$TMP_LOG" >&2
80 exit 1
81 fi
82 cat "$TMP_LOG" >> "$MAIN_OUTPUT"
84 # Benchmark head
85 echo "[$N/$SAMPLE_COUNT] Running on $CURRENT_BRANCH ($HEAD_COMMIT)"
86 git checkout -q "$HEAD_COMMIT"
87 if ! "$GO_BIN" test -run=^$ "$@" > "$TMP_LOG" 2>&1; then
88 echo "ERROR: Benchmark failed:" >&2
89 cat "$TMP_LOG" >&2
90 exit 1
91 fi
92 cat "$TMP_LOG" >> "$HEAD_OUTPUT"
93done
95rm -f "$TMP_LOG"
97# 6. Compare results
98if ! command -v benchstat &>/dev/null; then
99 echo "ERROR: benchstat is required. Install via: go install golang.org/x/perf/cmd/benchstat@latest" >&2
100 exit 1
101fi
103echo ""
104echo "== Benchmark results =="
105pushd "$TMP_DIR" >/dev/null
106 benchstat "${MAIN_OUTPUT#$TMP_DIR/}" "${HEAD_OUTPUT#$TMP_DIR/}"
107popd >/dev/null