#!/bin/sh
#  Copyright 2021 Hewlett Packard Enterprise Development LP
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
#
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
# DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
# OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
# USE OR OTHER DEALINGS IN THE SOFTWARE.


#
# This is an example script for using netperf. Feel free to modify it
# as necessary, but I would suggest that you copy this one first.
# This script performs various UDP unidirectional stream tests.
#

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

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

# where the programs are

#NETHOME=/usr/local/netperf
#NETHOME="/opt/netperf"
NETHOME="."

# 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 PORT apropriately
#PORT="-p some_other_portnum"
PORT=""

# The test length in seconds
TEST_TIME=60

# How accurate we want the estimate of performance:
#      maximum and minimum test iterations (-i)
#      confidence level (99 or 95) and interval (percent)

STATS_STUFF="-i 10,2 -I 99,10"

# The socket sizes that we will be testing. This should be a list of
# integers separated by spaces

SOCKET_SIZES="32768"

# The send sizes that we will be using. Using send sizes that result
# in UDP packets which are larger than link size can be a bad thing to do.
# for FDDI, you can tack-on a 4096 data point

SEND_SIZES="64 1024 1472"

# 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
  LOC_CPU=""
  REM_CPU=""
  REM_HOST=$1
fi

# 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=`$NETHOME/netperf $PORT -t LOC_CPU`;;
*) LOC_RATE=""
esac

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

# This will tell netperf that headers are not to be displayed
NO_HDR="-P 0"

for SOCKET_SIZE in $SOCKET_SIZES
do
  for SEND_SIZE in $SEND_SIZES
  do
    echo
    echo ------------------------------------------------------
    echo Testing with the following command line:
    echo $NETHOME/netperf $PORT -l $TEST_TIME -H $REM_HOST $STATS_STUFF \
           $LOC_CPU $LOC_RATE $REM_CPU $REM_RATE -t UDP_STREAM -- \
           -m $SEND_SIZE -s $SOCKET_SIZE -S $SOCKET_SIZE

    $NETHOME/netperf $PORT -l $TEST_TIME -H $REM_HOST $STATS_STUFF \
      $LOC_CPU $LOC_RATE $REM_CPU $REM_RATE -t UDP_STREAM -- \
      -m $SEND_SIZE -s $SOCKET_SIZE -S $SOCKET_SIZE

  done
done
echo
