#!/bin/bash
# bonnie++
#
# reqparam value The scratch directory to run the test in
# optparam prefix "-s" value SIZE[:CHUNK] Specify the file size for IO performance (in megabytes), also specify the chunk size (in bytes)
# optparam prefix "-n" value NUMBER[:MAX[:MIN][:DIRECTORIES]]] Specify the number of files for the file creation test. If min and max and specified then the files will have a random size between min and max. Directories is the number of directories to distribute the files into.
# optparam prefix "-m" value Specify the machine name for the pretty output
# optparam prefix "-r" value Specify the amount of RAM (megabytes) (0 to disable sanity check)
# optparam prefix "-x" value Number of tests to run
# optparam prefix "-u" value uid:gid Specify a uid and gid to run as
# optparam prefix "-g" value Specify a gid to run as
# optparam prefix "-q" Suppress plain-text output
# optparam prefix "-f" Skip per-char IO tests
# optparam prefix "-b" no write buffering
# optparam prefix "-p" Number of processes to server semaphores for
# optparam prefix "-y" Wait for semaphore before each test
# 

. /etc/autobench.conf || . functions

# Process the arguments ######################################################
if [ $# -lt 2 ]; then
  echo "You must specify the scratch directory"
  exit 1
fi

WORKDIR=$1
shift
FIRST_ARGS="-d $WORKDIR -r 0"
if echo " $*" | grep -q " -u "; then
  true
else
  FIRST_ARGS="-u root $FIRST_ARGS"
fi

# Install support files ######################################################
if [ ! -e $AUTODIR/sources/bonnie++ ]; then
  $AUTODIR/scripts/benchmarks/install/bonnie
fi

# Also get any commands that this benchmark uses... especially profilers
getcommand doprofilers
doprofilers install

# Prepare the benchmark ######################################################

mkdir -p $WORKDIR
fs_type $WORKDIR $LOGDIR/benchmark/fstype.$RUN_NUMBER

touch $LOGDIR/benchmark/bonnie++.$RUN_NUMBER
pushd $AUTODIR/sources/bonnie++ > /dev/null

# Run the benchmark ##########################################################

startprofilers # start what ever profilers have been specified
./bonnie++ $FIRST_ARGS $* | tee $LOGDIR/benchmark/bonnie++.$RUN_NUMBER
stopprofilers # stop the profilers
doprofilers report # do the reporting
doprofilers postprocess  # and postprocessing

# Clean up ###################################################################
popd > /dev/null
