Skip to content

Instantly share code, notes, and snippets.

@juaniyyoo
Last active October 26, 2016 12:20
Show Gist options
  • Save juaniyyoo/12d9c895f34b5f5f52efa5e2e7284a2e to your computer and use it in GitHub Desktop.
Save juaniyyoo/12d9c895f34b5f5f52efa5e2e7284a2e to your computer and use it in GitHub Desktop.
A simple benchmark runner with a date parser
#!/bin/bash
if [ -z "$1" ] || [ -z "$2" ] || [ -z "$3" ] ; then
echo 'Usage : $0 [PRE-COMMAND] [COMMAND] [RESULT FILE]'
exit 1
fi
if [ ! -f $3 ] ; then
echo "Creating result file : $3"
touch $3
fi
PRECOMMAND=$1
COMMAND=$2
RES_FILE=$3
for i in {0..20}
do
echo "Preparing benchmark ..."
$PRECOMMAND
echo "Running benchmark ($i) ..."
START=$(date +%s%N);
$COMMAND
END=$(date +%s%N);
echo "Registering benchmark ..."
echo $((END-START)) | awk -f date-parser.awk >> $RES_FILE
done
# Usage eg. :
# START=$(date +%s%N);
# END=$(date +%s%N);
# echo $((END-START)) | awk -f date-parser.awk
{
printf("Raw : %d nanoseconds\n", ($1 / 1000))
nanoseconds=$1 % 1000
microseconds=($1 / 1000) % 1000
milliseconds=($1 / (1000 * 1000)) % 1000
seconds=($1 / (1000 * 1000 * 1000)) % 60
minutes=($1 / (60 * 1000 * 1000 * 1000)) % 60
hours=($1 / (60 * 60 * 1000 * 1000 * 1000)) % 24
days=($1 / (24 * 60 * 60 * 1000 * 1000 * 1000)) % 7
weeks=($1 / (7 * 24 * 60 * 60 * 1000 * 1000 * 1000))
printf("Parsed : %d weeks %01d days %02d:%02d:%02d %03d.%03d.%03d\n", weeks, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment