#!/bin/sh
#
# $Id: benchdt,v 1.3 1999/05/11 11:59:30 roca Exp $
#
# benchdt
#
#	Modified by : V. Roca
#	Date        : dec 1997
#
#	Server benchdt tool.
#

###### Constants : set accordingly ######

# In case you use extensions to distinguish various bench/benchd versions...
BENCH_EXT='.linux'
#BENCH_EXT='.solaris'

# Message to add to trace file.
MSG=''

# NB: The first figure is the year. Set accordingly...
DATE=`date +"%y.%W.%w_%HH%M"`

# If you want the trace file to be in the /tmp dir...
DIRRESULT='/tmp'

HOST=`uname -n`
SYSTEM=`uname -s`
SYSTEM_VER=`uname -r`		 # NB: Version must be a single word!


###### Procedures ######

usage()
{
	echo 'Usage: benchdt [tcp|udp|rtt|rttudp] [sock|xti]'
	echo '	(in that order !)'
	echo '	additional optional arguments:'
	echo '		with the xti API: [V0|V1] [push]'
	echo '		with tcp: [nodelay]'
	echo '		in all cases: [rt] [cpustat1] [cpustat3] [sync] [port]'
	exit 1
}


getcpunb()
{
	# Method depend on system
	CPU_NB=1
}


benchrun()
{
	echo '================================================================================'
	# Format:
	#	Test co_bench date: 436.1_11H00 \
	#	Host: etna01 Peerhost: localhost \
	#	System: AIX 4.1.2 Provider: CCA.tcp.nodelay API: xti.t_sndseg \
	#	Fork: 1 CPU: 1
	#
	echo "Test $APPLI date: $DATE Host: $HOST PeerHost: * System: $SYSTEM $SYSTEM_VER Provider: $PROVIDER API: $API Fork: * CPU: $CPU_NB"
	echo "$MSG"
	echo '================================================================================'

	# Start with a cleanup...
	#
	ps -e | awk "benchd { print \"kill \" \$1 }" | sh

	echo "benchd${BENCH_EXT} $API_ARG $PROV_ARG $PORT_ARG $RCV_VERSION_ARG $NODELAY_ARG $DTONLY_ARG $PUSH_ARG $RT_ARG $CPUSTAT_ARG $SYNC_ARG -m120 -silent"
	benchd${BENCH_EXT}  $API_ARG $PROV_ARG $PORT_ARG $RCV_VERSION_ARG $NODELAY_ARG $DTONLY_ARG $PUSH_ARG $RT_ARG $CPUSTAT_ARG $SYNC_ARG -m120 -silent
}


###### main ######


#
# Parameters parsing.
#

if [ $# -lt 2 ]
then
	usage
fi

case $1 in
tcp)
	PROTO='tcp'
	PROV_ARG='-tcp'
	APPLI='bulk_d'
	PROVIDER="tcp"
	;;
udp)
	PROTO='udp'
	# Set the application recv buffer to the maximum TSDU size.
	PROV_ARG='-udp -rbuf25000'
	APPLI='bulk_d'
	PROVIDER="udp"
	;;
rtt)
	PROTO='rtt'
	PROV_ARG='-rtt'
	APPLI='rtt_d'
	PROVIDER="tcp"
	;;
rttudp)
	PROTO='rtt'
	PROV_ARG='-rtt -udp'
	APPLI='rtt_d'
	PROVIDER="udp"
	;;
*)
	echo "ERROR: bad argument 1: $1"
	usage
	;;
esac

case $2 in
sock)
	API_ARG=''
	API='sock'
	;;
xti)
	API_ARG='-xti'
	RCV_VERSION_ARG="-$4"
	case $4 in
	V0)
		API='xti.t_snd_v0'
		;;
	V1)
		API='xti.t_snd_with_write'
		;;
	*)
		# For UDP or if we don't care...
		RCV_VERSION_ARG=''
		API='xti'
		;;
	esac
	;;
*)
	echo "ERROR: bad argument 2: $2"
	usage
	;;
esac

#
# Parse optional parameters.
#
for param in "$@"
do
	case $param in
	port)
		PORT_ARG='-p7788'
		;;
	nodelay)
		NODELAY_ARG='-nodelay'
		;;
	dtonly)
		DTONLY_ARG='-dtonly'
		;;
	push)
		PUSH_ARG='-push'
		;;
	rt)
		RT_ARG='-rt60'
		;;
	cpustat1)
		CPUSTAT_ARG='-cpustat1'
		;;
	cpustat3)
		CPUSTAT_ARG='-cpustat3'
		;;
	sync)
		SYNC_ARG='-sync'
		;;
	esac
done

# Calculate the number of CPUs...
# Use it only if you are on an AIX/4.1 system.
#
getcpunb

# ... and now test.
#
benchrun | tee $DIRRESULT/$APPLI.$PROVIDER.$DATE
