|
| 1 | +#!/usr/bin/env bash |
| 2 | +# Benchmarks two commits and prints a before/after delta summary. |
| 3 | +# |
| 4 | +# Usage: |
| 5 | +# bash benchmarks/benchmark_compare.sh <before-sha> [after-sha] [ruby-binary ...] |
| 6 | +# |
| 7 | +# Defaults: after-sha = HEAD, ruby-binary = ruby |
| 8 | +# |
| 9 | +# Examples: |
| 10 | +# bash benchmarks/benchmark_compare.sh 6d857de |
| 11 | +# bash benchmarks/benchmark_compare.sh 6d857de HEAD |
| 12 | +# bash benchmarks/benchmark_compare.sh 6d857de HEAD ruby3.3 ruby3.4 |
| 13 | +# |
| 14 | +# With asdf: |
| 15 | +# bash benchmarks/benchmark_compare.sh 6d857de HEAD \ |
| 16 | +# $(asdf where ruby 3.3.11)/bin/ruby \ |
| 17 | +# $(asdf where ruby 3.4.9)/bin/ruby |
| 18 | +# |
| 19 | +# With rbenv: |
| 20 | +# bash benchmarks/benchmark_compare.sh 6d857de HEAD \ |
| 21 | +# $(rbenv prefix 3.3.11)/bin/ruby \ |
| 22 | +# $(rbenv prefix 3.4.9)/bin/ruby |
| 23 | +set -euo pipefail |
| 24 | + |
| 25 | +if [[ $# -lt 1 ]]; then |
| 26 | + echo "Usage: $0 <before-sha> [after-sha] [ruby-binary ...]" |
| 27 | + exit 1 |
| 28 | +fi |
| 29 | + |
| 30 | +DIR="$(cd "$(dirname "$0")" && pwd)" |
| 31 | +REPO="$(cd "$DIR/.." && pwd)" |
| 32 | + |
| 33 | +# Resolve to full SHAs immediately, before any git checkout changes HEAD |
| 34 | +BEFORE_LABEL=${1} |
| 35 | +AFTER_LABEL=${2:-HEAD} |
| 36 | +BEFORE=$(git -C "$REPO" rev-parse "$BEFORE_LABEL") |
| 37 | +AFTER=$(git -C "$REPO" rev-parse "$AFTER_LABEL") |
| 38 | +shift 2 2>/dev/null || shift $# |
| 39 | +RUBIES=("${@:-ruby}") |
| 40 | +RESULTS="$DIR/results" |
| 41 | +mkdir -p "$RESULTS" |
| 42 | + |
| 43 | +CURRENT_BRANCH=$(git -C "$REPO" rev-parse --abbrev-ref HEAD) |
| 44 | + |
| 45 | +restore() { |
| 46 | + git -C "$REPO" checkout --quiet "$CURRENT_BRANCH" |
| 47 | +} |
| 48 | +trap restore EXIT |
| 49 | + |
| 50 | +run_suite() { |
| 51 | + local label=$1 sha=$2 ruby_bin=$3 |
| 52 | + local ruby_label |
| 53 | + ruby_label=$("$ruby_bin" -e 'print "#{RUBY_VERSION}"') |
| 54 | + local tag="${label}_${ruby_label//./_}" |
| 55 | + local out="$RESULTS/${tag}.txt" |
| 56 | + |
| 57 | + echo |
| 58 | + echo "=== [$ruby_label] $label ($sha) ===" |
| 59 | + git -C "$REPO" checkout --quiet "$sha" |
| 60 | + "$ruby_bin" -e 'require "benchmark/ips"' 2>/dev/null || \ |
| 61 | + "$ruby_bin" -S gem install benchmark-ips --quiet --no-document |
| 62 | + # Prepend ruby's own directory to PATH so that bare `ruby` calls in older |
| 63 | + # versions of run_all.rb (before the RbConfig.ruby fix) also use the right binary |
| 64 | + PATH="$(dirname "$ruby_bin"):$PATH" "$ruby_bin" "$DIR/run_all.rb" 2>&1 | tee "$out" |
| 65 | + echo ">>> Saved: $out" |
| 66 | +} |
| 67 | + |
| 68 | +for ruby_bin in "${RUBIES[@]}"; do |
| 69 | + run_suite "before" "$BEFORE" "$ruby_bin" |
| 70 | + run_suite "after" "$AFTER" "$ruby_bin" |
| 71 | +done |
| 72 | + |
| 73 | +# Restore before printing the summary so compare_results.rb is available |
| 74 | +restore |
| 75 | + |
| 76 | +echo |
| 77 | +echo "==================================================================" |
| 78 | +echo "Summary: before ($BEFORE_LABEL) vs after ($AFTER_LABEL)" |
| 79 | +echo "==================================================================" |
| 80 | + |
| 81 | +for ruby_bin in "${RUBIES[@]}"; do |
| 82 | + ruby_label=$("$ruby_bin" -e 'print "#{RUBY_VERSION}"') |
| 83 | + before_file="$RESULTS/before_${ruby_label//./_}.txt" |
| 84 | + after_file="$RESULTS/after_${ruby_label//./_}.txt" |
| 85 | + [[ -f "$before_file" && -f "$after_file" ]] || continue |
| 86 | + |
| 87 | + echo |
| 88 | + echo " Ruby $ruby_label ($BEFORE_LABEL vs $AFTER_LABEL)" |
| 89 | + "$ruby_bin" "$DIR/compare_results.rb" "$before_file" "$after_file" |
| 90 | +done |
0 commit comments