Cache testing with curl and grep


Just wrote a quick shell script to test web server cache and background updating by displaying header output from curl.
Don’t have time to write a description here, just having the script for reference.

#!/bin/bash
#
# 2016-05-06 20:22:35
# myrveln@gmail.com
#
# Test cache functionality
#

usage(){
    echo "Usage: $0 [ http://domain.com ] [ sleeptimer ]"
    exit 1
}

# We need two parameters
if [[ $# -lt 2 ]]; then
    usage
    exit 1
# We want only full urls
elif [[ ! $1 == http*://* ]]; then
    usage
    exit 1
# We want only numeric sleeptimer
elif [[ -z "${2##*[!0-9]*}" ]]; then
    usage
    exit 1
fi

echo "TERMINATE THE SCRIPT WITH CTRL+C"
echo "----------------------"

while true
do
    START_TIME="$(date +%s%N)"
    OUTPUT=$(curl -o /dev/null -D - -s ${1} | grep 'Age:\|Warning:\|X-Cache\|Via:\|Expires:\|X-RateLimit-Remaining:' | sort)
    ELAPSED_TIME="$((($(date +%s%N) - ${START_TIME})/1000000))"
    echo "${OUTPUT}"
    echo "Elapsed time: ${ELAPSED_TIME} ms"
    echo "----------------------"
    sleep ${2}
done

Comments

comments