Skip to content

Instantly share code, notes, and snippets.

@damiencuvillier
Created November 14, 2023 21:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save damiencuvillier/a28b7f2592b2f1cd436685ee580bb852 to your computer and use it in GitHub Desktop.
Save damiencuvillier/a28b7f2592b2f1cd436685ee580bb852 to your computer and use it in GitHub Desktop.
Bash script for testing http call performance
#!/bin/bash
# HTTP call response time measurement
#
# Author: Damien Cuvillier <damien.cuvillier@rts.ch>
# Since: 2023-11
# License: MIT
#
RED='\033[0;31m'
REDLIGHT='\033[1;31m'
BLUE='\033[1;34m'
GREEN='\033[0;32m'
GREENLIGHT='\033[1;32m'
NC='\033[0m'
# Parameters management
if [[ $# == 0 || $1 == "--help" || $1 == "-h" ]] ; then
echo
echo "HTTP call response time measurement. "
echo " Make several identical calls and give average time (in ms)"
echo
echo "Syntax: "
echo " http-perf <url> [<json-file>] [-t|--tries try] [-s|--silent] [-n|--numeric] [-v|--verbose]"
echo
echo " If json-file is given, call HTTP url with POST with given file"
echo " "
echo " -t | --tries <num> How many times should I call given HTTP. Default: 10"
echo " -i | --interval How many second(s) should I wait between each calls. Default: 0"
echo " -s | --silent No details & progress. Give only results."
echo " -n | --numeric Only output ms raw value"
echo " -v | --verbose Give details on output"
echo " -h | --help Display this message"
echo
exit 0
fi
url=$1
shift
if [[ -f "$1" ]] ; then
jsonFile="$1"
shift
fi
silent=false
numericOnly=false
tries=10
verbose=false
interval=0
while [[ $# > 0 ]] ; do
case $1 in
"-t"|"--tries")
if [[ $# -ge 2 ]] ; then
tries=$2
shift
fi
;;
"-i"|"--interval")
if [[ $# -ge 2 ]] ; then
interval=$2
shift
fi
;;
"-s"|"--silent")
silent=true
;;
"-n"|"--numeric")
numericOnly=true
;;
"-v"|"--verbose")
verbose=true
;;
*)
echo "Unknown parameter $1" >&2
exit 1
esac
shift
done
# Measurement
function ms(){
t=$(ruby -e 'puts "%.3f" % Time.now')
echo "$t * 1000" | bc
}
TIMEFORMAT=%R
total=0
success=0
if ! $verbose ; then
tput sc
else
silent=false
numericOnly=false
fi
if [[ $silent -eq "false" && $numericOnly -eq "false" ]] ; then
echo -n "Executing $tries HTTP calls"
fi
for ((i=1; i<= $tries; i++)) ; do
start=$(ms)
if [ $jsonFile == "" ] ; then
httpCode=$(curl -i -s "$url"|head -n 1|awk '{print $2}')
else
httpCode=$(curl -i -s -X POST -H 'Content-Type: application/json' -d "@$jsonFile" "$url"|head -n 1|awk '{print $2}')
fi
commandCode=$(echo $?)
if [[ $commandCode > 0 ]] ; then
echo "Unable to call $1 with curl [$commandCode]">&2
exit $commandCode
elif [[ $httpCode -ne "200" ]] ; then
echo -n -e "\n${REDLIGHT}Error: ${RED}$httpCode${NC} " >&2
else
delay=$(echo "`ms` - $start"| bc)
total=$(echo "$total + $delay"| bc)
success=$(echo "$success + 1" |bc)
if $verbose ; then
if $numericOnly ; then
echo $delay
else
echo -n -e "\n[$i] "$delay"ms"
fi
else
echo -n "."
fi
fi
sleep $interval
done
# Display results
if [[ $success -gt 0 ]] ; then
avg=$(echo "$total / $success"|bc)
if ! $verbose ; then
tput rc
tput ed
fi
if [[ $silent == "true" || $numericOnly == "true" ]] ; then
if $numericOnly ; then
echo $avg
else
echo $avg"ms"
fi
else
if $verbose ; then
echo
fi
echo -e "${BLUE}$url${NC} average load time: ${GREENLIGHT}$avg${GREEN}ms${NC} ($success successfull tries) \033[0m"
fi
else
if [[ ! $verbose ]] ; then
tput rc
tput ed
else
if $verbose ; then
echo
fi
echo -e "${RED}No successfull call${NC} on ${BLUE}$url${NC}" >&2
fi
exit 404
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment