#!/bin/sh
#
# This script runs a series of netperf tests intended to gather the 
# raw material necessary to arrive at estimates for the cost of
# sending and receiving a TCP segment, the cost of each additional byte
# and the cost of each incremental segment.
# 
# there are a number of data points gathered by this script - it might
# run for a considerable length of time.
#
# rick jones 4/99
#
# teach it about processor affinity and the TCP_MSS test
# rick jones 2007-11-08
#

if [ $# -gt 2 ]; then
  echo "try again, correctly -> packet_byte_script hostname [CPU]"
  exit 1
fi

if [ $# -eq 0 ]; then
  echo "try again, correctly -> packet_byte_script hostname [CPU]"
  exit 1
fi

# where is netperf
NETPERF_DIR=${NETPERF_DIR:=/opt/netperf2/bin}


# at what port will netserver be waiting? If you decide to run
# netserver at a differnet port than the default of 12865, then set
# the value of NETPERF_PORT apropriately
# NETPERF_PORT="-p some_other_portnum"
NETPERF_PORT=${NETPERF_PORT:=""}


# The test length in seconds
NETPERF_TIME=${NETPERF_TIME:=30}

# How accurate we want the estimate of performance: 
#      maximum and minimum test iterations (-i)
#      confidence level (99 or 95) and interval (percent)
NETPERF_STATS=${NETPERF_STATS:="-i 30,3 -I 99,5"}

# The socket sizes that we will be testing - using -1 will let it 
# be the system default.
NETPERF_SKTS=${NETPERF_SKTS:="-1"}

# The CPU affinity to be applied
NETPERF_AFFINITY=${NETPERF_AFFINITY:=""}

# NETPERF_CMD is an amalgam of previous variables
NETPERF_CMD="${NETPERF_DIR}/netperf ${NETPERF_AFFINITY}"

# if there are two parms, parm one it the hostname and parm two will
# be a CPU indicator. actually, anything as a second parm will cause
# the CPU to be measured, but we will "advertise" it should be "CPU"

if [ $# -eq 2 ]; then
  REM_HOST=$1
  LOC_CPU="-c"
  REM_CPU="-C"
fi

if [ $# -eq 1 ]; then
  REM_HOST=$1
fi

MSS=`$NETPERF_CMD -H $REM_HOST -t TCP_MSS -P 0 -v 0`

# The request,response sizes that we will be using. The netperf
# command parser will treat "1" the same as "1,1" - I use 1,1 to
# remember that it is "request,response"

# start at one and multiply by two on our way to the MSS
bar=1
while [ $bar -lt $MSS ]
do
 NETPERF_REQS="${NETPERF_REQS} $bar"
 bar=`expr $bar \* 2`
done

# and now multiples of the mss and that plus one
for i in 1 2 3
do
 bar=`expr $MSS \* $i`
 NETPERF_REQS="${NETPERF_REQS} $bar"
 NETPERF_REQS="${NETPERF_REQS} `expr $bar + 1`"
done

bar=1
while [ $bar -lt $MSS ]
do
  NETPERF_RESP="${NETPERF_RESP} $bar"
  bar=`expr $bar \* 2`
done

for i in 1 2 3
do
 bar=`expr $MSS \* $i`
 NETPERF_RESP="${NETPERF_RESP} $bar"
 NETPERF_RESP="${NETPERF_RESP} `expr $bar + 1`"
done



# If we are measuring CPU utilization, then we can save beaucoup
# time by saving the results of the CPU calibration and passing
# them in during the real tests. So, we execute the new CPU "tests"
# of netperf and put the values into shell vars.
case $LOC_CPU in
\-c) LOC_RATE=`$NETPERF_CMD $PORT -t LOC_CPU`;;
*) LOC_RATE=""
esac

case $REM_CPU in
\-C) REM_RATE=`$NETPERF_CMD $PORT -t REM_CPU -H $REM_HOST`;;
*) REM_RATE=""
esac

# This disables header display
NO_HDR="-P 0"
NO_HDR=""

for SOCKET_SIZE in $NETPERF_SKTS
 do
  echo
  echo ------------------------------------------------------
  echo Testing with the following command line:
  # we echo the command line for cut and paste to th database
  echo $NETPERF_CMD $NETPERF_PORT -l $NETPERF_TIME -H $REM_HOST -t TCP_RR \
       $LOC_CPU $LOC_RATE $REM_CPU $REM_RATE $NETPERF_STATS --\
       -s $SOCKET_SIZE -S $SOCKET_SIZE
  echo
  echo and these settings for send sizes $NETPERF_REQS
  echo

  for REQ in $NETPERF_REQS
  do
   # since we have the confidence interval stuff, we do not
   # need to repeat a test multiple times from the shell
   $NETPERF_CMD $NETPERF_PORT -l $NETPERF_TIME -H $REM_HOST $NO_HDR \
   -t TCP_RR $LOC_CPU $LOC_RATE $REM_CPU $REM_RATE $NETPERF_STATS --\
   -r ${REQ},1 -s $SOCKET_SIZE -S $SOCKET_SIZE
   NO_HDR="-P 0"
  done
  echo
  echo ------------------------------------------------------
  NO_HDR=""
  echo Testing with the following command line:
  # we echo the command line for cut and paste to th database
  echo $NETPERF_CMD $NETPERF_PORT -l $NETPERF_TIME -H $REM_HOST -t TCP_RR \
       $LOC_CPU $LOC_RATE $REM_CPU $REM_RATE $NETPERF_STATS --\
       -s $SOCKET_SIZE -S $SOCKET_SIZE
  echo and these settings for response sizes $NETPERF_RESP
  echo
  for RESP in $NETPERF_RESP
   do
   # since we have the confidence interval stuff, we do not
   # need to repeat a test multiple times from the shell
   $NETPERF_CMD $PORT -l $NETPERF_TIME -H $REM_HOST $NO_HDR \
   -t TCP_RR $LOC_CPU $LOC_RATE $REM_CPU $REM_RATE $NETPERF_STATS --\
   -r 1,${RESP} -s $SOCKET_SIZE -S $SOCKET_SIZE
   NO_HDR="-P 0"
 done
  echo
  echo ------------------------------------------------------
  NO_HDR=""
  echo Testing with the following command line:
  # we echo the command line for cut and paste to th database
  echo $NETPERF_CMD $NETPERF_PORT -l $NETPERF_TIME -H $REM_HOST -t TCP_STREAM\
       $LOC_CPU $LOC_RATE $REM_CPU $REM_RATE $NETPERF_STATS --\
       -s $SOCKET_SIZE -S $SOCKET_SIZE
  echo and these settings for response sizes $NETPERF_RESP
  echo
  for REQ in $NETPERF_REQS
   do
   # since we have the confidence interval stuff, we do not
   # need to repeat a test multiple times from the shell
   $NETPERF_CMD $PORT -l $NETPERF_TIME -H $REM_HOST $NO_HDR \
   -t TCP_STREAM $LOC_CPU $LOC_RATE $REM_CPU $REM_RATE $NETPERF_STATS --\
   -m ${REQ} -s $SOCKET_SIZE -S $SOCKET_SIZE -D
   NO_HDR="-P 0"
 done
done

# The test length in seconds for the CRR test, which needs to be
#    longer for a connect/request/response test

NETPERF_CRR_TIME=${NETPERF_CRR_TIME:=120}

# now we do the TCP_CRR test
echo
echo ------------------------------------------------------
echo $NETPERF_CMD $NETPERF_PORT -l $NETPERF_CRR_TIME -H $REM_HOST -t TCP_CRR\
       $LOC_CPU $LOC_RATE $REM_CPU $REM_RATE $NETPERF_STATS --\
       -s $SOCKET_SIZE -S $SOCKET_SIZE
echo
$NETPERF_CMD $NETPERF_PORT -l $NETPERF_CRR_TIME -H $REM_HOST -t TCP_CRR\
       $LOC_CPU $LOC_RATE $REM_CPU $REM_RATE $NETPERF_STATS --\
       -s $SOCKET_SIZE -S $SOCKET_SIZE
