Simple script to test network throughput (performance)

This script is very similar to the ab wrapper script to test website performance except it uses iperf to test just the network throughput, it's only value add is running a bunch of tests in a row, parsing out just the requests per second and caclulating an average across all runs. Simple script for a simple benchmark.

Example command line:

ak@loon:~$ iperf.sh 10 lark

Output:

==================================================================
 Results
==================================================================
 target host ... lark
------------------------------------------------------------------
 run 1: 	 937 Mbits/sec
 run 2: 	 937 Mbits/sec
 run 3: 	 937 Mbits/sec
 run 4: 	 937 Mbits/sec
 run 5: 	 937 Mbits/sec
 run 6: 	 936 Mbits/sec
 run 7: 	 936 Mbits/sec
 run 8: 	 937 Mbits/sec
 run 9: 	 937 Mbits/sec
 run 10: 	 937 Mbits/sec
------------------------------------------------------------------
 average ....... 936.8 Mbits/sec
 
see iperf.lark.log for details

Here's the script:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/bin/bash
 
if ! [ -x "$(type -P iperf)" ]; then
  echo "ERROR: script requires iperf"
  echo "For Debian and friends get it with 'apt-get install iperf'"
  echo "If you have it, perhaps you don't have permissions to run it, try 'sudo $(basename $0)'"
  exit 1
fi
 
if [ "$#" -ne "2" ]; then
  echo "ERROR: script needs four arguments, where:"
  echo
  echo "1. Number of times to repeat test (e.g. 10)"
  echo "2. Host running 'iperf -s' (e.g. somehost)"
  echo
  echo "Example:"
  echo "  $(basename $0) 10 somehost"
  echo 
  echo "The above will run 'iperf -c' 10 times on the client and report totals and average."
  exit 1
else
  runs=$1
  host=$2
fi
 
log=iperf.$host.log
 
if [ -f $log ]; then
  echo removing $log
  rm $log
fi
 
echo "=================================================================="
echo " Results"
echo "=================================================================="
echo " target host .... $host"
echo "------------------------------------------------------------------"
 
for run in $(seq 1 $runs); do
  iperf -c $host -f m >> $log
  echo -e " run $run: \t $(awk '/Bandwidth/ {getline}; END{print $7, $8}' $log)"
done
 
avg=$(awk -v runs=$runs '/Bandwidth/ {getline; sum+=$7; avg=sum/runs} END {print avg}' $log)
 
 
echo "------------------------------------------------------------------"
echo " average ....... $avg Mbits/sec"
echo
echo "see $log for details"

3 Comments

  • 1. Luciano replies at 2nd August 2013, 5:04 am :

    Hello, I have launched your script and it works perfectly. What I have run from ~/scripts/iperf/iperf.sh
    The problem I have is that when creating a runtime from crontab does not generate the log file.
    I am beginner in Linux, and would appreciate if you can tell me how to solve it.
    Regards

  • 2. Alain Kelder replies at 2nd August 2013, 9:40 am :

    @Luciano

    Make sure the user that runs the command via cron has permissions to write to the directory where script executes from, since log goes to the same directory by default. I’d actually place the log elsewhere, by changing log=iperf.$host.log to log=/path/to/other/dir/iperf.$host.log

    For instance (assuming script runs as user “luciano”):

    sudo mkdir /var/log/iperf/
    chmod 775 /var/log/iperf/
    chown luciano:luciano /var/log/iperf/

    Then modify iperf.sh, set new log location:

    log=/var/log/iperf/iperf.$host.log

    You could also redirect cron output to a log (though you should probably modify the script so it writes time stamps):

    00 4  * * *  /path/to/iperf.sh >> /var/log/iperf/iperf.cron.log 2>&1
  • 3. Jon replies at 14th October 2013, 2:28 pm :

    I was running the script and modded it too…

    for run in $(seq 1 $runs); do
    iperf -c $host -P 20 >> $log
    echo -e ” run $run: \t $(awk ‘/Bandwidth/ {getline}; END{print $6, $7}’ $log)”
    done

    … It appears to correctly report back the overall SUM values of the tests per run but when it calculates the average it gives me some very low numbers and since i am testing 40 GbE i would expect averages around 39.6 Gbytes/s versus 2.32 Gbytes/s

    I think this line is what i need to fix but i am not skilled enough in scripting to figure it out ….

    avg=$(awk -v runs=$runs ‘/Bandwidth/ {getline; sum+=$7; avg=sum/runs} END {print avg}’ $lug)

Leave a comment

NOTE: Enclose quotes in <blockquote></blockquote>. Enclose code in <pre lang="LANG"></pre> (where LANG is one of these).