ab
, Apache Bench, is often used to load test websites, but I didn’t find it extensible enough for my needs.
Unless an unofficial patch is applied, ab
cannot simulate a real person by going to random URLs over a specified period of time. This is where siege comes in.
Install siege
siege
should be available in most popular package management systems and is an apt-get install
, yum install
, or brew install
away.
Once siege
is installed, get familiar with its features by running siege --help
or by reading the man page.
Load Testing Your Website
Gather all the URLs an enduser might go to on your website and put those URLs into a text file.
For example, create a file called urls.txt with the following content:
http://example.com/this-is-example-one.html
http://example.com/this-is-example-two.html
http://example.com/this-is-example-three.html
http://example.com/this-is-example-four.html
http://example.com/this-is-example-five.html
http://example.com/this-is-example-six.html
With the text file created containing all your URLs, it’s time to run siege
.
The following command will create 50 concurrent connections (scale up the concurrent connections if needed), the --internet
command line switch will attempt to simulate a real person for each of those connections by randomly going to URLs in the text file you created above, and the text file is specified with the --file
command line switch.
siege --concurrent=50 --internet --file urls.txt
You can stop the command at any time and it will output a report. Alternatively, specify a time to run the test with the --time
command line switch.
Load Testing Your API
siege
can also be used to load test your API.
Instead of gathering a bunch of URLsĀ and having siege
randomly access them, you will likely be hitting a single URL endpoint and POSTing data to it.
The following command will create 50 concurrent connections and POST plain text data - plain-text-content-here - to the URL specified:
siege --concurrent=50 --content-type="text/plain" 'http://example.com/api POST plain-text-content-here'
If your API accepts JSON instead of plain text, use the following command instead:
siege --concurrent=50 --content-type="application/json" 'http://example.com/api POST {"key1":"value1","key2":"value2"}'
Additionally, you can POST JSON data from a file:
siege --concurrent=50 --content-type="application/json" 'http://example.com/api POST < /path/to/json-file.txt'
Once again, you can stop the command at any time and it will output a report. Alternatively, specify a time to run the test with the --time
command line switch.