Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(benchmarks): Modernize benchmark script with CLI options and error handling #719

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
206 changes: 111 additions & 95 deletions scripts/run_benchmarks.sh
Original file line number Diff line number Diff line change
@@ -1,107 +1,123 @@
#!/usr/bin/env bash

# Created by argbash-init v2.10.0
# ARG_OPTIONAL_BOOLEAN([asm])
# ARG_OPTIONAL_BOOLEAN([multi_threads])
# ARG_HELP([<Jellyfish benchmarks>])
# ARGBASH_GO()
# needed because of Argbash --> m4_ignore([
### START OF CODE GENERATED BY Argbash v2.10.0 one line above ###
# Argbash is a bash code generator used to get arguments parsing right.
# Argbash is FREE SOFTWARE, see https://argbash.dev, https://github.com/matejak/argbash for more info


die()
{
local _ret="${2:-1}"
test "${_PRINT_HELP:-no}" = yes && print_help >&2
echo "$1" >&2
exit "${_ret}"
# Function to display help
print_help() {
echo "Jellyfish Benchmarks Runner"
echo
echo "Usage: $0 [options]"
echo
echo "Options:"
echo " --benchmark NAME Run a specific benchmark (e.g., hash, merkle, plonk)"
echo " --threads N Set the number of threads (default: all available)"
echo " --output FILE Save results to a file (supported formats: csv, json)"
echo " --asm Enable ASM optimizations"
echo " --iterations N Set the number of iterations (default: 100)"
echo " --warmup N Set the number of warm-up iterations (default: 10)"
echo " -h, --help Show this help message"
}


begins_with_short_option()
{
local first_option all_short_options='h'
first_option="${1:0:1}"
test "$all_short_options" = "${all_short_options/$first_option/}" && return 1 || return 0
# Error handling
handle_error() {
echo "Error: $1" >&2
exit 1
}

# THE DEFAULTS INITIALIZATION - OPTIONALS
_arg_asm="off"
_arg_multi_threads="off"


print_help()
{
printf '%s\n' "<Jellyfish benchmarks>"
printf 'Usage: %s [--(no-)asm] [--(no-)multi_threads] [-h|--help]\n' "$0"
printf '\t%s\n' "-h, --help: Prints help"
}


parse_commandline()
{
while test $# -gt 0
do
_key="$1"
case "$_key" in
--no-asm|--asm)
_arg_asm="on"
test "${1:0:5}" = "--no-" && _arg_asm="off"
;;
--no-multi_threads|--multi_threads)
_arg_multi_threads="on"
test "${1:0:5}" = "--no-" && _arg_multi_threads="off"
;;
-h|--help)
print_help
exit 0
;;
-h*)
print_help
exit 0
;;
*)
_PRINT_HELP=yes die "FATAL ERROR: Got an unexpected argument '$1'" 1
;;
esac
shift
done
}

parse_commandline "$@"

# OTHER STUFF GENERATED BY Argbash

### END OF CODE GENERATED BY Argbash (sortof) ### ])
# [ <-- needed because of Argbash

cargo clean

if [ "$_arg_multi_threads" = on ]
then
echo "Multi-threads: ON"
# Do nothing
else
echo "Multi-threads: OFF"
export RAYON_NUM_THREADS=1
# Default values
BENCHMARK=""
THREADS=""
OUTPUT_FILE=""
ASM_ENABLED=false
ITERATIONS=100
WARMUP=10

# Argument parsing
while [[ $# -gt 0 ]]; do
case $1 in
--benchmark)
BENCHMARK="$2"
shift 2
;;
--threads)
THREADS="$2"
shift 2
;;
--output)
OUTPUT_FILE="$2"
shift 2
;;
--asm)
ASM_ENABLED=true
shift
;;
--iterations)
ITERATIONS="$2"
shift 2
;;
--warmup)
WARMUP="$2"
shift 2
;;
-h|--help)
print_help
exit 0
;;
*)
handle_error "Unknown parameter: $1"
;;
esac
done

# Environment setup
if [ "$ASM_ENABLED" = true ]; then
export RUSTFLAGS="-C target-feature=+bmi2,+adx"
fi

if [ "$_arg_asm" = on ]
then
echo "Asm feature: ON"
export RUSTFLAGS="-C target-feature=+bmi2,+adx"
else
echo "Asm feature: OFF"
# Do nothing
if [ -n "$THREADS" ]; then
export RAYON_NUM_THREADS="$THREADS"
fi

# Run the benchmark binary
set -e
cargo bench

# Clean previous build
cargo clean || handle_error "Failed to clean previous build"

# Function to run benchmarks
run_benchmark() {
local cmd="cargo bench"

if [ -n "$BENCHMARK" ]; then
cmd="$cmd --bench $BENCHMARK"
fi

cmd="$cmd -- --warm-up-time $WARMUP --measurement-time $ITERATIONS"

if [ -n "$OUTPUT_FILE" ]; then
case "${OUTPUT_FILE##*.}" in
csv)
$cmd --format csv > "$OUTPUT_FILE"
;;
json)
$cmd --format json > "$OUTPUT_FILE"
;;
*)
handle_error "Unsupported file format. Use .csv or .json"
;;
esac
else
$cmd
fi
}

# ^^^ TERMINATE YOUR CODE BEFORE THE BOTTOM ARGBASH MARKER ^^^
# Run benchmarks with error handling
echo "Running benchmarks..."
echo "Configuration:"
echo "- Benchmark: ${BENCHMARK:-all}"
echo "- Threads: ${THREADS:-auto}"
echo "- ASM: $ASM_ENABLED"
echo "- Iterations: $ITERATIONS"
echo "- Warm-up: $WARMUP"
[ -n "$OUTPUT_FILE" ] && echo "- Output to: $OUTPUT_FILE"

if ! run_benchmark; then
handle_error "Failed to execute benchmarks"
fi

# ] <-- needed because of Argbash
echo "Benchmarks completed successfully"
Loading