Skip to content

Instantly share code, notes, and snippets.

@phptek
Last active August 22, 2016 02:10
Show Gist options
  • Save phptek/7ae003d046ff7a02545c to your computer and use it in GitHub Desktop.
Save phptek/7ae003d046ff7a02545c to your computer and use it in GitHub Desktop.
siege-ci.sh
#!/bin/bash
#
# Simple Siege wrapper for use in CI.
# A good use of this script might be in a CI setup calling a custom URL that featured many DB queries..
# Outputs 'PASS' or 'FAIL' to stdout/stderror respectively, for given URL(s) depending on:
#
# 1). Script params
# 2). Baked-in "acceptable" performance params (See "Performance Parameters" below)
#
# Russell Michell 2015 <[email protected]>
#
# Note: Modify /usr/local/etc/seiegerc thus:
#
# show-logfile = false
#
# TODO:
# - Script occasionbally segfaults. Not sure why.
# - Allow configurable server-response time
#
# Performance Parameters
#
# Response time (s)
BV_B_RT=0.5
# Availability (%)
BV_B_AV=100
# Failed transactions
BV_B_FT=0
# Longest transaction (s)
BV_B_LT=10
#
# The script. You shouldn't need to edit anything below here
#
function usage {
cat << EOF
Usage: $0 <options>
Wrapper script around Siege suited for CI environments.
It will output 'PASS' or 'FAIL' to stdout/stderror, depending on passed and baked-in params.
OPTIONS:
-h Show this message
-c No. Concurrent users e.g. 10
-d Time Delay between requests in seconds e.g. 1
-t Time for test to run in seconds e.g. 60
-u Url to test e.g. http://localhost
-R Show only the Response time metric (No -R assumes run all metrics)
EOF
exit
}
SILENT=0
METRIC_RESPONSE=0
while getopts "hc:d:t:u:s:R" OPT; do
case $OPT in
c)
PARAM_CONCURRENT="$OPTARG"
MSG="${OPTARG} Concurrent users\n"
;;
d)
PARAM_DELAY="$OPTARG"
MSG="${MSG} Delay: ${OPTARG}s\n"
;;
t)
PARAM_TIMING="$OPTARG"
MSG="${MSG} Time/Duration: ${OPTARG}s\n"
;;
u)
PARAM_URL="$OPTARG"
MSG="${MSG} URL: ${OPTARG}\n\n"
;;
s)
SILENT=1
;;
R)
METRIC_RESPONSE=1
;;
h)
usage
exit 1
;;
\?)
usage
exit 1
;;
esac
done
if [[ $# -eq 0 ]]
then
usage
fi
# Useful if run from TeamCity etc - don't show "user prompts"..
#if [ $SILENT -eq 0 ]
#then
# #echo -e $MSG
#fi
SIEGE=$(which siege)
OUTFILE="./siege.out"
if [ "${#SIEGE}" -eq "0" ]
then
echo -f "Install Siege: http://www.joedog.org/siege-home/"
exit
fi
CMD="$SIEGE -q -c${PARAM_CONCURRENT} -d${PARAM_DELAY} -t${PARAM_TIMING}S ${PARAM_URL}"
# Truncate the output and re-create
> $OUTFILE
$CMD 2> $OUTFILE
# Run the command and parse output for one or more "bad" things:
RESPONSE=$( cat $OUTFILE | perl -p -e's/^.*Response time:\s+(.*)\ssecs.*$/_\1/' | grep '_' | sed 's/_//' )
AVAILABILITY=$( cat $OUTFILE | perl -p -e's/^.*Availability:\s+(.*)\s%.*$/_\1/' | grep '_' | sed 's/_//' )
FAILED_TRANSACTIONS=$( cat $OUTFILE | perl -p -e's/^.*Failed transactions:\s+([0-9]+)$/_\1/' | grep '_' | sed 's/_//' )
LONGEST_TRANSACTION=$( cat $OUTFILE | perl -p -e's/^.*Longest transaction:\s+(.*)/_\1/' | grep '_' | sed 's/_//' )
# Colour the output
RED=`tput setaf 1`
GRN=`tput setaf 2`
RST=`tput sgr0`
# Let's be positive by default..
STATUS="${GRN}PASS${RST}"
EXIT_CODE=0
# Response time too high (always run this. Only run the rest if $METRIC_RESPONSE=0)
if [ $( echo "$RESPONSE > $BV_B_RT" | bc ) -gt 0 ]; then
STATUS="${RED}FAIL${RST} (Bad response time: Allowed: ${BV_B_RT}s Actual: ${RESPONSE}s)"
EXIT_CODE=1
else
if [ $METRIC_RESPONSE -eq 0 ]; then
# Less than acceptable URL availability
if [ $( echo "scale=0;($AVAILABILITY)/1 < $BV_B_AV" | bc ) -gt 0 ]; then
STATUS="${RED}FAIL${RST} (Bad availability: Allowed: ${BV_B_AV}% Actual: ${AVAILABILITY}%)"
EXIT_CODE=1
# Greater than acceptable failed requests
elif [ $FAILED_TRANSACTIONS -gt $BV_B_FT ]; then
STATUS="${RED}FAIL${RST} (No. failed requests too high: Allowed: ${BV_B_FT} Actual: ${FAILED_TRANSACTIONS})"
EXIT_CODE=1
# Longest request took too long
elif [ $( echo "$LONGEST_TRANSACTION > $BV_B_LT" | bc ) -gt 0 ]; then
STATUS="${RED}FAIL${RST} (Longest request took too long. Allowed: ${BV_B_LT}s Actual: ${LONGEST_TRANSACTION}s)"
EXIT_CODE=1
fi
fi
fi
if [ $EXIT_CODE -eq 1 ]; then
>&2 echo "${STATUS} Av response: ${RESPONSE}s"
else
echo "${STATUS} Av response: ${RESPONSE}s"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment