# bencht
#
#	Created on : 1994
#	Author	   : J.D. Sorace
#	Modified on: 14/04/95
#	Author	   : V. Roca
#
#	Client bencht tool.
#

###### Constants to set accordingly ######

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

# Only used on 3.2.5 systems, otherwise let bindprocessor estimate it
# (see getcpunb call at the end of file).
#CPU_NB=1

# NB: The first figure is the year. Set accordingly...
DATE=`date +"5%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 identifier to complete information returned by uname -s.
# Must be a single word!
#SYSTEM_VER='325'
SYSTEM_VER='412'

# Test duration. 30 seconds seems to be a good choice.
# This technique is better than the one consisting in
# limiting the number of messages!
DURATION=30

# Fast rendering: limit the number of samples.
LMSG_BIG='1 500 1000 2000 3000 4000 4100 5000 6000 7000 8100 8200 9000 10000 12200 12300 14000 16300 16400 18000 20000 24000'


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

usage()
{
	echo 'Usage: bencht [BSD|CCA|STD] [tcp|udp|int|intudp] [sock|xti] <connexion nb> <peer host>'
	echo '	additional optional arguments:'
	echo '		with the xti API: [v0|v1|v2|v3|v4|v5] [push]'
	echo '		with tcp: [nodelay]'
	echo '		in all cases: [rt] [dtonly] [cpustat1] [cpustat2] [sync]'
	exit 1
}


getcpunb()
{
	bindprocessor $$ 0 > /dev/null 2>&1
	if [ $? -eq 0 ]
	then
		CPU_NB=1
	fi
	bindprocessor $$ 1 > /dev/null 2>&1
	if [ $? -eq 0 ]
	then
		CPU_NB=`expr $CPU_NB + 1`
	fi
	bindprocessor $$ 2 > /dev/null 2>&1
	if [ $? -eq 0 ]
	then
		CPU_NB=`expr $CPU_NB + 1`
	fi
	bindprocessor $$ 3 > /dev/null 2>&1
	if [ $? -eq 0 ]
	then
		CPU_NB=`expr $CPU_NB + 1`
	fi
	bindprocessor -u $$
}


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 "\nTest $APPLI date: $DATE Host: $HOST PeerHost: $PEERHOST System: $SYSTEM $SYSTEM_VER Provider: $PROVIDER API: $API Fork: $CONN_NB CPU: $CPU_NB\n\n"
	echo "$MSG\n"
	echo '================================================================================'

	for lmsg in $LMSG
	do

		echo "====== lmsg=$lmsg ======"
		echo "./bench -$STACK $API_ARG $PROV_ARG -h$PEERHOST $SND_VERSION_ARG $NODELAY_ARG $DTONLY_ARG $PUSH_ARG $RT_ARG $CPUSTAT_ARG $SYNC_ARG $CONN_ARG -m120 -silent -l$lmsg -dur$DURATION"
		./bench -$STACK $API_ARG $PROV_ARG -h$PEERHOST $SND_VERSION_ARG $NODELAY_ARG $DTONLY_ARG $PUSH_ARG $RT_ARG $CPUSTAT_ARG $SYNC_ARG $CONN_ARG -m120 -silent -l$lmsg -dur$DURATION
		echo '====== end bench ======'

		# security: consolidate the trace file.
		sync
		sleep 1
	done
}


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


#
# Parameters parsing.
#

if [ $# -lt 5 ]
then
	usage
fi

# Define the TSDU size of interest. It depends on the network interface
# you are using and its MTU (Max. Transmission Unit), on the precision
# you want to have. Having measure points at MSS, MSS+1 will create nice
# curves with sharp teeths.
# Edit as appropriate...
#
case $1 in
BSD)
	# BSD stack: values of interest if MSS is 1460
	#	(1460 for Ethernet or loopback in 3.2.5)
	#	use of multiple mbufs in [n*4096 ; n*4096 + 936[, and cluster
	#	elsewhere.
	#
	# LMSG='1 10 50 100 200 300 463 464 500 699 700 800 935 936 1024 1200 1300 1400 1460 1461 1600 1800 2000 2500 2919 2920 2921 3000 3500 4000 4096 4100 4379 4380 4381 4500 4797 5000 5031 5032 5500 5839 5840 5841 6000 6500 7000 7299 7300 7301 8000 8192 8200 8500 8759 8760 8761 9127 9128 9500 10000'
	#
	# BSD stack: values of interest if MSS is 3460
	#
	# LMSG='1 10 50 100 200 300 400 500 600 800 1024 1500 2000 2500 3000 3300 3459 3460 3461 3500 4000 4096 4100 4500 5000 5500 6000 6500 6919 6920 6921 7000 8000 8192 8200 8500 9128 9500 10000 10379 10380 10381 11000 12000 12288 12289 13000 13839 13840 13841 14000 15000 16000 17000 18000 19000 20000 24000'
	#
	# BSD stack: values of interest if MTU is 4352
	#	(FDDI in 4.1.2)
	#
	# LMSG='1 10 64 100 200 300 400 500 600 800 930 940 1024 1500 2000 2500 3000 3500 4000 4096 4100 4296 4330 5000 5500 6000 6500 7000 7500 8000 8192 8200 8592 8650 9128 9500 10000 10500 11000 12000 12288 12300 12888 13000 14000 15000 16000 16300 16384 16400 17184 17300 18000 19000 20000 24000'
	#
	# BSD stack: values of interest if MSS is 8192
	#	(loopback in 4.1.2)
	#
	LMSG='1 10 50 100 200 300 400 500 600 800 1024 1500 2000 2500 3000 3500 4000 4096 4100 4500 5000 5500 6000 6500 7000 7500 8000 8191 8192 8200 8500 9128 9500 10000 10500 11000 12000 12288 12289 13000 14000 15000 16000 16383 16384 16385 16500 17000 18000 19000 20000 24000'

	#LMSG=$LMSG_BIG
	;;

CCA|STD)
	#
	# CCA and STD stacks: values of interest if MSS is 1460
	#	(Ethernet or loopback in 3.2.5)
	#
	# LMSG='1 10 50 100 200 300 400 500 600 800 1024 1200 1300 1400 1460 1461 1600 1800 2000 2500 2919 2920 2921 3000 3500 4000 4096 4100 4379 4380 4381 4500 5000 5500 5839 5840 5841 6000 6500 7000 7299 7300 7301 8000 8192 8200 8500 8759 8760 8761 9128 9500 10000 10219 10220 10221 11000 12000 12288 12289 13000 14000 15000 16000 17000 18000 19000 20000 24000'
	#
	# CCA and STD stacks: values of interest if MSS is 3460
	#
	# LMSG='1 10 50 100 200 300 400 500 600 800 1024 1500 2000 2500 3000 3300 3459 3460 3461 3500 4000 4096 4100 4500 5000 5500 6000 6500 6919 6920 6921 7000 8000 8192 8200 8500 9128 9500 10000 10379 10380 10381 11000 12000 12288 12289 13000 13839 13840 13841 14000 15000 16000 17000 18000 19000 20000 24000'
	#
	# CCA and STD stacks: values of interest if MTU is 4352
	#	(FDDI in 4.1.2)
	#
	# LMSG='1 10 64 100 200 300 400 500 600 800 930 940 1024 1500 2000 2500 3000 3500 4000 4096 4100 4296 4330 5000 5500 6000 6500 7000 7500 8000 8192 8200 8592 8650 9128 9500 10000 10500 11000 12000 12288 12300 12888 13000 14000 15000 16000 16300 16384 16400 17184 17300 18000 19000 20000 24000'
	#
	# CCA and STD stacks: values of interest if MSS is 8192
	#	(loopback in 4.1.2)
	#
	LMSG='1 10 50 100 200 300 400 500 600 800 1024 1500 2000 2500 3000 3500 4000 4096 4100 4500 5000 5500 6000 6500 7000 7500 8000 8191 8192 8200 8500 9128 9500 10000 10500 11000 12000 12288 12289 13000 14000 15000 16000 16383 16384 16385 16500 17000 18000 19000 20000 24000'

	#LMSG=$LMSG_BIG
	;;

*)
	usage
	;;
esac
STACK=$1

case $2 in
tcp)
	PROTO='tcp'
	PROV_ARG='-t'
	APPLI='bulk'
	PROVIDER="$STACK.tcp"
	;;
udp)
	PROTO='udp'
	PROV_ARG='-u'
	APPLI='bulk'
	PROVIDER="$STACK.udp"
	;;
int)
	PROTO='int'
	PROV_ARG='-i'
	APPLI='int'
	PROVIDER="$STACK.tcp"
	;;
intudp)
	PROTO='intudp'
	# Set the application recv buffer to the maximum TSDU size.
	PROV_ARG='-i -u -rbuf25000'
	APPLI='int'
	PROVIDER="$STACK.udp"
	;;
*)
	usage
	;;
esac

case $3 in
sock)
	API_ARG=''
	API='sock'
	;;
xti)
	API_ARG='-x'
	SND_VERSION_ARG="-$6"
	case $6 in
	v0)
		API='xti.t_snd_v0'
		;;
	v1)
		API='xti.t_snd_with_write'
		;;
	v2)
		API='xti.write_wo_t_snd'
		;;
	v3)
		API='xti.t_snd_wo_ctrl'
		;;
	v4)
		API='xti.t_sndseg'
		;;
	v5)
		API='xti.t_sndseg.copycksum'
		;;
	*)
		# For UDP or if we don't care...
		SND_VERSION_ARG=''
		API='xti'
		;;
	esac
	;;
*)
	usage
	;;
esac

#
# With UDP no connection argument is possible
#
CONN_NB=$4
if [ $4 -gt 1 ]
then
	CONN_ARG="-c$4 -d1"
else
	case $PROTO in
	tcp|int)
		CONN_ARG='-c1'
		;;
	udp|intudp)
		CONN_ARG=''
		;;
	esac
fi

PEERHOST=$5

#
# Parse optional parameters.
#
for param in "$@"
do
	case $param in
	nodelay)
		NODELAY_ARG='-nodelay'
		;;
	dtonly)
		DTONLY_ARG='-dtonly'
		;;
	push)
		PUSH_ARG='-push'
		;;
	rt)
		RT_ARG='-rt60'
		;;
	cpustat1)
		CPUSTAT_ARG='-cpustat1'
		;;
	cpustat2)
		CPUSTAT_ARG='-cpustat2'
		;;
	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.$CONN_NB.$DATE
