|  | 
|  | 1 | +#!/bin/bash | 
|  | 2 | +set -e | 
|  | 3 | + | 
|  | 4 | +# Script to compare benchmark performance between branches using best practices | 
|  | 5 | +# Usage: ./benchmark-compare.sh [base-branch] [feature-branch] [count] | 
|  | 6 | +# | 
|  | 7 | +# Best practices implemented: | 
|  | 8 | +# - Uses -count=10 for statistical significance (can override with 3rd arg) | 
|  | 9 | +# - Clears build cache between runs | 
|  | 10 | +# - Stabilizes CPU frequency where possible | 
|  | 11 | +# - Uses -benchtime for longer runs to reduce noise | 
|  | 12 | +# | 
|  | 13 | +# If no arguments provided: | 
|  | 14 | +# - Runs benchmarks on current branch and saves to new.txt | 
|  | 15 | +# - Switches to main, runs benchmarks and saves to old.txt | 
|  | 16 | +# - Switches back and compares with benchstat | 
|  | 17 | +# | 
|  | 18 | +# If arguments provided: | 
|  | 19 | +# - Uses specified branches for comparison | 
|  | 20 | + | 
|  | 21 | +CURRENT_BRANCH=$(git branch --show-current) | 
|  | 22 | +BASE_BRANCH=${1:-main} | 
|  | 23 | +FEATURE_BRANCH=${2:-$CURRENT_BRANCH} | 
|  | 24 | +COUNT=${3:-10}  # Default to 10 runs for better statistical significance | 
|  | 25 | + | 
|  | 26 | +echo "======================================================================" | 
|  | 27 | +echo "Benchmark Comparison with Best Practices" | 
|  | 28 | +echo "======================================================================" | 
|  | 29 | +echo "  Base branch: $BASE_BRANCH" | 
|  | 30 | +echo "  Feature branch: $FEATURE_BRANCH" | 
|  | 31 | +echo "  Iterations: $COUNT (minimum 6 recommended for confidence intervals)" | 
|  | 32 | +echo "" | 
|  | 33 | + | 
|  | 34 | +# Check if benchstat is installed | 
|  | 35 | +if ! command -v benchstat &> /dev/null; then | 
|  | 36 | +    echo "benchstat is not installed. Installing..." | 
|  | 37 | +    go install golang.org/x/perf/cmd/benchstat@latest | 
|  | 38 | +    echo "" | 
|  | 39 | +fi | 
|  | 40 | + | 
|  | 41 | +# Warn about CPU frequency scaling | 
|  | 42 | +echo "NOTE: For most accurate results:" | 
|  | 43 | +echo "  - Close other applications" | 
|  | 44 | +echo "  - Disable CPU frequency scaling if possible" | 
|  | 45 | +echo "  - Run on AC power (laptops)" | 
|  | 46 | +echo "  - Consider: sudo cpupower frequency-set --governor performance (Linux)" | 
|  | 47 | +echo "" | 
|  | 48 | + | 
|  | 49 | +# Save current state | 
|  | 50 | +echo "Saving current work..." | 
|  | 51 | +git stash push -u -m "benchmark comparison stash" 2>/dev/null || true | 
|  | 52 | + | 
|  | 53 | +# Function to run benchmarks with best practices | 
|  | 54 | +run_benchmarks() { | 
|  | 55 | +    local branch=$1 | 
|  | 56 | +    local output=$2 | 
|  | 57 | +     | 
|  | 58 | +    echo "======================================================================" | 
|  | 59 | +    echo "Running benchmarks on $branch..." | 
|  | 60 | +    echo "======================================================================" | 
|  | 61 | +     | 
|  | 62 | +    # Clear build cache to ensure clean build | 
|  | 63 | +    echo "Clearing build cache..." | 
|  | 64 | +    go clean -cache -testcache | 
|  | 65 | +     | 
|  | 66 | +    # Run benchmarks with: | 
|  | 67 | +    # - count=$COUNT: Multiple runs for statistical significance | 
|  | 68 | +    # - benchmem: Include memory allocation stats | 
|  | 69 | +    # - benchtime=1s: Run each benchmark for at least 1 second (reduces timing noise) | 
|  | 70 | +    # - run=^$: Don't run any tests, only benchmarks | 
|  | 71 | +    echo "Running $COUNT iterations (this may take several minutes)..." | 
|  | 72 | +    go test -bench=. -benchmem -count=$COUNT -benchtime=1s -cpu=1 -run=^$ ./data 2>&1 | tee "$output" | 
|  | 73 | +     | 
|  | 74 | +    echo "" | 
|  | 75 | +    echo "Results saved to $output" | 
|  | 76 | +} | 
|  | 77 | + | 
|  | 78 | +# Run benchmarks on base branch | 
|  | 79 | +git checkout "$BASE_BRANCH" 2>&1 | grep -v "^M\s" || true | 
|  | 80 | +run_benchmarks "$BASE_BRANCH" "old.txt" | 
|  | 81 | + | 
|  | 82 | +# Run benchmarks on feature branch | 
|  | 83 | +git checkout "$FEATURE_BRANCH" 2>&1 | grep -v "^M\s" || true | 
|  | 84 | +run_benchmarks "$FEATURE_BRANCH" "new.txt" | 
|  | 85 | + | 
|  | 86 | +echo "" | 
|  | 87 | +echo "======================================================================" | 
|  | 88 | +echo "Benchmark Comparison Results" | 
|  | 89 | +echo "======================================================================" | 
|  | 90 | +benchstat -alpha=0.05 old.txt new.txt | 
0 commit comments