#!/bin/ksh
#
#ident	"@(#)bonnie.sh	1.8	04/12/08 SMI"
#
# Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
# Shell script to initiate disk I/O performance benchmark,
# use multiple iterations and get average benchmark results
# By Svetlana Kostyuchenko

BASEDIR=/export/home

export  BASEDIR

SDIR=$BASEDIR/bonnie.tmp
MACHINE=`uname -n`
FSIZE=200
COUNT=5

usage()
{
	echo
	echo "Usage: $0 [ -i <# of iterations, default is 5. If -1,"
	echo "           then RUNTIME environment variable is used>]"
	echo "                 [ -m <machine lable, default 'uname -n'>]"
	echo "                 [ -s <file size, default is 200MB> ]"
	echo "                 [ -d <scratch dir, default is $BASEDIR/bonnie.tmp> ]"
	echo
}

if [ $# -gt 8 ]
then
	usage
	exit 1
fi

while [ $# -gt 0 ]
do
	case $1 in
	-i) COUNT=$2; export COUNT; shift; shift;
		;;
	-s) FSIZE=$2; export FSIZE; shift; shift;
	    echo "File size: "$FSIZE
		;;
	-d) SDIR=$2; export SDIR; shift; shift;
	    echo "Scratch dir: "$SDIR
	    if [ ! -d $SDIR ]
	    then mkdir -p $SDIR
	    fi
		;;
	-m) MACHINE=$2; export MACHINE; shift; shift;
	    echo "Machine label is: "$MACHINE
		;;
	*) echo
	   echo "$0: ERROR:"
	   echo "Unknown option"
	   usage
	   exit 1
	esac
done

if [ -z "$COUNT" ]
then
	COUNT=5
elif [ $COUNT -le 0 -a $COUNT -ne -1 ]
then
	echo "$0: ERROR:"
	echo "Wrong option"
	usage
	exit 1
fi

if [ -z "$FSIZE" ]
then
	FSIZE=200
elif [ $FSIZE -le 0 ]
then
	echo "$0: ERROR:"
	echo "Wrong option"
	usage
	exit 1
fi

if [ ! -d $SDIR ]
then
	mkdir -p $SDIR
fi
chmod 777 $SDIR 2>/dev/null

if [ -f $SDIR/bonnie.out* -o -f $SDIR/Bonnie.* -o -f $SDIR/bonnie.tmp ]
then
	rm -f $SDIR/Bonnie.* 2>/dev/null
	rm -f $SDIR/bonnie.out* 2>/dev/null
	rm -f $SDIR/bonnie.tmp 2>/dev/null
fi

if [ -f $SDIR/bonnie/bonnie.results ]
then cp $SDIR/bonnie/bonnie.results $SDIR/bonnie/bonnie.results.saved 2>/dev/null
fi

# use localhost as default nodename if $MACHINE var is null. added by phost 2004.07.07
if [ -z "$MACHINE" ]
then
	MACHINE='localhost'
fi

MACHINE_EXT=`echo "$MACHINE" | sed -e 's/\^/\\\^/g' | sed -e 's/\./\\\./g' | sed -e 's/\[/\\\[/g' | sed -e 's/\"/\\\"/g'`

if [ -z "$COUNT" ]
then
	COUNT=5
fi
count=$COUNT

echo
echo > $SDIR/bonnie.out1
echo > $SDIR/bonnie.out2 
echo "		DISK & FILESYSTEMS I/0 BENCHMARK " 
echo
if [ $COUNT -eq -1 ]
then
	echo "Running time: "$RUNTIME
else
	echo "Iterations requested: "$COUNT 
fi
echo "Please wait for the benchmark to be completed..."
echo "Started benchmark: "`date` >> $SDIR/bonnie.out2
if [ $COUNT -eq -1 ]
then
	echo "Running time: "$RUNTIME >> $SDIR/bonnie.out2
else
	echo "Iterations requested: "$COUNT >> $SDIR/bonnie.out2
fi
echo >> $SDIR/bonnie.out2

if [ $COUNT -eq -1 ]
then
	sleep $RUNTIME &
	sleepid=$!
	count=0
else
	sleepid=-1
fi

while [ $COUNT -gt 0 -o -d /proc/$sleepid ]
do
	./Bonnie -d $SDIR -m $MACHINE -s $FSIZE -f html >>$SDIR/bonnie.tmp 2>&1
	if [ $? -ne 0 ]
	then
        	echo
        	echo "!ERROR: Bonnie returned an error "
		cat $SDIR/bonnie.tmp
        	echo
		rm $SDIR/Bonnie.*
        	exit 1
	fi

	if [ $COUNT -eq -1 ]
	then
		count=`expr $count + 1`
		echo "Completed iteration $count at " `date`
	else
		COUNT=`expr $COUNT - 1`
		echo "Iterations left: "$COUNT
	fi
done

