#!/bin/bash
# Run unixbench benchmark

P=unixbench-bench
DEFAULT_VERSION=5.1.3
. $SHELLPACK_INCLUDE/common.sh
TIME_CMD=`which time`
if [ "$TIME_CMD" = "" ]; then
        TIMEFORMAT="%2Uuser %2Ssystem %Relapsed %P%%CPU"
        TIME_CMD="time"
fi
TESTTIME=15
ITERATIONS=12
TESTLIST=
MAX_THREADS=$NUMCPUS

# Basic argument parser
TASKSET_SERVER=
TASKSET_CLIENT=
SERVERSIDE_COMMAND=none
SERVERSIDE_NAME=`date +%Y%m%d-%H%M-%S`

while [ "$1" != "" ]; do
	case "$1" in
	-v)
		VERSION=$2
		shift 2
		;;
	--serverside-command)
		SERVERSIDE_COMMAND=$2
		shift 2
		;;
	--serverside-name)
		SERVERSIDE_NAME=$2
		shift 2
		;;
	--install-only)
		INSTALL_ONLY=yes
		shift
		;;
	--install-force)
		INSTALL_FORCE=yes
		shift
		;;
	--min-threads)
		UNIXBENCH_MIN_THREADS=$2
		shift 2
		;;
	--max-threads)
		UNIXBENCH_MAX_THREADS=$2
		shift 2
		;;
	--iterations)
		UNIXBENCH_ITERATIONS=$2
		shift 2
		;;
	--workloads)
		UNIXBENCH_WORKLOADS=$2
		shift 2
		;;
	*)
		echo Unrecognised option: $1
		shift
	esac
done
if [ "$TASKSET_SERVER" != "" ]; then
	echo TASKSET_SERVER: $TASKSET_SERVER
	echo TASKSET_CLIENT: $TASKSET_CLIENT
fi
if [ -z "$VERSION" ]; then
	VERSION=$DEFAULT_VERSION
fi

if [ "$INSTALL_FORCE" = "yes" ]; then
	rm -rf $SHELLPACK_SOURCES/unixbench-${VERSION}
fi
if [ ! -d $SHELLPACK_SOURCES/unixbench-${VERSION}-installed ]; then
	mmtests_activity source-install
	$SHELLPACK_INCLUDE/shellpack-install-unixbench -v ${VERSION}  || die unixbench install script returned error
	mmtests_activity source-installed
fi
cd $SHELLPACK_SOURCES/unixbench-${VERSION}-installed || die Failed to cd to unixbench install directory
if [ "$INSTALL_ONLY" = "yes" ]; then
	echo unixbench installed only as requested.
	exit $SHELLPACK_SUCCESS
fi
# Include monitor hooks
. $SHELLPACK_INCLUDE/include-monitor.sh

echo $UNIXBENCH_WORKLOADS > $LOGDIR_RESULTS/workloads

export UB_TESTDIR=$SHELLPACK_DATA

for NR_THREADS in $UNIXBENCH_MIN_THREADS $UNIXBENCH_MAX_THREADS; do
	mmtests_activity process $NR_THREADS/$UNIXBENCH_MAX_THREADS
	for WORKLOAD in $UNIXBENCH_WORKLOADS; do
		mmtests_activity $WORKLOAD-$NR_THREADS
		monitor_pre_hook $LOGDIR_RESULTS $NR_THREADS
for ITERATION in `seq 1 $UNIXBENCH_ITERATIONS`; do
	mmtests_activity iteration $ITERATION
			echo Running test $WORKLOAD iteration $ITERATION/$UNIXBENCH_ITERATIONS
			./Run -q -c $NR_THREADS $WORKLOAD 2>&1 | \
				tee $LOGDIR_RESULTS/$WORKLOAD-${NR_THREADS}-${ITERATION}.log \
				|| die Failed ro run unixbench
done
		monitor_post_hook $LOGDIR_RESULTS $NR_THREADS
	done
done
exit $SHELLPACK_SUCCESS
#### Description unixbench
#### Details unixbench-bench 7
