#!/bin/bash
#
# Copyright (c) 2002-2003 Silicon Graphics, Inc.  All Rights Reserved.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it would be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write the Free Software Foundation,
# Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#
# 
# Do a bonnie++ create/read/delete run using 8192 files (unless
# BONNIE_KFILES environment variable is set -- note this number
# is multiplied by 1024 by bonnie++).  By default the files are
# created in the same directory, BONNIE_NDIRS specifies a count
# of subdirs to evenly spread files through.  Regular files are
# created by default (BONNIE_FILETYPE=regular), alternatively
# the values "symlinks" and "hardlinks" can be used.
# 
# The script then massages the output into CSV format with the
# human-readable output preceding it as a "comment" (#-prefixed).
# 
BONNIE_NDIRS=${BONNIE_NDIRS:=1}
BONNIE_KFILES=${BONNIE_KFILES:=8}
BONNIE_FILETYPE=${BONNIE_FILETYPE:=regular}

[ -z "$here" ] && here=`pwd`
. $here/common.bonnie

# 
# Sample bonnie throughput output (stderr):
#Version 1.02c       ------Sequential Create------ --------Random Create--------
#                    -Create-- --Read--- -Delete-- -Create-- --Read--- -Delete--
#              files  /sec %CP  /sec %CP  /sec %CP  /sec %CP  /sec %CP  /sec %CP
#                  4   525  27 +++++ +++   517  26   526  25 +++++ +++   230  12
# [ Note: the "files" can also be "^files:max" and possibly other things ]
filter_stderr()
{
	sed -e 's/^..................../# /g' | awk '{print} END {print "#"}'
}

# 
# Sample bonnie IOPs output (stdout):
# ",,,,,,,,,,,,,,4,525,27,+++++,+++,517,26,526,25,+++++,+++,230,12"
# [ nathans note: always get +++ due to 0:0 for min:max file sizes. ]
# [ I will need to fix the filter if I ever start using file sizes. ]
# 
filter_stdout()
{
	perl -ne '
		chomp;
		s/\+*//g; s/,+/,/g; s/^,//; s/,$//;
		@values = split /,/;
		printf "%9s", 1024 * shift @values;
		for ($i = 0; $i <= $#values; $i++) {
			if ($i % 2) { printf ",%4s%%", $values[$i] }
			else        { printf ",%10s", $values[$i] }
		}
		printf "\n";
	'
}

if [ $# -gt 0 ]; then
	printf "%9s,%10s,%5s,%10s,%5s,%10s,%5s,%10s,%5s\n" files \
		seqCR/s scCPU seqRM/s srCPU randCR/s rcCPU randRM/s rrCPU
	exit 0
fi

case "$BONNIE_FILETYPE"
in
	regular)  BONNIE_FILETYPE=0 ;;
	symlink)  BONNIE_FILETYPE=-2 ;;
	hardlink) BONNIE_FILETYPE=-1 ;;
	*)  echo "BONNIE_FILETYPE has an unrecognised value" && exit 1 ;;
esac

run_bonnie -s 0 -n $BONNIE_KFILES:$BONNIE_FILETYPE:0:$BONNIE_NDIRS
