|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -euo pipefail |
| 3 | + |
| 4 | +TARGET_WORKSPACE=${HYPERFINE_BENCHMARK_WORKSPACE:?HYPERFINE_BENCHMARK_WORKSPACE is required} |
| 5 | +COMMENT=${HYPERFINE_RESULTS_FILE:?HYPERFINE_RESULTS_FILE is required} |
| 6 | +HEAD_BINARY=${HYPERFINE_HEAD_BINARY:?HYPERFINE_HEAD_BINARY is required} |
| 7 | +BASE_BINARY=${HYPERFINE_BASE_BINARY:?HYPERFINE_BASE_BINARY is required} |
| 8 | +REPO_WORKSPACE=$(pwd) |
| 9 | +OUT_DIR=$(dirname "$COMMENT") |
| 10 | +META_WORKSPACE="${TARGET_WORKSPACE}-meta" |
| 11 | + |
| 12 | +failed=false |
| 13 | + |
| 14 | +mkdir -p "$OUT_DIR" |
| 15 | +OUT_MD="$OUT_DIR/out.md" |
| 16 | +OUT_JSON="$OUT_DIR/out.json" |
| 17 | + |
| 18 | +CURRENT_PREK_VERSION=$( |
| 19 | + "$HEAD_BINARY" --version | sed -n '1p' |
| 20 | +) |
| 21 | + |
| 22 | +write_line() { |
| 23 | + printf '%s\n' "$1" >> "$COMMENT" |
| 24 | +} |
| 25 | + |
| 26 | +write_blank_line() { |
| 27 | + printf '\n' >> "$COMMENT" |
| 28 | +} |
| 29 | + |
| 30 | +write_section() { |
| 31 | + local title="$1" |
| 32 | + local description="${2:-}" |
| 33 | + |
| 34 | + write_blank_line |
| 35 | + write_line "## $title" |
| 36 | + if [ -n "$description" ]; then |
| 37 | + write_line "$description" |
| 38 | + fi |
| 39 | +} |
| 40 | + |
| 41 | +# Compare the two commands in out.json (reference vs current). |
| 42 | +# Hyperfine's JSON has results[0] = reference and results[1] = current. |
| 43 | +# A ratio > 1 means current is slower (regression), < 1 means faster (improvement). |
| 44 | +check_variance() { |
| 45 | + local cmd="$1" |
| 46 | + local num_results |
| 47 | + num_results=$(jq '.results | length' "$OUT_JSON") |
| 48 | + |
| 49 | + if [ "$num_results" -lt 2 ]; then |
| 50 | + return |
| 51 | + fi |
| 52 | + |
| 53 | + local ref_mean current_mean ratio pct |
| 54 | + ref_mean=$(jq '.results[0].mean' "$OUT_JSON") |
| 55 | + current_mean=$(jq '.results[1].mean' "$OUT_JSON") |
| 56 | + ratio=$(echo "scale=4; $current_mean / $ref_mean" | bc) |
| 57 | + pct=$(echo "scale=2; ($ratio - 1) * 100" | bc) |
| 58 | + |
| 59 | + if (( $(echo "${pct#-} > 10" | bc -l) )); then |
| 60 | + if (( $(echo "$ratio < 1" | bc -l) )); then |
| 61 | + write_line "✅ Performance improvement for \`$cmd\`: ${pct#-}% faster" |
| 62 | + else |
| 63 | + write_line "⚠️ Warning: Performance regression for \`$cmd\`: ${pct}% slower" |
| 64 | + failed=true |
| 65 | + fi |
| 66 | + fi |
| 67 | +} |
| 68 | + |
| 69 | +write_benchmark_details() { |
| 70 | + write_line "<details>" |
| 71 | + write_line "<summary>Benchmark details</summary>" |
| 72 | + write_blank_line |
| 73 | + cat "$OUT_MD" >> "$COMMENT" |
| 74 | + write_blank_line |
| 75 | + write_line "</details>" |
| 76 | +} |
| 77 | + |
| 78 | +benchmark() { |
| 79 | + local label="$1" |
| 80 | + local cmd="$2" |
| 81 | + local warmup="${3:-3}" |
| 82 | + local runs="${4:-30}" |
| 83 | + local setup="${5:-}" |
| 84 | + local prepare="${6:-}" |
| 85 | + local check_change="${7:-false}" |
| 86 | + local -a hyperfine_args=(-i -N -w "$warmup" -r "$runs" --export-markdown "$OUT_MD" --export-json "$OUT_JSON" --show-output) |
| 87 | + |
| 88 | + if [ -n "$setup" ]; then |
| 89 | + hyperfine_args+=(--setup "$setup") |
| 90 | + fi |
| 91 | + |
| 92 | + if [ -n "$prepare" ]; then |
| 93 | + hyperfine_args+=(--prepare "$prepare") |
| 94 | + fi |
| 95 | + |
| 96 | + write_line "### \`$label\`" |
| 97 | + if ! hyperfine "${hyperfine_args[@]}" --reference "$BASE_BINARY $cmd" "$HEAD_BINARY $cmd"; then |
| 98 | + write_line "⚠️ Benchmark failed for: $cmd" |
| 99 | + return 1 |
| 100 | + fi |
| 101 | + write_benchmark_details |
| 102 | + if [ "$check_change" = "true" ]; then |
| 103 | + check_variance "$cmd" |
| 104 | + fi |
| 105 | +} |
| 106 | + |
| 107 | +create_meta_workspace() { |
| 108 | + rm -rf "$META_WORKSPACE" |
| 109 | + mkdir -p "$META_WORKSPACE" |
| 110 | + cd "$META_WORKSPACE" |
| 111 | + git init || { echo "Failed to init git for meta hooks"; exit 1; } |
| 112 | + git config user.name "Benchmark" |
| 113 | + git config user.email "[email protected]" |
| 114 | + |
| 115 | + cp "$TARGET_WORKSPACE"/*.txt "$TARGET_WORKSPACE"/*.json . 2>/dev/null || true |
| 116 | + |
| 117 | + cat > .pre-commit-config.yaml << 'EOF' |
| 118 | +repos: |
| 119 | + - repo: meta |
| 120 | + hooks: |
| 121 | + - id: check-hooks-apply |
| 122 | + - id: check-useless-excludes |
| 123 | + - id: identity |
| 124 | + - repo: builtin |
| 125 | + hooks: |
| 126 | + - id: trailing-whitespace |
| 127 | + - id: end-of-file-fixer |
| 128 | +EOF |
| 129 | + |
| 130 | + git add -A |
| 131 | + git commit -m "Meta hooks test" || { echo "Failed to commit meta hooks test"; exit 1; } |
| 132 | + prek install-hooks |
| 133 | +} |
| 134 | + |
| 135 | +# Add environment metadata |
| 136 | +write_line "## Hyperfine Performance" |
| 137 | +write_blank_line |
| 138 | +write_line "**Environment:**" |
| 139 | +write_line "- OS: $(uname -s) $(uname -r)" |
| 140 | +write_line "- CPU: $(nproc) cores" |
| 141 | +write_line "- prek version: $CURRENT_PREK_VERSION" |
| 142 | +write_line "- Rust version: $(rustc --version)" |
| 143 | +write_line "- Hyperfine version: $(hyperfine --version)" |
| 144 | + |
| 145 | +# Benchmark in the main repo |
| 146 | +CMDS=( |
| 147 | + "--version" |
| 148 | + "list" |
| 149 | + "validate-config .pre-commit-config.yaml" |
| 150 | + "sample-config" |
| 151 | +) |
| 152 | +for cmd in "${CMDS[@]}"; do |
| 153 | + if [[ "$cmd" == "validate-config"* ]] && [ ! -f ".pre-commit-config.yaml" ]; then |
| 154 | + write_line "### \`prek $cmd\`" |
| 155 | + write_line "⏭️ Skipped: .pre-commit-config.yaml not found" |
| 156 | + continue |
| 157 | + fi |
| 158 | + |
| 159 | + if [[ "$cmd" == "--version" ]] || [[ "$cmd" == "list" ]]; then |
| 160 | + benchmark "prek $cmd" "$cmd" 5 100 |
| 161 | + else |
| 162 | + benchmark "prek $cmd" "$cmd" 3 50 |
| 163 | + fi |
| 164 | + check_variance "$cmd" |
| 165 | +done |
| 166 | + |
| 167 | +# Benchmark builtin hooks in test directory |
| 168 | +cd "$TARGET_WORKSPACE" |
| 169 | + |
| 170 | +# Cold vs warm benchmarks before polluting cache |
| 171 | +write_section "Cold vs Warm Runs" "Comparing first run (cold) vs subsequent runs (warm cache):" |
| 172 | +benchmark "prek run --all-files (cold - no cache)" "run --all-files" 0 10 "rm -rf ~/.cache/prek" "git checkout -- ." |
| 173 | +benchmark "prek run --all-files (warm - with cache)" "run --all-files" 3 20 "" "git checkout -- ." |
| 174 | + |
| 175 | +# Full benchmark suite with cache warmed up |
| 176 | +write_section "Full Hook Suite" "Running the builtin hook suite on the benchmark workspace:" |
| 177 | +benchmark "prek run --all-files (full builtin hook suite)" "run --all-files" 3 50 "" "git checkout -- ." true |
| 178 | + |
| 179 | +# Individual hook performance |
| 180 | +write_section "Individual Hook Performance" "Benchmarking each hook individually on the test repo:" |
| 181 | + |
| 182 | +INDIVIDUAL_HOOKS=( |
| 183 | + "trailing-whitespace" |
| 184 | + "end-of-file-fixer" |
| 185 | + "check-json" |
| 186 | + "check-yaml" |
| 187 | + "check-toml" |
| 188 | + "check-xml" |
| 189 | +) |
| 190 | + |
| 191 | +for hook in "${INDIVIDUAL_HOOKS[@]}"; do |
| 192 | + benchmark "prek run $hook --all-files" "run $hook --all-files" 3 30 "" "git checkout -- ." |
| 193 | +done |
| 194 | + |
| 195 | +# Installation performance |
| 196 | +write_section "Installation Performance" "Benchmarking hook installation (fast path hooks skip Python setup):" |
| 197 | +benchmark "prek install-hooks (cold - no cache)" "install-hooks" 1 5 "rm -rf ~/.cache/prek/hooks ~/.cache/prek/repos" |
| 198 | +benchmark "prek install-hooks (warm - with cache)" "install-hooks" 1 5 |
| 199 | + |
| 200 | +# File filtering/scoping performance |
| 201 | +write_section "File Filtering/Scoping Performance" "Testing different file selection modes:" |
| 202 | + |
| 203 | +git add -A |
| 204 | +benchmark "prek run (staged files only)" "run" 3 20 "" "sh -c 'git checkout -- . && git add -A'" |
| 205 | +benchmark "prek run --files '*.json' (specific file type)" "run --files '*.json'" 3 20 |
| 206 | + |
| 207 | +# Workspace discovery & initialization |
| 208 | +write_section "Workspace Discovery & Initialization" "Benchmarking hook discovery and initialization overhead:" |
| 209 | +benchmark "prek run --dry-run --all-files (measures init overhead)" "run --dry-run --all-files" 3 20 |
| 210 | + |
| 211 | +# Meta hooks performance |
| 212 | +write_section "Meta Hooks Performance" "Benchmarking meta hooks separately:" |
| 213 | +create_meta_workspace |
| 214 | + |
| 215 | +META_HOOKS=( |
| 216 | + "check-hooks-apply" |
| 217 | + "check-useless-excludes" |
| 218 | + "identity" |
| 219 | +) |
| 220 | + |
| 221 | +for hook in "${META_HOOKS[@]}"; do |
| 222 | + benchmark "prek run $hook --all-files" "run $hook --all-files" 3 15 "" "git checkout -- ." |
| 223 | +done |
| 224 | + |
| 225 | +if [ "$failed" = true ]; then |
| 226 | + exit 1 |
| 227 | +fi |
0 commit comments