Skip to content

Instantly share code, notes, and snippets.

@jonboulle
Forked from peterjmit/benchmark.sh
Last active February 9, 2018 16:49
Show Gist options
  • Save jonboulle/c51eadacab6be480dbf1b1b9587dd975 to your computer and use it in GitHub Desktop.
Save jonboulle/c51eadacab6be480dbf1b1b9587dd975 to your computer and use it in GitHub Desktop.
Bash Benchmark Script (using time)
#!/bin/bash
# Benchmark runner
repeats=20
output_file='benchmark_results.csv'
command_to_run='echo 1'
run_tests() {
# --------------------------------------------------------------------------
# Benchmark loop
# --------------------------------------------------------------------------
echo 'Benchmarking ' $command_to_run '...';
# Indicate the command we just run in the csv file
echo '======' $command_to_run '======' >> $output_file;
# Run the given command [repeats] times
for (( i = 1; i <= $repeats ; i++ ))
do
# percentage completion
p=$(( $i * 100 / $repeats))
# indicator of progress
l=$(seq -s "+" $i | sed 's/[0-9]//g')
# runs time function for the called script, output in a comma seperated
# format output file specified with -o command and -a specifies append
/usr/bin/time -f "%E,%U,%S" -o ${output_file} -a ${command_to_run} > /dev/null 2>&1
echo -ne ${l}' ('${p}'%) \r'
done;
echo -ne '\n'
# Convenience seperator for file
#echo '--------------------------' >> $output_file
}
# Option parsing
while getopts n:c:o: OPT
do
case "$OPT" in
n)
repeats=$OPTARG
;;
o)
output_file=$OPTARG
;;
c)
command_to_run=$OPTARG
run_tests
;;
\?)
echo 'No arguments supplied'
exit 1
;;
esac
done
shift `expr $OPTIND - 1`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment