Last active
February 18, 2020 16:18
-
-
Save dhavalgshah/bb94836ec36dc11135d1dce39e0bfb8e to your computer and use it in GitHub Desktop.
Test time to first byte for specific URL
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
#!/usr/bin/env sh | |
# bash ttfb.sh url [num_of_tests] | |
# bash ttfb.sh https://example.org 11 | |
if [[ -z $@ ]] | |
then | |
echo "Pass url and number of test runs to execute. By default 10 tests are executed." | |
echo "bash ttfb.sh url [num_of_tests]" | |
echo "E.g: $ bash ttfb.sh https://example.org 10" | |
exit 2 | |
fi | |
if [[ -z $1 ]] | |
then | |
echo "Need an URL to proceed!" | |
exit 2 | |
fi | |
url=$1 | |
tries=10 | |
if [[ ! -z $2 ]] | |
then | |
tries=$2 | |
fi | |
echo "Testing TTFB $tries times for $url" | |
# Execute the run | |
for (( i=0; i<$tries; i++)); | |
do | |
res[i]=$(curl -so /dev/null -w "%{time_total}\n" -H "Pragma: no-cache" $url); | |
done; | |
# Sort the results numerically and print them. | |
IFS=$'\n' sorted=($(sort <<<"${res[*]}")) | |
echo -e "\nSorted Results:\n${sorted[*]}" | |
unset IFS | |
min=${sorted[0]} | |
max=${sorted[$tries-1]} | |
if [[ $tries%2 -eq 1 ]] | |
then | |
median=${sorted[$tries/2]} | |
else | |
# sorted has zero based index so it ranges from 0 to $tries-1 | |
P1=${sorted[($tries/2)-1]} | |
P2=${sorted[$tries/2]} | |
median=$(echo "$P1 $P2" | awk '{print ($1 + $2)/2}') | |
fi | |
printf "\nMin: %f\tMax: %f\tMedian: %f\n" $min $max $median; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment