1
0
mirror of https://github.com/jaygooby/ttfb.sh synced 2025-12-10 16:07:22 +01:00

Sort the array so min & max are correct (doh!)

Also use a median function to correctly get median values
This commit is contained in:
Jay Caines-Gooby
2019-04-10 11:57:59 +01:00
parent 58209f3838
commit 30d3d1305a

28
ttfb
View File

@@ -49,6 +49,25 @@
# #
# Uses a dirty eval to do the ttfb arithmetic. Depends # Uses a dirty eval to do the ttfb arithmetic. Depends
# on bc and column commands. # on bc and column commands.
set -eu
# Cribbed from https://stackoverflow.com/a/41762669/391826
median() {
arr=($(printf '%s\n' "${@}" | sort -n))
nel=${#arr[@]}
if (( $nel % 2 == 1 )); then # Odd number of elements
val="${arr[ $(($nel/2)) ]}"
else # Even number of elements
(( j=nel/2 ))
(( k=j-1 ))
# (( val=(${arr[j]} + ${arr[k]})/2 ))
val=$(echo "scale=6;(${arr[j]}" + "${arr[k]})"/2|bc -l)
fi
echo $val
}
DEBUG=""
LOG=""
while getopts ":n:dl:" OPTION while getopts ":n:dl:" OPTION
do do
@@ -103,15 +122,22 @@ for URL in $URLS; do
# output it on the results line # output it on the results line
if [ ${#@} -gt 1 ]; then if [ ${#@} -gt 1 ]; then
SHOW_URL="${URL}|" SHOW_URL="${URL}|"
else
SHOW_URL=""
fi fi
# if multiple requests have been specified, then show min, max & median values # if multiple requests have been specified, then show min, max & median values
if [[ -n "$NUM_REQUESTS" && "$NUM_REQUESTS" -gt 1 ]]; then if [[ -n "$NUM_REQUESTS" && "$NUM_REQUESTS" -gt 1 ]]; then
times=() times=()
for i in $(seq $NUM_REQUESTS); do for i in $(seq $NUM_REQUESTS); do
printf "." >&2
times+=($(eval $(curl "${options[@]}" "$URL") | grep -oE "TTFB: .{0,7}" | cut -d' ' -f2 | sort -n)); times+=($(eval $(curl "${options[@]}" "$URL") | grep -oE "TTFB: .{0,7}" | cut -d' ' -f2 | sort -n));
done done
printf "$SHOW_URL\e[32mmin \e[39m${times[0]} \e[91mmax \e[39m${times[${#times[*]}-1]} \e[95mmedian \e[39m${times[${#times[*]}/2]}\e[39m\n"; printf "\n" >&2
# sort the times
times=( $( printf "%s\n" "${times[@]}" | sort -n ) )
# show quickest, slowest and median fftb
printf "${SHOW_URL}\e[32mfastest \e[39m${times[0]} \e[91mslowest \e[39m${times[${#times[*]}-1]} \e[95mmedian \e[39m$(median ${times[*]})\e[39m\n";
else else
echo -e $SHOW_URL $(eval $(curl "${options[@]}" "$URL")) echo -e $SHOW_URL $(eval $(curl "${options[@]}" "$URL"))
fi fi