Skip to content

Instantly share code, notes, and snippets.

@paul-ridgway
Created December 13, 2024 08:21
Show Gist options
  • Save paul-ridgway/3ce8dfdbe341aa160606c32e675016ee to your computer and use it in GitHub Desktop.
Save paul-ridgway/3ce8dfdbe341aa160606c32e675016ee to your computer and use it in GitHub Desktop.
"Ping" a website and measure the response times
#!/bin/bash
# Adapted from https://gist.githubusercontent.com/weeyin83/c697468c5986cb3385990fa1c4c2d72b/raw/b8eed6a3259122bce442f2db5c9030f982948a88/websiteresponse.sh
if [ -z "$1" ]; then
echo "Usage: $0 <URL>"
exit 1
fi
last_run=0;
while true; do
last_run=$(date +%s);
response=$(curl -w 'Return code: %{http_code}; Bytes Received: %{size_download}; Response Time: %{time_total}' "$1" -m 2 -o /dev/null -s)
return_code=$(echo "$response" | awk -F'; ' '{print $1}' | awk -F': ' '{print $2}')
bytes_received=$(echo "$response" | awk -F'; ' '{print $2}' | awk -F': ' '{print $2}')
response_time=$(echo "$response" | awk -F'; ' '{print $3}' | awk -F': ' '{print $2}')
echo -n -e "\033[0;97m"
echo -n "$(date --iso-8601=seconds) :: ";
if (( $(echo "$response_time > 1" | bc -l) )); then
echo -n -e "\033[0;91m"
elif (( $(echo "$response_time > 0.5" | bc -l) )); then
echo -n -e "\033[0;93m"
else
echo -n -e "\033[0;92m"
fi
echo -n "Return code: $return_code; Bytes Received: $bytes_received; Response Time: $response_time"
echo -e "\033[0m"
while [ $(date +%s) -lt $((last_run + 1)) ]; do
sleep 0.1;
done
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment