#!/bin/bash
# -----------------------------------------------------------------
# DriveBnch - Runs benchmark testing on a RAID array of drives

#Programmer: Nicholas Barrett

#Copyright 2017... haha fuck I'm not copywriting this shit it's probably going to be garbage

#Date: 12/21/2017 @ 10:59 AM


# ------------------------------------------------------------------------

pause(){ # Waits for user input to continue
echo 
echo "Press Enter to run these tests." 
read -p "Warning, these benchmarking tests will run in this directory. Verify you are in the correct directory before proceeding $1" con
case $con in
*)
	;;
esac
}

main_menu () { # Indicates what tests have been instructed to be run
echo
echo "---------------------------------------------------------------------------------------"
echo 
echo Welcome to DriveBnch: A drive benchmarking tool
echo Re-run DriveBnch with '-h' for help
echo
echo "----------------------------------------------------------------------------------------"
echo
echo Tests set to run:
echo

if [ "$SIXTYFOURk_FLAG" == "yes" ];then
	echo "-a (64k Bench Test)"
	echo "iozone -t 8 -i 0 -r 64k -s $SIZE -+n -w -O"
	echo "iozone -t 8 -i 1 -r 64k -s $SIZE -+n -w -O"
	echo 
fi	
if [ "$ONEm_FLAG" == "yes" ];then
	echo "-b (1M Bench Test)"
	echo "iozone -t 8 -i 0 -r 1M -s $SIZE -+n -w"
	echo "iozone -t 8 -i 1 -r 1M -s $SIZE -+n -w"
	echo 	
fi	
if [ "$FOURk_FLAG" == "yes" ];then
	echo "-c (4k Bench Test)"
	echo "iozone -t 8 -i 0 -r 4k -s $SIZE -+n -w -O"
	echo "iozone -t 8 -i 1 -r 4k -s $SIZE -+n -w -O"
	echo
fi	
if [ "$FOURkRAND_FLAG" == "yes" ];then
	echo "-d (4k Random Bench Test)"
	echo "iozone -t 8 -i 2 -r 4k -s $SIZE -+n -w -O"
	echo
fi	
if [ "$CUST_FLAG" == "yes" ];then
	echo "-z (Custom Bench Tests)"
	while [ "$COUNTER2" -lt $(( NUMofCUST+1 )) ];do
		echo "iozone -t ${tVal[$COUNTER2]} -i ${WRorR[$COUNTER2]} -r ${rVal[$COUNTER2]} -s ${custSIZE[$COUNTER2]} -+n -w ${oType[$COUNTER2]}"
		let COUNTER2=COUNTER2+1
	done
	echo
fi

pause
}

usage() { # Help
        cat << EOF
Usage:	DriveBnch
	[-a] Runs 64k bench test with default parameters
	[-b] Runs 1M bench test with default parameters
	[-c] Runs 4k bench test with default parameters
	[-d] Runs 4k random bench test with default parameters
	[-z] lets user customize parameters
	[-s] lets user modify the file size for the default tests
	[-h] Displays this message
EOF
        exit 0
}

SIXTYFOURk_FLAG=no
ONEm_FLAG=no
FOURk_FLAG=no
FOURkRAND_FLAG=no
CUST_FLAG=no
NUMofCUST=0
SIZE=4G # default size for the default parameter tests 
COUNTER=1
COUNTER2=1
COUNTER3=1

while getopts 'abcdz:s:h' OPTION; do
	case ${OPTION} in
	a)
		SIXTYFOURk_FLAG=yes
		;;
	b)
		ONEm_FLAG=yes
		;;
	c)
		FOURk_FLAG=yes
		;;
	d)
		FOURkRAND_FLAG=yes
		;;
	
	z)
		NUMofCUST=${OPTARG}
		CUST_FLAG=yes
		;;
		
	s)
		SIZE=${OPTARG}
		;;
		
	h)
		usage
		;;
	
	esac
done

if [ $# -eq 0 ];then
	usage
	exit 1
fi

if [ "$CUST_FLAG" == "yes" ];then
	echo "You have indicated you wish to run $NUMofCUST custom benchmark(s)"
	while [ "$COUNTER" -lt $(( NUMofCUST+1 )) ];do
		echo 
		read -p "Enter the number of threads (Custom benchmark $COUNTER): " tVal[$COUNTER]
		read -p "Enter '0' for a write, a '1' for a read, or a '2' for a rand (Custom benchmark $COUNTER): " WRorR[$COUNTER]
		read -p "Enter the record size (Custom benchmark $COUNTER): " rVal[$COUNTER]
		read -p "Enter the size of the file (Custom benchmark $COUNTER): " custSIZE[$COUNTER]
		read -p "Enter '-O' for results in ops/sec or simply press enter for kb/sec: " oType[$COUNTER]
		let COUNTER=COUNTER+1
	done
fi

#the main menu is called after the user enters the details for the custom bench tests
main_menu

if [ "$SIXTYFOURk_FLAG" == "yes" ];then
	echo 3 > /proc/sys/vm/drop_caches
	iozone -t 8 -i 0 -r 64k -s $SIZE -+n -w -O
	echo 3 > /proc/sys/vm/drop_caches
	iozone -t 8 -i 1 -r 64k -s $SIZE -+n -w -O
	echo 3 > /proc/sys/vm/drop_caches
fi	
if [ "$ONEm_FLAG" == "yes" ];then
	echo 3 > /proc/sys/vm/drop_caches 
	iozone -t 8 -i 0 -r 1M -s $SIZE -+n -w
	echo 3 > /proc/sys/vm/drop_caches
	iozone -t 8 -i 1 -r 1M -s $SIZE -+n -w
	echo 3 > /proc/sys/vm/drop_caches	
fi	
if [ "$FOURk_FLAG" == "yes" ];then
	echo 3 > /proc/sys/vm/drop_caches
	iozone -t 8 -i 0 -r 4k -s $SIZE -+n -w -O
	echo 3 > /proc/sys/vm/drop_caches
	iozone -t 8 -i 1 -r 4k -s $SIZE -+n -w -O
	echo 3 > /proc/sys/vm/drop_caches
fi	
if [ "$FOURkRAND_FLAG" == "yes" ];then
	echo 3 > /proc/sys/vm/drop_caches
	iozone -t 8 -i 2 -r 4k -s $SIZE -+n -w -O
fi	
if [ "$CUST_FLAG" == "yes" ];then
	while [ "$COUNTER3" -lt $(( NUMofCUST+1 )) ];do
		echo 3 > /proc/sys/vm/drop_caches
		echo "iozone -t ${tVal[$COUNTER3]} -i ${WRorR[$COUNTER3]} -r ${rVal[$COUNTER3]} -s ${custSIZE[$COUNTER3]} -+n -w ${oType[$COUNTER3]}"
		let COUNTER3=COUNTER3+1
	done
fi
