#!/bin/bash
# This is the script to run the tbench benchmark
#
# reqparam value Class: client|server|both Should this instance of the benchmark run the client, the server, or both
# reqparam value The number of instance to run.  For server 0 means stop > 0 mean start.  For client or both specifies the number of clients to run.
# optparam value The address of the server (required for client)
# optparam prefix "-v" Show version
# optparam prefix "-s" Synchronous file IO
# optparam prefix "-S" Synchronous directories (mkdir, unlink...)
# optparam prefix "-t" value Set socket options

. /etc/autobench.conf || . functions

# Process the arguments ######################################################
if [ $# -lt 2 ]; then
  echo "You must specify client, server, or both, and the number of instances"
  exit 1
fi
TYPE=$1
NUM_CLIENTS=$2

case $1 in
  server|both)
    # Server or both doesn't care about it's address
    shift 2
    while [ $# -gt 0 -a "`echo $1 | cut -c1`" != "-" ]; do
      shift
    done
    ;;
  client)
    if [ $# -lt 3 ]; then
      echo "You must specify the number of clients, and the server address"
      exit 1
    fi
    SERVER_ADDRESS=$3
    shift 3
    ;;
  *)
    ;;
esac

# Install support files ######################################################
if [ ! \( -d $AUTODIR/sources/dbench -a -x $AUTODIR/sources/dbench/dbench \) ];
then
  getcommand dbench  #tbench used the dbench install script, so we need it.
  $AUTODIR/scripts/benchmarks/install/dbench || exit 2
fi

getcommand doprofilers
doprofilers install

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

function mykill() {
  #So I can supress the message when something executed from within a script
  # is killed
  kill $1
}
touch $LOGDIR/benchmark/tbenchout.$RUN_NUMBER
pushd $AUTODIR/sources/dbench > /dev/null

# Run the benchmark ##########################################################
case $TYPE in

  both)
    ./tbench_srv &
    PID=$!
    startprofilers
    ./tbench $NUM_CLIENTS $@ | tee $LOGDIR/benchmark/tbenchout.$RUN_NUMBER
    stopprofilers
    doprofilers report
    doprofilers postprocess
    mykill $PID 2> /dev/null
    ;;

  client)
    startprofilers
    ./tbench $NUM_CLIENTS $SERVER_ADDRESS $@ | tee $LOGDIR/benchmark/tbenchout.$RUN_NUMBER
    stopprofilers
    doprofilers report
    doprofilers postprocess
    ;;

  server)
    if [ $NUM_CLIENTS -ne 0 ]; then
        # Start the server
        watchfor "^" 'test $count -lt 2 && startprofilers'
        ./tbench_srv | tee $LOGDIR/benchmark/tbench_srvout.$RUN_NUMBER &
        echo $! > $TMPDIR/tbench-server.pid
    else
        # Stop the server
        if [ -e $TMPDIR/profilers_running ]; then
          stopprofilers
          doprofilers report
          doprofilers postprocess
        fi
        if [ -e $TMPDIR/tbench-server.pid ]; then
          PID=`cat $TMPDIR/tbench-server.pid`
          ps p $PID | grep -q tbench_srv
          if [ $? = 0 ]; then
            mykill $PID 2> /dev/null
          else
            killall tbench_srv 2> /dev/null
          fi
          rm $TMPDIR/tbench-server.pid
        else
          killall tbench_srv 2> /dev/null
        fi
    fi | streamfilter >&2 &
    # We want this to run in the background, but it does use the streamfilter.
    # Since the streamfilter in run isn't backgrounded, we run one here that's
    # backgrounded.  And then redirect stdout to stderr on the outside of it.
    # That way the outside streamfilter will exit because it will get no input
    ;;

  *)
    exit 1
  ;;
esac

popd > /dev/null
