Created
April 17, 2016 16:52
-
-
Save marklkelly/ea151de020809b04ab1d21226b127eb4 to your computer and use it in GitHub Desktop.
For testing max age of cached resources. bash age-test.sh <http://your-resource/> <age in milliseconds> <optional: HTTP host name>
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
shopt -s extglob # Required to trim whitespace | |
RESOURCE=$1 | |
AGE=$2 | |
HOST=$3 | |
GREEN=$(tput setaf 2) | |
RED=$(tput setaf 1) | |
WHITE=$(tput setaf 7) | |
BOLD=$(tput bold) | |
NORMAL=$(tput sgr0) | |
RESET=$(tput sgr0) | |
currentage=0 | |
previousage=0 | |
if [[ $# -eq 0 || -z "$AGE" || -z "$RESOURCE" ]] | |
then | |
echo | |
echo -e "${RED}Incorrect arguments supplied. Please supply a url, an age, and optionally, a host header entry. e.g: agetest.sh http://www.mydomain.com 300 www.mytest.com${WHITE}" | |
echo | |
exit | |
fi | |
trap ctrl_c INT | |
function ctrl_c() { | |
echo -e "\033[5B\033[2K" | |
echo "Exiting." | |
echo -e $RESET | |
exit | |
} | |
command="curl -Is $RESOURCE" | |
if [ -n "$HOST" ]; then | |
command="$command -H 'Host: $HOST'" | |
fi | |
echo | |
echo | |
echo -e "${GREEN}Resource: $RESOURCE" | |
echo "Target age: $AGE" | |
if [ -z "$HOST" ]; then | |
echo 'Host: No Host Header supplied' | |
else | |
echo "Host: ${HOST}" | |
fi | |
echo "Command: $command" | |
echo | |
echo -e "${WHITE}Testing:" | |
while [ "$currentage" -le "$AGE" ]; do | |
#clear | |
eval response=\$\($command\) | |
httpstatus=$(echo "$response" | grep HTTP/| awk '{$1=""; print $0}') | |
httpstatus=${httpstatus##+([[:space:]])}; httpstatus=${httpstatus%%+([[:space:]])} | |
currentage=$(echo "$response" | grep Age | awk {'print $2'}) | |
currentage=${currentage##+([[:space:]])}; currentage=${currentage%%+([[:space:]])} | |
cachestatus=$(echo "$response" | grep X-Cache | awk '{$1=""; print $0}') | |
cachestatus=${cachestatus##+([[:space:]])}; cachestatus=${cachestatus%%+([[:space:]])} | |
if [[ $cachestatus != "Hit from cloudfront" && "$previousage" -gt 0 ]] | |
then | |
printf "\033[5B" | |
if [[ "$previousage" -eq "$AGE" || "$previousage" -eq "$AGE-1" ]] | |
then | |
echo -e "${GREEN}" | |
else | |
echo -e "${RED}" | |
fi | |
echo -e "Something other than a Cache Hit was detected. Response was: ${BOLD}$cachestatus${NORMAL}" | |
exit | |
fi | |
if [ -z "$currentage" ] | |
then | |
currentage=0 | |
fi | |
echo -e "${NORMAL}HTTP Status: ${BOLD}$httpstatus " | |
echo -e "${NORMAL}Age: ${BOLD}$currentage " | |
echo -e "${NORMAL}Cache status (X-Cache): ${BOLD}$cachestatus " | |
if [ $currentage -gt $AGE ] | |
then | |
echo | |
echo -e "${RED}Age breached. Target age: $AGE Reported age: $currentage${WHITE}" | |
exit | |
fi | |
#Move up four lines | |
echo -e "\033[4A" | |
sleep 1 | |
previousage=$currentage | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment