#!/bin/bash
#!/bin/bash
P=sysbench-bench
DEFAULT_VERSION=0.4.12
. $SHELLPACK_INCLUDE/common.sh
TIME_CMD=`which time`
if [ "$TIME_CMD" = "" ]; then
        TIMEFORMAT="%2Uuser %2Ssystem %Relapsed %P%%CPU"
        TIME_CMD="time"
fi
install-depends gnuplot

DBDRIVER=postgres
DBUSER=nobody
EFFECTIVE_CACHESIZE=$((756*1048576))
SHARED_BUFFERS=$((32*1048576))
WORK_MEM=$((32*1048576))
MARIADB_VERSION=10.2.14
POSTGRES_VERSION=9.6.1
MARIADB_ADMIN_USER=root
MARIADB_ADMIN_PASSWORD=mmtests-default
RUN_CACHE_COLD=no

# 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
		;;
	--effective-cachesize)
		EFFECTIVE_CACHESIZE=$2
		shift 2
		;;
	--shared-buffers)
		SHARED_BUFFERS=$2
		shift 2
		;;
	--work-mem)
		WORK_MEM=$2
		shift 2
		;;
	--dbdriver)
		DBDRIVER=$2
		shift 2
		;;
	--max-threads)
		MAX_THREADS=$2
		shift 2
		;;
	--max-time)
		MAX_TIME=$2
		shift 2
		;;
	--max-transactions)
		MAX_TRANSACTIONS=$2
		shift 2
		;;
	--iterations)
		MAX_ITERATIONS=$2
		shift 2
		;;
	--workload-size)
		WORKLOAD_SIZE=$2
		shift 2
		;;
	--read-only)
		READONLY=yes
		shift
		;;
	--cache-cold)
		RUN_CACHE_COLD=yes
		shift
		;;
	*)
		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

MARIADB_TABLE_DRIVER=innodb
MARIADB_START_OPTIONS="--innodb_buffer_pool_size=${SHARED_BUFFERS},--innodb_log_file_size=512M"
OLTP_TESTTYPE=complex

# Always scrub sources as the benchmark in question does not properly
# reinitialise itself if the data partitions gets scrubbed
if [ "$INSTALL_ONLY" = "yes" ]; then
	rm -rf $SHELLPACK_SOURCES/mariadb*
	rm -rf $SHELLPACK_SOURCES/sysbench*
fi

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

# Calculate OLTP tablesize. 1 megabyte is around 4500 rows
OLTP_TABLESIZE=$((WORKLOAD_SIZE/1048576*4500))

# Set transaction type
OLTP_TRANSTYPE=
if [ "$READONLY" = "yes" ]; then
	OLTP_TRANSTYPE=--oltp-read-only
fi

# Automatically calculate number of transactions if requested
if [ "$MAX_TRANSACTIONS" = "auto" ]; then
	if [ "$READONLY" = "yes" ]; then
		CEILING=400000
		FLOOR=1000
	else
		CEILING=10000
		FLOOR=1000
	fi
	WEIGHT_MAINMEMORY=2
	WEIGHT_DISK=80
	cat > /tmp/points <<EOF
0 $((CEILING/2))
$SHARED_BUFFERS $CEILING
$EFFECTIVE_CACHESIZE $((CEILING/WEIGHT_MAINMEMORY*3/2))
$MEMTOTAL_BYTES $((CEILING/WEIGHT_MAINMEMORY))
$((MEMTOTAL_BYTES*4/3)) $((CEILING/WEIGHT_MAINMEMORY/2))
$((MEMTOTAL_BYTES*2)) $((CEILING/WEIGHT_DISK))
$((MEMTOTAL_BYTES*8)) $((CEILING/WEIGHT_DISK*2))
EOF

	cat > /tmp/gnuplot.script <<EOF
