Sometimes you just want to check the performance of a website or asset over a short period of time to get a general idea if it is performing normally. The bash script below simplifies that by calling curl with performance counter output formatting, wait and recursion. Tested on CentOS7 (and Ubuntu 14.0.4).
yum install curl -y
vi curl-site-speed.sh
# copy paste the following into curl-site-speed.sh
uri=$1
count=$2
wait=$3
defaultcount=10
defaultwait=3
bold=$(tput bold)
normal=$(tput sgr0)
if [ -z "$count" ]; then
count=$defaultcount
fi
if [ -z "$wait" ]; then
wait=$defaultwait
fi
echo "Testing $uri with curl for $count times with $wait seconds wait";
read -r -d '' CURLFORMAT << EOM
${bold}%{time_total}${normal} = time_total
%{time_namelookup}:time_namelookup
%{time_connect}:time_connect
%{time_appconnect}:time_appconnect
%{time_pretransfer}:time_pretransfer
%{time_redirect}:time_redirect
%{time_starttransfer}:time_starttransfer
---
EOM
for i in `seq 1 $count`; do
curl -k -w "$CURLFORMAT" -o /dev/null -s "$uri"
echo "Sleeping $wait"
sleep $wait
done
chmod +x curl-site-speed.sh
./curl-site-speed.sh $uri ($repeat=10) ($wait=3)
./curl-site-speed.sh https://www.http365.co.uk/1MB.txt
./curl-site-speed.sh https://www.http365.co.uk/1MB.txt 5
./curl-site-speed.sh https://www.http365.co.uk/1MB.txt 100 1</pre>
Output Example:
# ./curl-site-speed.sh https://www.http365.co.uk/1MB.txt 5 1
Testing https://www.http365.co.uk/1MB.txt with curl for 5 times with 1 seconds wait
0.113 = time_total
0.023:time_namelookup
0.023:time_connect
0.108:time_appconnect
0.108:time_pretransfer
0.000:time_redirect
0.109:time_starttransfer
---Sleeping 1
0.084 = time_total
0.002:time_namelookup
0.003:time_connect
0.079:time_appconnect
0.079:time_pretransfer
0.000:time_redirect
0.081:time_starttransfer
---Sleeping 1
0.085 = time_total
0.010:time_namelookup
0.010:time_connect
0.080:time_appconnect
0.080:time_pretransfer
0.000:time_redirect
0.082:time_starttransfer
---Sleeping 1
0.086 = time_total
0.011:time_namelookup
0.011:time_connect
0.081:time_appconnect
0.081:time_pretransfer
0.000:time_redirect
0.082:time_starttransfer
---Sleeping 1
0.120 = time_total
0.024:time_namelookup
0.024:time_connect
0.113:time_appconnect
0.113:time_pretransfer
0.000:time_redirect
0.116:time_starttransfer
---Sleeping 1
References
- http://stackoverflow.com/questions/18215389/how-do-i-measure-request-and-response-times-at-once-using-curl
- https://blog.josephscott.org/2011/10/14/timing-details-with-curl/
- http://stackoverflow.com/questions/23929235/multi-line-string-with-extra-space-preserved-indentation
- http://unix.stackexchange.com/questions/32290/pass-command-line-arguments-to-bash-script
- http://unix.stackexchange.com/a/60752
- http://www.cyberciti.biz/faq/linux-unix-sleep-bash-scripting/
- http://stackoverflow.com/questions/2924697/how-does-one-output-bold-text-in-bash
- http://www.unix.com/shell-programming-and-scripting/26389-creating-file-1mb-using-shell-command.html