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

ITER_REPS=150

# 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
		;;
	--max-users)
		MAX_USERS=$2
		shift 2
		;;
	--iterations)
		ITERATIONS=$2
		shift 2
		;;
	--reps-per-iter)
		ITER_REPS=$2
		shift 2
		;;
	--time-per-iter)
		ITER_TIME=$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
        
# Include monitor hooks
. $SHELLPACK_INCLUDE/include-monitor.sh
echo Shutting down apache server...
$SHELLPACK_INCLUDE/shellpack-bench-apachebuild --stop || die Failed to shutdown apache http server for restart.
if [ "$INSTALL_FORCE" = "yes" ]; then
	rm -rf $SHELLPACK_SOURCES/siege-${VERSION}
fi
if [ ! -d $SHELLPACK_SOURCES/siege-${VERSION}-installed ]; then
	mmtests_activity source-install
	$SHELLPACK_INCLUDE/shellpack-install-siege -v ${VERSION}  || die siege install script returned error
	mmtests_activity source-installed
fi
cd $SHELLPACK_SOURCES/siege-${VERSION}-installed || die Failed to cd to siege install directory
if [ "$INSTALL_ONLY" = "yes" ]; then
	echo siege installed only as requested.
	exit $SHELLPACK_SUCCESS
fi

echo Shutting down apache server...
$SHELLPACK_INCLUDE/shellpack-bench-apachebuild --stop || die Failed to shutdown apache http server for restart.
echo Starting apache server...
$SHELLPACK_INCLUDE/shellpack-bench-apachebuild --start || die Failed to start apache http server.
sleep 5

ITER_ARG=
if [ "$ITER_TIME" != "" ]; then
	ITER_ARG="-t ${ITER_TIME}s"
	TIMEOUT_ARG=$((ITER_TIME*3/2))
else
	ITER_ARG="-r $ITER_REPS"
	TIMEOUT_ARG=
fi

if [ -e /proc/sys/net/ipv4/tcp_tw_recycle ]; then
	echo Enabling tcp_tw_recycle
	sysctl -w net.ipv4.tcp_tw_recycle=1
else
	echo Enabling tcp_tw_reuse
	sysctl -w net.ipv4.tcp_tw_reuse=1
fi
echo Disabling TCP timestamp shuffling
sysctl net.ipv4.tcp_timestamps=0

echo Setting files limit $((MAX_USERS*1000))

# Bump the system hard limit first if it's too small otherwise the
# following ulimit will fail with EPERM.
cur_limit=$(sysctl fs.nr_open | awk '{print $3}')

if [ $cur_limit -lt $((MAX_USERS*1000)) ]; then
	sysctl -w fs.nr_open=$((MAX_USERS*1000))
fi

ulimit -n $((MAX_USERS*1000))

echo Running up to $MAX_USERS users, $ITERATIONS iterations per user
NR_THREADS=1
if [ "$NR_THREADS" = "" ]; then
	NR_THREADS=1
fi
THREADS=$NR_THREADS
NR_THREADS=$((NR_THREADS*2))
while [ $NR_THREADS -le $MAX_USERS ]; do
	THREADS="$THREADS $NR_THREADS"
	NR_THREADS=$((NR_THREADS*2))
done
if [ `echo $THREADS | awk '{print $NF}'` -ne $MAX_USERS ]; then
	THREADS="$THREADS $MAX_USERS"
fi
for NR_THREADS in $THREADS; do
	if [ $NR_THREADS -gt $MAX_USERS ]; then
		NR_THREADS=$MAX_USERS
	fi
	mmtests_activity process $NR_THREADS/$MAX_USERS
	monitor_pre_hook $LOGDIR_RESULTS $NR_THREADS
	for ITER in `seq 1 $ITERATIONS`; do
		echo Running $ITER/$ITERATIONS: siege -b $ITER_ARG -c $NR_THREADS
		RETRY=1
		FAILURES=0
		while [ $RETRY -eq 1 ]; do
			ATTEMPTS=$((ATTEMPTS+1))
			RETRY=0
			$TIME_CMD -o $LOGDIR_RESULTS/siege-${NR_THREADS}-${ITER}.time \
				$SHELLPACK_SOURCES/siege-${VERSION}-installed/bin/siege -b $ITER_ARG \
					-c $NR_THREADS \
						http://localhost/siege.html \
						2>&1 | grep -v HTTP/ | tee $LOGDIR_RESULTS/siege-${NR_THREADS}-${ITER}.log &
			PIDWAIT=$!
			wait_on_pid_exit $! $TIMEOUT_ARG
			if [ $? -ne 0 ]; then
				echo WARNING: Retry due to siege timeout
				killall -KILL siege
				FAILURES=$((FAILURES+1))
				RETRY=1
			fi

			echo Waiting on tcp_wait state to drop to an acceptable level
			WAITING=`netstat -n -a | grep TIME_WAIT | wc -l`
			while [ $WAITING -gt 1024 ]; do
				echo o $WAITING
				sleep 5
				WAITING=`netstat -n -a | grep TIME_WAIT | wc -l`
			done
		done
		if [ $FAILURES -ne 0 ]; then
			echo $FAILURES > $LOGDIR_RESULTS/siege-${NR_THREADS}-${ITER}.failures
		fi
	done
	monitor_post_hook $LOGDIR_RESULTS $NR_THREADS
done

echo siege completed successfully
exit $SHELLPACK_SUCCESS
#### Description siege
#### Details siege-bench 55