set terminal dumb
set table "/tmp/coordinates"
plot '/tmp/points' smooth cspline
EOF
	cat /tmp/gnuplot.script | gnuplot > /dev/null

        MAX_TRANSACTIONS=`awk "{ if (\\$1 >= \$WORKLOAD_SIZE) print \\$2 }" /tmp/coordinates | head -1`
	MAX_TRANSACTIONS=`perl -e "print int $MAX_TRANSACTIONS"`
	if [ "$MAX_TRANSACTIONS" = "" ]; then
		MAX_TRANSACTIONS=$FLOOR
	fi
	if [ $MAX_TRANSACTIONS -lt $FLOOR ]; then
		MAX_TRANSACTIONS=$FLOOR
	fi
	if [ "$READONLY" != "yes" ]; then
		MAX_TRANSACTIONS=$((MAX_TRANSACTIONS/8))
	fi
	mv /tmp/points $LOGDIR_RESULTS/sysbench-auto-points
	mv /tmp/coordinates $LOGDIR_RESULTS/sysbench-auto-coordinates
fi

# Initialise database
SYSBENCH_DB_OPTIONS=
export PATH=$SHELLPACK_SOURCES/sysbench-${VERSION}-installed/bin:$PATH
echo Configuring paths for $DBDRIVER
if [ "$DBDRIVER" = "postgres" ]; then
	SYSBENCH_DB_OPTIONS="--db-driver=pgsql --pgsql-db=pgtest"
	PSQL=$SHELLPACK_SOURCES/postgresbuild-${POSTGRES_VERSION}-installed/bin/psql
	RUNDBUSER="su -s /bin/bash $DBUSER -c"
	export PATH=$SHELLPACK_SOURCES/postgresbuild-${POSTGRES_VERSION}-installed/bin:$PATH
	export LD_LIBRARY_PATH=$SHELLPACK_SOURCES/postgresbuild-${POSTGRES_VERSION}-installed/lib
	export PGHOST=/var/run/postgresql
	export PGPORT=5432
else
	MYSQLADMIN="mysqladmin -u $MARIADB_ADMIN_USER -p$MARIADB_ADMIN_PASSWORD"
	SYSBENCH_DB_OPTIONS="--mysql-user=$MARIADB_ADMIN_USER --mysql-password=$MARIADB_ADMIN_PASSWORD --mysql-db=sbtest --mysql-socket=/tmp/mysql.sock --mysql-table-engine=innodb"
	export PATH=$SHELLPACK_SOURCES/mariadbbuild-${MARIADB_VERSION}-installed/bin:$PATH
	export LD_LIBRARY_PATH=$SHELLPACK_SOURCES/mariadbbuild-${MARIADB_VERSION}-installed/lib
fi

FIRST_TIME=yes
SYSBENCH_OPTIONS="$MAX_TIME_COMMAND $MAX_TRANSACTIONS_COMMAND $SYSBENCH_DB_OPTIONS"
OLTP_OPTIONS="--test=oltp $OLTP_TRANSTYPE \
		--oltp-test-mode=$OLTP_TESTTYPE \
		--oltp-table-size=$OLTP_TABLESIZE"
THREADS=
START_THREAD=1
END_THREAD=$MAX_THREADS
if [ $END_THREAD -ge 32 ]; then
	THREADS=`seq $START_THREAD 4 8`
	THREADS="$THREADS `seq 12 9 32`"
	THREADS="$THREADS `seq 48 31 $END_THREAD`"
elif [ $END_THREAD -ge 8 ]; then
	THREADS=`seq $START_THREAD 3 8`
	THREADS="$THREADS `seq 12 6 $END_THREAD`"
else
	THREADS=`seq $START_THREAD 2 $END_THREAD`
fi
if [ `echo $THREADS | awk '{print $NF}'` -ne $END_THREAD ]; then
	THREADS="$THREADS $END_THREAD"
fi
for NR_THREADS in $THREADS; do
	if [ $NR_THREADS -lt 1 ]; then
		continue
	fi
	mmtests_activity process $NR_THREADS/$END_THREAD
	if [ "$DATABASE_INIT_ONCE" != "yes" -o "$FIRST_TIME" = "yes" ]; then
		echo Starting database for initialisation
		mmtests_activity database-init
		$SHELLPACK_INCLUDE/shellpack-bench-${DBDRIVER}build --start	\
			--start_opts $MARIADB_START_OPTIONS			\
			--effective_cachesize $EFFECTIVE_CACHESIZE		\
			--shared_buffers $SHARED_BUFFERS			\
			--work_mem $WORK_MEM || die Failed to get usable database installation

		echo Preparing database
		if [ "$DBDRIVER" = "postgres" ]; then
			$RUNDBUSER "$PSQL template1 -c 'DROP DATABASE pgtest;'"
			$RUNDBUSER "$PSQL template1 -c 'DROP ROLE sbtest;'"
			$RUNDBUSER "$PSQL template1 -c 'CREATE DATABASE pgtest;'" || die Failed to setup database
			$RUNDBUSER "$PSQL template1 -c 'CREATE ROLE sbtest with LOGIN;'" || die Failed to create sbtest role
			cp $SHELLPACK_SOURCES/postgresbuild-${POSTGRES_VERSION}-installed/data/postgresql.conf $LOGDIR_RESULTS
		else
			eval $MYSQLADMIN -f drop sbtest
			eval $MYSQLADMIN create sbtest || die Failed to setup database
			cp /etc/my.cnf $LOGDIR_RESULTS
		fi

		echo Initialising database for sysbench: $SYSBENCH_OPTIONS $OLTP_OPTIONS
		$TIME_CMD -o $LOGDIR_RESULTS/load-${NR_THREADS}.time \
			sysbench $SYSBENCH_OPTIONS $OLTP_OPTIONS prepare \
			> $LOGDIR_RESULTS/load-${NR_THREADS}.log
	fi
	FIRST_TIME=no

	if [ "$RUN_CACHE_COLD" = "yes" ]; then
		echo Stopping database to drop cache
		$SHELLPACK_INCLUDE/shellpack-bench-${DBDRIVER}build --stop

		echo Dumping cache to run database cache cold as requested
		echo 3 > /proc/sys/vm/drop_caches

		echo Starting database cache cold
		$SHELLPACK_INCLUDE/shellpack-bench-${DBDRIVER}build --start \
			--effective_cachesize $EFFECTIVE_CACHESIZE \
			--shared_buffers $SHARED_BUFFERS \
			--work_mem $WORK_MEM || die Failed to get usable database installation
	fi

	mmtests_activity sysbench-$NR_THREADS
	monitor_pre_hook $LOGDIR_RESULTS $NR_THREADS
	for ITER in `seq 1 $MAX_ITERATIONS`; do
		echo Running $NR_THREADS threads Iteration $ITER/$MAX_ITERATIONS
		echo sysbench $SYSBENCH_OPTIONS $OLTP_OPTIONS --num-threads=$NR_THREADS run | tee $LOGDIR_RESULTS/sysbench-raw-$NR_THREADS-$ITER
		eval $TIME_CMD -o $LOGDIR_RESULTS/time-${NR_THREADS}-${ITER} \
			sysbench $SYSBENCH_OPTIONS \
				$OLTP_OPTIONS \
				--num-threads=$NR_THREADS \
				run >> $LOGDIR_RESULTS/sysbench-raw-$NR_THREADS-$ITER
		SYSRET=$?
		if [ $SYSRET -ne 0 ]; then
			cat $LOGDIR_RESULTS/sysbench-raw-$NR_THREADS-$ITER
			die sysbench exited abnormally
		fi
	done
	monitor_post_hook $LOGDIR_RESULTS $NR_THREADS
done

echo Cleaning database: sysbench $SYSBENCH_OPTIONS --test=oltp $OLTP_OPTIONS cleanup
sysbench $SYSBENCH_OPTIONS --test=oltp $OLTP_OPTIONS cleanup

# Shutdown server
if [ "$DBDRIVER" = "postgres" ]; then
	$RUNDBUSER "$PSQL template1 -c 'DROP DATABASE pgtest;'"
	$RUNDBUSER "$PSQL template1 -c 'DROP ROLE sbtest;'"
else
	eval $MYSQLADMIN -f drop sbtest
fi
$SHELLPACK_INCLUDE/shellpack-bench-${DBDRIVER}build --stop

echo sysbench successfully completed
exit $SHELLPACK_SUCCESS
#### Description Run the sysbench benchmark
#### Details sysbench-bench 86
