#!/bin/sh
#
# bencht
#
#	Modified by : V.Roca
#	Date	    : dec 1997
#
#	Client bencht 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=''

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!

# 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_FAST='2 50 100 500 1000 1500 2000 2500 3000 4000 4100 5000 6000 7000 8100 8200 9000 10000 12200 12300 14000 16300 16400 18000 20000 24000 28000 32000 40000 48000 56000 64000'


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

usage()
{
	echo 'Usage: bencht [tcp|udp|rtt|rttudp] [sock|xti] <connexion nb> <peer host>'
	echo '	(in that order !)'
	echo '	additional optional arguments:'
	#echo '		with the xti API: [v0|v1|v2|v3|v4|v5] [push]'
	echo '		with tcp: [nodelay]'
	echo '		in all cases: [dtonly] [cpustat1] [cpustat3] [sync] [port]'
	exit 1
}


getcpunb()
{
	# Method depends 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: $PEERHOST System: $SYSTEM $SYSTEM_VER Provider: $PROVIDER API: $API Fork: $CONN_NB CPU: $CPU_NB"
	echo "$MSG"
	echo '================================================================================'

	for lmsg in $LMSG
	do

		echo "====== lmsg=$lmsg ======"
		echo "bench${BENCH_EXT} $API_ARG $PROV_ARG -h$PEERHOST $PORT_ARG $SND_VERSION_ARG $NODELAY_ARG $DTONLY_ARG $PUSH_ARG $RT_ARG $CPUSTAT_ARG $SYNC_ARG $CONN_ARG -m120 -silent -l$lmsg -dur$DURATION"
		bench${BENCH_EXT} $API_ARG $PROV_ARG -h$PEERHOST $PORT_ARG $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 4 ]
then
	echo "ERROR: too few arguments"
	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...
#

# values of interest if MSS is 1460
#	(1460 for Ethernet)
#	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'
#
# 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'
#
# values of interest if MTU is 4352
#	(FDDI)
#
# 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'
#
# values of interest if MSS is 8192
#	(loopback)
#
# 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'
#
# values of interest if MTU is 9180
#	(CLIP ATM)
#
LMSG='2 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 8192 8500 9139 9140 9141 9500 10000 10500 11000 12000 13000 14000 15000 16000 16500 17000 18000 18279 18280 18281 18500 19000 20000 24000 28000 32000 36000 40000 44000 48000 52000 56000 60000 64000 65535'

#LMSG=$LMSG_FAST


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

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

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

PEERHOST=$4

#
# 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.$CONN_NB.$DATE
