I was working on a project today and wanted to be able to get the average latency for an API that I was working on. Performance is a concern because we’re running the API over a VPN, and then SSH tunneling over to another server. I wanted a quick way to do it and wrote a little bash function that will calculate the average for me. I couldn’t find an example on how to do this online so I’m sharing in case anyone else runs into the same issue.

This is tested on Mac only. Add these two functions to your .bashrc and do a shellupdate in terminal to load the latest, or just grab my dotfiles from my github: https://github.com/gregvarghese/dotfiles

curlb(){
  curl -s -o /dev/null -w '%{time_starttransfer}\n' "$@"
}

# Usage:
# latencyavg [# of times to run] [URL]
function latencyavg()
{
  time=0.00
  for (( c=1; c<=$1; c++ ))
  do
     num1=$(curlb $2 -H 'Accept-Encoding: gzip, deflate, sdch' -H 'Accept-Language: en-US,en;q=0.8,ja;q=0.6' -H 'Upgrade-Insecure-Requests: 1' -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.86 Safari/537.36' -H 'Connection: keep-alive' --compressed)
     echo "$c - $num1"
     time=$(echo "$time + $num1" | bc -l)
  done
  echo "Total Time $time"
  echo $(echo $time / $1 | bc -l)
}

Usage example:

latencyavg 20 https://www.godaddy.com

How to Fix ‘Converter Failed to Save File’ with Excel 2016
View Comments
There are currently no comments.