Skip to content

Siege

Siege Basics

Siege is a tool that is used to make requests to a site. It is commonly used for load testing

Advanced Usage

Siege can use a file with URLs in it to test.

This can read basic variables and carry out POST requests. I have not found a way of getting dynamic araibles varaibles to be read, so I put together the following script to generate them

#!/usr/bin/env bash
readonly DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )";
cd $DIR;
set -e
set -u
set -o pipefail
standardIFS="$IFS"
IFS=$'\n\t'
echo "
===========================================
$(hostname) $0 $@
===========================================
"

# The number of requests to make this can be changed by passing in a number as an agrument to the script
numberToTest=${1:-10}
# The base URL for the instance to test
baseUrl=http://www.example.com
# request path to test
requestPath=/rest/default/my/custom/api
# The product ID we are going to buy - make sure this exists and has plenty of stock before starting
productId=306
# The type of order id that is being used, either int64 or uuid
orderType=uuid
# The urls file we are going to write
siegeFile=/tmp/urls.txt
counter=0;

# These are variables that will be used in every request
echo "
BASE_URL=${baseUrl}
REQUEST_PATH=${requestPath}
PRODUCT_ID=${productId}
" > ${siegeFile}

echo "
Generating urls.txt file
"

while [ ${numberToTest} -gt ${counter} ]
do
    # These are the per request variables that we are going to use
    counter=$((counter+1));
    customerId=`uuidgen`
    if [[ ${orderType} == 'int64' ]]
    then
        orderId=`shuf -i 1-100000 -n 1`
    else
        orderId=`uuidgen | tr -d -`
    fi
    echo "
EMAIL_${counter}=customer${counter}@example.com
ID_${counter}=${customerId}
ORDER_ID_${counter}=${orderId}
\${BASE_URL}\${REQUEST_PATH} POST {\"order_id\":\"\${ORDER_ID_${counter}}\",\"phone_number\":\"0123456789\",\"partner_id\":12,\"products\":[{\"qty\":2,\"id\":\"\${PRODUCT_ID}\",\"configurable_options\":[],\"custom_options\":[]}],\"customer_details\":{\"email_address\":\"\${EMAIL_${counter}}\",\"customer_identifier\":\"\${ID_${counter}}\",\"first_name\":\"John\",\"last_name\":\"Smith\"}}
" >> ${siegeFile}
done

echo "
Done:

Running siege

siege -c ${numberToTest} -r1 -v -f ${siegeFile} --content-type \"application/json\"
"
siege -c ${numberToTest} -r1 -v -f ${siegeFile} --content-type "application/json"
echo "
----------------
$(hostname) $0 completed
----------------
"