Skip to content

Instantly share code, notes, and snippets.

@errorstudio
Created October 31, 2011 10:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save errorstudio/1327259 to your computer and use it in GitHub Desktop.
Save errorstudio/1327259 to your computer and use it in GitHub Desktop.
HTTP stats - code, download time, number of assets and pageweight
#!/bin/bash
#A little shell script which uses wget to download
#all the requisites for a page, and returns the amount
#of time, the 'weight' of the page in K and the number
#of files, along with the HTTP code of the original request
#requires curl, bc, wget (version 1.13), awk and bash.
#expects the url (including http://) to be passed from STDIN
#Disclaimer - this was hacked together to scratch our itch
#there are plenty of better ways of achieving this!
#some config options
url=$1
dumpfolder="/tmp/http-stats"
mkdir -p $dumpfolder
tidy_url=`echo $url | cut -d "/" -f 3`
stats_file=$dumpfolder/"$tidy_url-stats"
#get the HTTP response code for the page
http_code=`curl -sL -w "%{http_code}" $url -o /dev/null`
if [ $http_code -ne 200 ]
then
#if it's not 200, don't do the rest
echo "code:$http_code files: size: time:"
exit 1
else
#get the other stats from wget
/usr/local/bin/wget -P $dumpfolder -t 1 -p -nv $url &> $stats_file
time_line=`grep "Total wall clock time" $stats_file `
stats_line=`tail -1 $stats_file `
download_time=`echo "$time_line" | cut -d ":" -f 2 | sed 's/ //g' | sed 's/s//g'`
num_files=`echo $stats_line | awk '{print $2}'`
size=`echo $stats_line | awk '{print $4}'`
size_unit=${size: -1}
size_value=${size:0:${#size}-1}
case "$size_unit" in
"B" )
file_size=`echo "$size_value / 1024" | bc`
;;
"K" )
file_size=$size_value
;;
"M" )
file_size=`echo "$size_value * 1024" | bc`
;;
"G" )
file_size=`echo "$size_value * 1024 * 1024" | bc`
;;
esac
echo "code:$http_code files:$num_files size:$file_size time:$download_time"
rm -rf $dumpfolder
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment