Skip to content

Instantly share code, notes, and snippets.

@KHCode
Last active February 27, 2026 16:47
Show Gist options
  • Select an option

  • Save KHCode/01b9e9dbcf43d937d93913c0ed9f6c85 to your computer and use it in GitHub Desktop.

Select an option

Save KHCode/01b9e9dbcf43d937d93913c0ed9f6c85 to your computer and use it in GitHub Desktop.
Rspec file benchmark zsh script
#!/usr/bin/env zsh
# rspec_benchmark.sh
set -e
if [[ $# -ne 2 ]]; then
echo "Usage: $0 <spec_file> <number_of_runs>"
echo "Example: $0 spec/features/some_feature/feature_spec.rb 5"
exit 1
fi
file="$1"
runs="$2"
if [[ ! -f "$file" ]]; then
echo "File not found: $file"
exit 1
fi
if [[ "$runs" -lt 1 ]]; then
echo "Number of runs must be at least 1."
exit 1
fi
typeset -a times
for i in $(seq 1 $runs); do
echo ""
echo "$(printf '=%.0s' {1..60})"
echo "Run $i of $runs"
echo "$(printf '=%.0s' {1..60})"
output=$(bundle exec rspec "$file" 2>&1)
echo "$output"
duration=$(echo "$output" | grep -oE 'Finished in [0-9]+\.?[0-9]*' | grep -oE '[0-9]+\.?[0-9]*')
if [[ -n "$duration" ]]; then
times+=("$duration")
echo ">> Run $i completed in ${duration}s"
else
echo ">> Run $i: Could not parse timing from output."
fi
done
count=${#times[@]}
if [[ "$count" -eq 0 ]]; then
echo "\nNo timing data collected."
exit 1
fi
sorted=($(echo "${times[@]}" | tr ' ' '\n' | sort -n))
stats=$(echo "${sorted[@]}" | tr ' ' '\n' | awk '
BEGIN { count = 0 }
{
vals[NR] = $1
sum += $1
count++
rounded = sprintf("%.1f", $1)
freq[rounded]++
}
END {
mean = sum / count
if (count % 2 == 1) {
median = vals[int(count / 2) + 1]
} else {
median = (vals[count / 2] + vals[count / 2 + 1]) / 2.0
}
range_val = vals[count] - vals[1]
min_val = vals[1]
max_val = vals[count]
max_freq = 0
for (k in freq) {
if (freq[k] > max_freq) {
max_freq = freq[k]
}
}
mode_str = ""
for (k in freq) {
if (freq[k] == max_freq) {
if (mode_str != "") mode_str = mode_str ", "
mode_str = mode_str k "s"
}
}
printf "MEAN=%.4f\n", mean
printf "MEDIAN=%.4f\n", median
printf "RANGE=%.4f\n", range_val
printf "MIN=%.4f\n", min_val
printf "MAX=%.4f\n", max_val
printf "MODE=%s\n", mode_str
printf "MODE_FREQ=%d\n", max_freq
}')
mean=$(echo "$stats" | grep '^MEAN=' | cut -d= -f2)
median=$(echo "$stats" | grep '^MEDIAN=' | cut -d= -f2)
range_val=$(echo "$stats" | grep '^RANGE=' | cut -d= -f2)
min_val=$(echo "$stats" | grep '^MIN=' | cut -d= -f2)
max_val=$(echo "$stats" | grep '^MAX=' | cut -d= -f2)
mode=$(echo "$stats" | grep '^MODE=' | cut -d= -f2)
mode_freq=$(echo "$stats" | grep '^MODE_FREQ=' | cut -d= -f2)
echo ""
echo "$(printf '=%.0s' {1..60})"
echo "RESULTS"
echo "$(printf '=%.0s' {1..60})"
for i in $(seq 1 $count); do
echo " Run $i: ${sorted[$i]}s"
done
echo "$(printf -- '-%.0s' {1..40})"
echo " Mean: ${mean}s"
echo " Median: ${median}s"
echo " Mode: ${mode} (rounded to 0.1s, appeared ${mode_freq}x)"
echo " Range: ${range_val}s (${min_val}s – ${max_val}s)"
echo "$(printf '=%.0s' {1..60})"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment