#!/bin/bash
#-------------------------- =+- Shell script -+= --------------------------
#
# @file      test_netperf.sh
# @date      Thu Apr  6 17:14:45 2006
# @brief
#
# CVS version control block - do not edit manually
#  $RCSfile: test_netperf.sh,v $
#  $Source: /home/cvs/yoh/progr/misc/netperf/test_netperf.sh,v $
#
# Created: Fri Apr  7 02:45:26 2006
#  Commited: $Date: 2006/04/21 15:46:45 $
#  Revision: $Revision: 1.5 $
#
#  Yaroslav Halchenko                                      CS@UNM, CS@NJIT
#  web:     http://www.onerussian.com                      & PSYCH@RUTGERS
#  e-mail:  yoh@onerussian.com                              ICQ#: 60653192
#
# DESCRIPTION (NOTES):
#  I got surprised that I could not find any script online which would
#  plot some nice graphs from netperf reports, while changing some network
#  setup parameter (MTU for instance). So this brute dirty hack came into
#  existance. Now I think that I should have started to work on it using
#  python...
#  For now variables are hardcoded... so tune it up to your needs. I would
#  appreciate if you have any feedback/patches
#  This tool should be used in conjunction with plot_test.py script which
#  allows to plot results quite easily
# COPYRIGHT: Yaroslav Halchenko 2006
#
# LICENSE:
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the
#  Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
#  MA 02110-1301, USA.
#
# On Debian system see /usr/share/common-licenses/GPL for the full license.
#
#-----------------\____________________________________/------------------

hosts=( 192.168.0.3 192.168.0.4 )
hosts_legal=( node3 node4 )
interface=eth1

NAME="crossover/"

param_files=( "/sys/class/net/@@I/mtu" )
params=(1024 128 9000)

dosimplex=1
doduplex=1
runs=1
cmd="netperf -P 0 -c -C -n 2 -f K -l 45 -H @@H"

while getopts "h:r:H:c:i:n:f:p:P:SD" opt
do
  case "$opt" in
      h) hosts=($OPTARG);;
	  r) runs="$OPTARG";;
      H) hosts_legal=($OPTARG);;
      c) cmd="$OPTARG";;
      i) interface="$OPTARG";;
      n) NAME="$OPTARG";;
      f) param_files="$OPTARG";;
      p) params=($OPTARG) ;;
      P) PRECMD="$OPTARG" ;;	# if we want to specify stupid command for debugging like echo
      S) dosimplex= ;;
      D) doduplex= ;;
#    *) usage; exit 2 ;;
  esac
done



param_files=( ${param_files//@@I/$interface} )
param=${params[0]}

#PRECMD=echo
while true
do
  # set PARAM
  for host_ in ${hosts_legal[*]}; do
	  for param_file in ${param_files[*]}; do
		  $PRECMD rsh $host_ "echo $param >| $param_file"
	  done
  done

  # wait to make sure everyone is up now with new MTU or with whatever
  # we changed
  for hostnum in 0 1; do
	  dhost=${hosts_legal[$hostnum]}
	  $PRECMD rsh $dhost \
		  ping -q -w 100 -c 1 ${hosts[$((1-$hostnum))]} >/dev/null
	  realparam[$hostnum]=`rsh ${dhost} cat ${param_files[0]}`
  done

  lineprefix="$param"

  for currentrun in `seq 1 $runs`; do

	  if [ ! -z $dosimplex ]; then
		  # lets do simplex/one-way runs
		  for hostnum in 0 1; do
			  dhost=${hosts_legal[$hostnum]}
			  targethost=${hosts[$((1-$hostnum))]}
			  res=(`$PRECMD rsh $dhost ${cmd//@@H/$targethost}`)
			  echo -e "$lineprefix $dhost ${realparam[$hostnum]} ${NAME}S.$hostnum ${res[*]}"
		  done
	  fi
	
	  # just for the sake of it
	  sleep 1
	
	  if [ ! -z $doduplex ]
		  then
		  # lets do nasty duplex
		  # run cmds first
	      for hostnum in 0 1; do
			  dhost=${hosts_legal[$hostnum]}
			  targethost=${hosts[$((1-$hostnum))]}
			  resfile[$hostnum]=/tmp/`basename $0`.res$hostnum.dat
			  $PRECMD rsh $dhost ${cmd//@@H/$targethost} >| ${resfile[$hostnum]} &
		  done
		  # wait for to complete
		  wait
		  # fetch the results
		  restotal=(0)
		  for hostnum in 0 1; do
			  res=(`cat ${resfile[$hostnum]}`)
			  restotal=`echo "x=[${restotal[*]}]+[${res[*]}]; printf('%.2f ',x)" | octave -q`
			  rm -rf ${resfile[$hostnum]}
			  echo -e "$lineprefix ${hosts_legal[$hostnum]} ${realparam[$hostnum]} ${NAME}D.$hostnum ${res[*]}"
		  done
		  # print sum
		  echo -e "$lineprefix ${hosts_legal[$hostnum]} ${realparam[$hostnum]} ${NAME}D.+ ${restotal[*]}"
		  # go to the next PARAM
	  fi
  done
  # stupid bash with no doubles...
  [ $param -ge ${params[2]} ] && break
  param=$(($param + ${params[1]}))
  # do for the tail anyways if we get above.
  # 9000-1024 doesn't divide evenly and didn't want another octave call
  # to manipulate doubles
  [ $param -ge ${params[2]} ] && param=${params[2]}
done

