Skip to content

Instantly share code, notes, and snippets.

@paulsheldrake
Last active December 27, 2015 11:39
Show Gist options
  • Save paulsheldrake/7320538 to your computer and use it in GitHub Desktop.
Save paulsheldrake/7320538 to your computer and use it in GitHub Desktop.
This uses PhantomJS and YSlow for PhantomJS so get the min/max/average page load time for a specific url over a number of iterations. This script assumes you have PhantomJS installed and will download yslow if it's not in the correct place.
#!/bin/bash
# Example usage: sh perf_yslow.sh -l 3 -u http://example.com
while getopts "l:u:" opt; do
case $opt in
l)
LOOPS=$OPTARG
;;
u)
URL=$OPTARG
;;
\?)
echo "Invalid option: -$OPTARG" >&2
exit 1
;;
esac
done
# Check PhantomJS is installed
command -v phantomjs >/dev/null 2>&1 || { echo >&2 "Phantomjs is not installed. Aborting."; exit 1; }
# Check we have yslow
if [ ! -f yslow.js ]; then
echo "ySlow not found, downloading"
wget http://yslow.org/yslow-phantomjs-3.1.8.zip
unzip yslow-phantomjs-3.1.8.zip
fi
echo "###############################"
echo "# Testing: ${URL}"
echo "# Loops: ${LOOPS}"
echo "###############################"
rm -rf load_times.txt
# Loop through all the tables and export
for (( c=1; c<=LOOPS; c++ ))
do
echo "Running test ${c} of ${LOOPS}"
# run the test, get the page load line, get the time, trim whitespace and add to file
phantomjs yslow.js --info basic --format plain ${URL} |
grep 'page load time:' |
cut -d: -f 2 |
tr -d ' ' >> load_times.txt
done
# find the min / max / average
# from: http://unix.stackexchange.com/a/13738/30901
INFO=$(cat load_times.txt | awk 'NR == 1 { max=$1; min=$1; sum=0 }
{ if ($1>max) max=$1; if ($1<min) min=$1; sum+=$1;}
END {printf "Min: %d\tMax: %d\tAverage: %f\n", min, max, sum/NR}')
# add the median
INFO="${INFO}\tMedian: $(cat load_times.txt | sort -n | awk '{arr[NR]=$1}
END { if (NR%2==1) print arr[(NR+1)/2]; else print (arr[NR/2]+arr[NR/2+1])/2}')"
echo ${INFO}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment