|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# Run rbac authz benchmark tests on the current Git branch or compare benchmark results |
| 4 | +# between two branches using `benchstat`. |
| 5 | +# |
| 6 | +# The script supports: |
| 7 | +# 1) Running benchmarks and saving output to a file. |
| 8 | +# 2) Checking out two branches, running benchmarks on each, and saving the benchstat |
| 9 | +# comparison results to a file. |
| 10 | +# Benchmark results are saved with filenames based on the branch name. |
| 11 | +# |
| 12 | +# Usage: |
| 13 | +# benchmark_authz.sh --single # Run benchmarks on current branch |
| 14 | +# benchmark_authz.sh --compare <branchA> <branchB> # Compare benchmarks between two branches |
| 15 | + |
| 16 | +set -euo pipefail |
| 17 | + |
| 18 | +# Go benchmark parameters |
| 19 | +GOMAXPROCS=16 |
| 20 | +TIMEOUT=30m |
| 21 | +BENCHTIME=1s# TODO: add 5s |
| 22 | +COUNT=1# TODO: add 5 |
| 23 | + |
| 24 | +# Script configuration |
| 25 | +OUTPUT_DIR="benchmark_outputs" |
| 26 | + |
| 27 | +# List of benchmark tests |
| 28 | +BENCHMARKS=( |
| 29 | + BenchmarkRBACAuthorize |
| 30 | + BenchmarkRBACAuthorizeGroups |
| 31 | + BenchmarkRBACFilter |
| 32 | +) |
| 33 | + |
| 34 | +# Create output directory |
| 35 | +mkdir -p"$OUTPUT_DIR" |
| 36 | + |
| 37 | +functionrun_benchmarks() { |
| 38 | +local branch=$1 |
| 39 | +# Replace '/' with '-' for branch names with format user/branchName |
| 40 | +local filename_branch=${branch//\//-} |
| 41 | +local output_file_prefix="$OUTPUT_DIR/${filename_branch}" |
| 42 | + |
| 43 | +echo"Checking out$branch..." |
| 44 | + git checkout"$branch" |
| 45 | + |
| 46 | +forbenchin"${BENCHMARKS[@]}";do |
| 47 | +local output_file="${output_file_prefix}_${bench}.txt" |
| 48 | +echo"Running benchmark$bench on$branch..." |
| 49 | +GOMAXPROCS=$GOMAXPROCS gotest -timeout$TIMEOUT -bench="^${bench}$" -run=^$ -benchtime=$BENCHTIME -count=$COUNT ./..| tee"$output_file" |
| 50 | +done |
| 51 | +} |
| 52 | + |
| 53 | +if [["${1:-}"=="--single" ]];then |
| 54 | + current_branch=$(git rev-parse --abbrev-ref HEAD) |
| 55 | + run_benchmarks"$current_branch" |
| 56 | +elif [["${1:-}"=="--compare" ]];then |
| 57 | + base_branch=$2 |
| 58 | + test_branch=$3 |
| 59 | + |
| 60 | +# Run all benchmarks on both branches |
| 61 | + run_benchmarks"$base_branch" |
| 62 | + run_benchmarks"$test_branch" |
| 63 | + |
| 64 | +# Compare results benchmark by benchmark |
| 65 | +forbenchin"${BENCHMARKS[@]}";do |
| 66 | +# Replace / with - for branch names with format user/branchName |
| 67 | + filename_base_branch=${base_branch//\//-} |
| 68 | + filename_test_branch=${test_branch//\//-} |
| 69 | + |
| 70 | +echo -e"\nGenerating benchmark diff for$bench using benchstat..." |
| 71 | +benchstat"$OUTPUT_DIR/${filename_base_branch}_${bench}.txt""$OUTPUT_DIR/${filename_test_branch}_${bench}.txt"| tee"$OUTPUT_DIR/${bench}_diff.txt" |
| 72 | +done |
| 73 | +else |
| 74 | +echo"Usage:" |
| 75 | +echo"$0 --single # run benchmarks on current branch" |
| 76 | +echo"$0 --compare branchA branchB # compare benchmarks between two branches" |
| 77 | +exit 1 |
| 78 | +fi |