speedtest.sh

Blog Post: http://oioiiooixiii.blogspot.ie/2016/03/bash-script-speedtestsh-graphical.html
Download: speedtest.sh

#!/bin/bash
################################################################################
#
#  Simple wget internet speed test, with Zenity-powered graphical speedometer
#   - Takes connection speed (in Mb/s) as optional argument
#
#  Ver. 2.2 - 17th March 2016
#   - fixed divide-by-zero error when ispSpeed > 100
#   - fixed scaling issue (harcoded divide by 10) with average speed result 
#
#  Source: http://oioiiooixiii.blogspot.com
#
################################################################################
#set -x -e

### VARIABLES ##################################################################

scale=100 # Zenity percentage bar limit (100% is unlikely to ever change)
ispSpeed="${1:-10}" # What ISP claims the connection is (default: 10Mb/s)
multiplier="$(bc -l <<< $scale/$ispSpeed)" # Used for scaling calculations
pingTarget="heanet.ie" # URL of domain to ping
timesLog="times.text" # Temporary file to store speed data
downloadFile="http://speedtest.wdc01.softlayer.com/downloads/test10.zip"
               #File to test download speeds with - NB1

### FUNCTIONS ##################################################################

function cleanUp() 
{ 
   rm "$timesLog" 
}
trap cleanUp EXIT

function calculateAverage()
{
   # "paste -sd+ -" NB2
   printf %.2f "$(bc -l <<<"($(sed '/^#.*/d' < "$timesLog" | paste -sd+ -))/\
                              $(sed '/^#.*/d' < "$timesLog" \
                              | wc -l)/$multiplier")"
}

function runTests() # back-end
{
   (  
      echo "# Running ping test on: http://$pingTarget"
      pingTime="$(ping -c 3 heanet.ie \
         | grep "rtt min/avg/max/mdev" \
         | awk -F'/' '{printf "%d\n", $5}')"
      
      wget \
         "$downloadFile" \
         --report-speed=bits \
         -O /dev/null \
         2>&1 \
      | stdbuf -o0 \
         awk  \
         -v speed="$multiplier" \
         -v ping="$pingTime" \
         '{ 
            if(index($8, "M") != 0)  
               print $8*speed;
            else if(index($8, "K") != 0) 
               print $8/1000*speed;
         }
         {print "# Ping: "ping"ms - Current speed: "$8"bps"; }' \
      | tee -a "$timesLog"

      result="# Completed Test in $SECONDS seconds\nAverage ping: ${pingTime}ms"
      echo "${result}\nAverage speed: $(calculateAverage)Mbps"
   ) 
}

function showDialog() # front-end
{
   zenity --progress \
      --title="zenity+wget Speed Test" \
      --width=375 
}

### BEGIN ######################################################################

runTests | showDialog
exit

### NOTES ######################################################################

#NB1: Some other download-test file locations:
#     http://speedtest.reliableservers.com/
#     http://www.thinkbroadband.com/download.html
#     http://mirror.internode.on.net/pub/test/
#NB2: http://stackoverflow.com/questions/926069/add-up-a-column-of-numbers-at-the-unix-shell