#!/bin/sh
#
# benchdt
#
#	Created on : 14/04/95
#	Author	   : V. Roca
#
#	Server benchdt 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'


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

usage()
{
	echo 'Usage: benchdt [BSD|CCA|STD] [tcp|udp|int|intudp] [sock|xti]'
	echo '	additional optional arguments:'
	echo '		with the xti API: [V0|V1] [push]'
	echo '		with tcp: [nodelay]'
	echo '		in all cases: [rt] [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: * System: $SYSTEM $SYSTEM_VER Provider: $PROVIDER API: $API Fork: * CPU: $CPU_NB\n\n"
	echo "$MSG\n"
	echo '================================================================================'

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

	echo "./benchd -$STACK $API_ARG $PROV_ARG $RCV_VERSION_ARG $NODELAY_ARG $DTONLY_ARG $PUSH_ARG $RT_ARG $CPUSTAT_ARG $SYNC_ARG -m120 -silent"
	./benchd -$STACK $API_ARG $PROV_ARG $RCV_VERSION_ARG $NODELAY_ARG $DTONLY_ARG $PUSH_ARG $RT_ARG $CPUSTAT_ARG $SYNC_ARG -m120 -silent
}


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


#
# Parameters parsing.
#

if [ $# -lt 3 ]
then
	usage
fi

case $1 in
BSD|CCA|STD)
	STACK=$1
	;;
*)
	usage
	;;
esac

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

case $3 in
sock)
	API_ARG=''
	API='sock'
	;;
xti)
	API_ARG='-x'
	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
	;;
*)
	usage
	;;
esac

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