#!/usr/bin/env bash # gobench helps me compare benchmarks between a local branch and main, # so I can see the difference (and hopefully the improvement!). # # It runs samples in the local branch and main, then compares the # results using benchstat. # # == Usage == # # Call `gobench` with the arguments you'd pass to `go test` to run # the benchmark. For example: # # $ gobench ./control -bench=BenchmarkServerEncode # # By default, it runs 6 samples. If you want to run more samples, set # the SAMPLE_COUNT environment variable. For example: # # $ SAMPLE_COUNT=12 gobench ./control -bench=BenchmarkServerEncode # # == Installation == # # You need to install benchstat to use this script. # # $ go install golang.org/x/perf/cmd/benchstat@latest # set -o errexit set -o nounset # 1. Get the current branch and commits to test CURRENT_BRANCH=$(git symbolic-ref --short -q HEAD || git rev-parse --short HEAD) MAIN_COMMIT=$(git rev-parse --short main) HEAD_COMMIT=$(git rev-parse --short HEAD) if [[ "$CURRENT_BRANCH" = "main" ]]; then echo "gobench can only be run from non-main branches" >&2 exit 1 fi if [[ "$MAIN_COMMIT" = "$HEAD_COMMIT" ]]; then echo "Your branch is the same as main; gobench can only be run from distinct branches" >&2 exit 1 fi # 2. Create a temporary log for output. Clean it up on exit or error, # and restore the starting branch/commit. TMP_LOG=$(mktemp) trap 'git checkout -q "$CURRENT_BRANCH"; rm -f "$TMP_LOG"' EXIT INT TERM # 3. Create a temporary directory to store output TMP_DIR=$(mktemp -d) echo "Output directory: $TMP_DIR" # 4. Determine the Go binary to use. # # Prefer `./tool/go` if present (which is common in Tailscale repos); # fallback to `go` if not. GO_BIN="go" if [[ -x "./tool/go" ]]; then GO_BIN="./tool/go" fi # 5. Run alternating benchmarks SAMPLE_COUNT="${SAMPLE_COUNT:-6}" MAIN_OUTPUT="$TMP_DIR/main (${MAIN_COMMIT:0:7})" HEAD_OUTPUT="$TMP_DIR/$CURRENT_BRANCH (${HEAD_COMMIT:0:7})" mkdir -p "$(dirname "$MAIN_OUTPUT")" mkdir -p "$(dirname "$HEAD_OUTPUT")" for ((N=1; N<=SAMPLE_COUNT; N++)); do # Benchmark main echo "[$N/$SAMPLE_COUNT] Running on main ($MAIN_COMMIT)" git checkout -q "$MAIN_COMMIT" if ! "$GO_BIN" test -run=^$ "$@" > "$TMP_LOG" 2>&1; then echo "ERROR: Benchmark failed:" >&2 cat "$TMP_LOG" >&2 exit 1 fi cat "$TMP_LOG" >> "$MAIN_OUTPUT" # Benchmark head echo "[$N/$SAMPLE_COUNT] Running on $CURRENT_BRANCH ($HEAD_COMMIT)" git checkout -q "$HEAD_COMMIT" if ! "$GO_BIN" test -run=^$ "$@" > "$TMP_LOG" 2>&1; then echo "ERROR: Benchmark failed:" >&2 cat "$TMP_LOG" >&2 exit 1 fi cat "$TMP_LOG" >> "$HEAD_OUTPUT" done rm -f "$TMP_LOG" # 6. Compare results if ! command -v benchstat &>/dev/null; then echo "ERROR: benchstat is required. Install via: go install golang.org/x/perf/cmd/benchstat@latest" >&2 exit 1 fi echo "" echo "== Benchmark results ==" pushd "$TMP_DIR" >/dev/null benchstat "${MAIN_OUTPUT#$TMP_DIR/}" "${HEAD_OUTPUT#$TMP_DIR/}" popd >/dev/null