# added by phost, 2004.10.10, aligh the output under right column.
machine_tab=`expr ${#MACHINE} + 7`
machine_tab=`expr $machine_tab / 8`
num=$machine_tab
machine_head=""
while [ $num -gt 0 ]
do
	machine_head=${machine_head}"\t"
	num=`expr $num - 1`
done

size_tab=`expr ${#FSIZE} + 7`
size_tab=`expr $size_tab / 8`
num=$size_tab
size_head=""
while [ $num -gt 0 ]
do
	size_head=${size_head}"\t"
	num=`expr $num - 1`
done

total_length=`expr $machine_tab + $size_tab`
total_length=`expr $total_length + 12`
total_length=`expr $total_length \* 8`
line=""
while [ $total_length -gt 0 ]
do
	line=${line}"-"
	total_length=`expr $total_length - 1`
done
# end

grep '<' $SDIR/bonnie.tmp | grep '>'  > $SDIR/bonnie.out.html 2>&1
printf "${machine_head}${size_head}%s\n" "------------Sequential Output------------	------Sequential Input------	--Random--" >> $SDIR/bonnie.out1
printf "${machine_head}${size_head}%s\n" "--Per Char--	--Block--	--Rewrite--	--Per Char--	--Block--	--Seeks--" >> $SDIR/bonnie.out1
sed -e 's/<\/[^>]*>/	/g' $SDIR/bonnie.out.html | sed -e 's/<[^>]*>//g' >> $SDIR/bonnie.out1 2>&1
printf "\n" >> $SDIR/bonnie.out2
printf "${machine_head}${size_head}%s\n" "DISK & FILESYSTEMS I/0 BENCHMARK " >> $SDIR/bonnie.out2
printf "\n" >> $SDIR/bonnie.out2
printf "${machine_head}${size_head}Machine: %s\n" $MACHINE >> $SDIR/bonnie.out2
echo "${machine_head}${size_head}" `date` >> $SDIR/bonnie.out2
printf "\n"  >> $SDIR/bonnie.out2
echo "Benchmark completed: "`date` >> $SDIR/bonnie.out2
printf "\n" >> $SDIR/bonnie.out2
grep "Sequential Output" $SDIR/bonnie.out1 | grep "-" | uniq > $SDIR/bonnie.out3 2>&1
printf "\n" >> $SDIR/bonnie.out3
grep "Per Char" $SDIR/bonnie.out1 | grep "-" | uniq >> $SDIR/bonnie.out3 2>&1
printf "\n" >> $SDIR/bonnie.out3
printf "%s${machine_head}%s${size_head}%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t\n" "Machine" "MB" "K/sec" "%CPU" "K/sec" "%CPU" "K/sec" "%CPU" "K/sec" "%CPU" "K/sec" "%CPU" " /sec" "%CPU" >> $SDIR/bonnie.out3 2>&1

printf "%s\n" $line >> $SDIR/bonnie.out2
grep $MACHINE_EXT $SDIR/bonnie.out1 > $SDIR/bonnie.out4 2>&1

AVG_lable="AVG:"$machine_head
cat $SDIR/bonnie.out4 | nawk -F" " '  BEGIN { cnt = '$count' };  {
 S1 = "'$AVG_lable'";
 S2 = (S2 += $2);
 S3 = (S3 += $3); 
 S4 = (S4 += $4);
 S5 = (S5 += $5);
 S6 = (S6 += $6);
 S7 = (S7 += $7); 
 S8 = (S8 += $8);
 S9 = (S9 += $9); 
 S10 = (S10 += $10);
 S11 = (S11 += $11);
 S12 = (S12 += $12);
 S13 = (S13 += $13);
 S14 = (S14 += $14) };
END {printf "%s%d\t%d\t%4.1f\t%d\t%4.1f\t%d\t%4.1f\t%d\t%4.1f\t%d\t%4.1f\t%d\t%4.1f\n", S1, S2/cnt, S3/cnt, S4/cnt, S5/cnt, S6/cnt, S7/cnt, S8/cnt, S9/cnt, S10/cnt, S11/cnt, S12/cnt, S13/cnt, S14/cnt} ' > $SDIR/bonnie.out5

cat $SDIR/bonnie.out4 >> $SDIR/bonnie.out3
printf "%s\n" $line >> $SDIR/bonnie.out3
cat $SDIR/bonnie.out5 >> $SDIR/bonnie.out3
cat $SDIR/bonnie.out3 >> $SDIR/bonnie.out2
printf "%s\n" $line >> $SDIR/bonnie.out2
cat $SDIR/bonnie.out2
cat $SDIR/bonnie.out2 > $SDIR/bonnie.results 2>&1
echo
echo "!PASS: Benchmark results are stored in $SDIR/bonnie.results"
echo

#Clean up
#rm $SDIR/bonnie.out* 2>/dev/null
rm $SDIR/Bonnie.* 2>/dev/null
printf "%s\n" $line >> $SDIR/bonnie.out2
