#!/bin/bash
# This is the script to run the lmbench benchmark
#
# optparam prefix "-m" value Specify the amount of memory to use (megs)
# optparam prefix "-f" value yes|no Do a fast check on memory latency [no]
# optparam prefix "-s" value yes|no Skip filesystem latency tests [no]
# optparam prefix "-d" value repeat device "description" Do a disk test on the specified device (with the given text description)
# optparam prefix "-c" value Specify the speed (in MhZ) of the CPU
# optparam prefix "-t" value Specify the tempfile location (default is /usr/tmp)
# optparam prefix "-r" value repeat Run test to the remote host
#

. /etc/autobench.conf || . functions

# Process the arguments ######################################################
MEM="\n"
FASTMEM="\n"
SLOWFS="\n"
DISKS="\n"
DISKS_DESC=""
MHZ="\n"
FSDIR="\n"
REMOTE="\n"

while [ $# -gt 1 ]; do
  case $1 in
    -m)
      MEM="$2\n"
      shift 2
      ;;
    -f)
      FASTMEM="$2\n"
      shift 2
      ;;
    -s)
      SLOWFS="$2\n"
      shift 2
      ;;
    -d)
      DISKS="$2 $DISKS"
      DISKS_DESC="$3\n$DISKS_DESC"
      shift 3
      ;;
    -c)
      MHZ="$2\n"
      shift 2
      ;;
    -t)
      FSDIR="$2\n"
      shift 2
      ;;
    -r)
      REMOTE="$2 $REMOTE"
      shift 2
      ;;
    *)
      echo "$1 unknown"
      shift
      ;;
  esac
done

CONFIG_STRING="$MEM$FASTMEM$SLOWFS$DISKS$DISKS_DESC$MHZ$FSDIR${REMOTE}\nno"

# Install support files ######################################################
if [ ! \( -d $AUTODIR/sources/lmbench -a -d $AUTODIR/sources/lmbench/bin/ \) ];
then
  $AUTODIR/scripts/benchmarks/install/lmbench || exit 2
fi

getcommand doprofilers
doprofilers install

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

pushd $AUTODIR/sources/lmbench/src > /dev/null
echo -en "$CONFIG_STRING" | ../scripts/config-run
rm -rf ../results/*-*

# Run the benchmark ##########################################################
startprofilers
../scripts/results 
stopprofilers
doprofilers report
doprofilers postprocess

# Copy the results ###########################################################
for file in ../results/*/*; do
  cp ../results/*/* $LOGDIR/benchmark/`echo $file | sed -e "s:.*/::"`.$RUN_NUMBER
done

popd > /dev/null
