#!/bin/sh
#
# runnettest -p proto [-d dest] [-R remote] [-L local] 
#		[-r run tag] [-N nBytes] [-n nstreams]
#               [-B singlesendsize]
#
# Executes a nettest session both locally and remotely. Not all nettest
# arguments are # used by this script - it's intended to be a simple 
# performance neasurement tool.
# -p	Protocol. tcp, udp, unix (unix domain sockets), pipe, or file.
# -d	Destination (hostname, host alias, fully qualified domain name, or
#	IP address in dot format) to which the local host will send. 
# -R	Address of the remote host (hostname, host alias, fully qualified 
#	domain name, or IP address in dot format) which will send to the
#	local host. 
# -L 	Address of the local host (hostname, host alias, fully qualified 
#	domain name, or IP address in dot format) which the remote host
#	will send to.
# -r	Run tag. An integer or character string which will be used to label 
#	output files. A tag of "5" will generate local output file out.5 (both
#	sender's and receiver's output) for tests where the local host is 
#	the sender, and remout.5 for tests where the remote host is the 
#	sender. Defaults to 1.
# -N	Total number of bytes to send. Defaults to 8 Meg (8*1024*1024, or
#	8388608) in the 3.3-comparison case, to slightly more than 8 Meg 
#	(4*1460*1460, or 8526400) in the optimized Ethernet TCP case, and 
#	to slightly more than 80 meg (2000*4352, or 8704000) in the optimized
#       FDDI TCP case. Optimized cases send only MTU-multiples for send
#       sizes of 1 MTU or greater, so each packet will be MTU-sized.
# -n	Number of instances of nettest to run concurrently. For TCP tests,
#	this will be the number of connections to the receiving host. If not
#	specified, n defaults to one.
# -B    Buffer size to send when only a single run is desired. Defaults to the
#       uncommented standard series defined below by "lengths".
# 
# Examples:
# runnettest -p tcp -r 1 -d foo -n 3	: runs a TCP test with three concurrent
#					  connections to foo as the target
#					  host, and locally generates
#					  output files called t1 and r1.
#
# runnettest -p udp -r 4 -R foo -L doo	: runs a single UDP testfrom the remote
#					  host foo to the local host doo, and 
#					  locally generates output files called
#					  rt4 and rr4 .
#
# runnettest assumes that nettest and nettestd reside in /usr/etc on all hosts,
# and also assumes that any remote hosts have an open guest account. Should that
# not be the case (that is, if the remote hosts(s) are not SGI machines), edit 
# the rsh command lines to use appropriate remote logins and the correct remote 
# directory path to nettest and nettestd.
#
# Bidirectional tests may be run by specifying -d , -R , and -L .
# If -R is specified but -L is not, -L defaults to the system hostname
# (which may not be on the same network as the -R IP address).
#
# The number of cases to be run and the send size per case are specified
# in the defined character string "lengths" below. Choose the "lengths" and
# "nbytes" pair appropriate to your test. Edit lengths to add or remove
# send-size cases - customization is encouraged.
#
# The nettest daemon nettestd must be running on the target machine. It must be
# installed in /usr/etc. The nettest progrtam itself must also be installed in
# /usr/etc .

USAGE="$0 -p proto [-d dest] [-L laddr] [-R raddr] [-r run] [-N nBytes] [-n nstreams]"

# for comparison with 3.3 figures, uncomment the next two lines
#lengths="128 256 512 1024 1536 2048 3072 3584 4096 5120 5400 6144 7168 8192"
#nBytes=`expr 8 \* 1024 \* 1024`

# for Ethernet TCP optimum sends, uncomment the next two lines
lengths="128 256 512 1024 1460 2920 5840 7300 8760 11680 14600 17520 35040 43800 58400 61320"
nBytes=`expr 4 \* 1460 \* 1460`

# for FDDI MTU-sized TCP tests, uncomment the next two lines.
#lengths="128 256 512 1024 2048 4352 8704 13056 21760 30464 43520 52224 60928"
#nBytes=`expr 20000 \* 4352`

myname=`hostname`
run=1
number=1
while getopts "p:d:r:L:R:N:n:B:" c; do
    case $c in
    p) proto="$OPTARG";;
    d) dest="$OPTARG"; destination=yes;;
    r) run="$OPTARG";;
    R) hisname="$OPTARG"; remote=yes;;
    L) myname="$OPTARG";; 
    N) nBytes="$OPTARG";;
    n) number="$OPTARG";;
    B) lengths="$OPTARG";;
    \?) echo $USAGE; exit 1;;
    esac
done
shift `expr $OPTIND - 1`
if test "$#" != 0; then
    echo $USAGE
    exit 1
fi
echo "Starting $proto run $run"
if test "$destination" = "yes"; then
   op=out.
   output=$op$run
   cp /dev/null $output
fi
if test "$remote" = "yes"; then
   bop=remout.
   bout=$bop$run
   cp /dev/null $bout
fi
if test "$destination" = "yes"; then
   if rsh guest@$dest ps -ef | grep nettestd | grep $proto >/dev/null 2>&1; 
      then :
      else
         rsh guest@$dest -n /usr/etc/nettestd -p $proto &
   fi
fi
if test "$remote" = "yes"; then
   if ps -ef | grep nettestd | grep $proto >/dev/null 2>&1; 
      then :
      else
         /usr/etc/nettestd -p $proto &
   fi
fi

   sleep 2
for buflen in $lengths; do
   nBuf=`expr $nBytes / $buflen`
   echo "$buflen * $nBuf"
   if test "$remote" = "yes"; then
      echo "" >> $remout
      rsh guest@$hisname -n /usr/etc/nettest -p $proto -n $number $myname $nBuf $buflen  >> $remout &
   fi
   if test "$destination" = "yes"; then
      echo "" >> $output
      nettest -p $proto -n $number $dest $nBuf $buflen >> $output
   fi
   wait
   sleep 4
done
