Apache’s AB tool is a great way to really put stress on a server. I have compiled a list of the parameters I use to test my sites and applications.
First we have to make sure AB can hit the desired page and get the desired results. The most basic page is the homepage of a site, use the command below to send as many single requests as it can in 1 second
ab http://example.com/
But that is great and dandy but what happens if we want to try to stress a page that hits the database and has to do some actual dynamic work
ab http://example.com/search.php?q=Trees
Last but not least what if the site is behind a login that is an http form and not an htaccess form? That is where POST variables come into play, it is a little trickier but doable. Here is the command I use to hit a page behind a login.
ab -p ~/Documents/post.txt -T application/x-www-form-urlencoded -e http://example.com/manager/
Here is what my post.txt file contains:
email=test@domain.com&password=testing&submit=Login
Next we need to get these results to a file so we can work with them and have a record of progress as we tweak our site. It can be done with the “-e” parameter. An example is below:
ab http://example.com/search.php?q=Trees -e ~/Documents/results1.csv
What that does is hits the search page and puts the results in the results1.csv file. It is useful to put the file at the end of the command since you will want to change the filename on each run to keep a history of your results.
The csv file is separated into two columns. The first is the percentage complete and the second is the time in ms that it took to get to that percentage.
The first thing you want to look at is the zero percent. This is your latency, it is the amount of time it takes to fulfill the first request. You want this number to be as low as possible. I am running my site on a development server on my local network so the times will be lower but at the end I will show the results from the testing server.
ab -p ~/Documents/aci_post.txt -T application/x-www-form-urlencoded -e ~/Documents/aci_output.csv -kc 10 -t 10 http://aci/manager/makes.php
Hold tight, more coming soon…
Hi…thanks for this excellent article on ab. However I am getting this error –
Cannot open CSV output file: Invalid argument
for the following ab command –
ab -n 10 -c 2 -e ‘c:\ab.csv’ http
://192.168.1.12/search/video_search?search_text=sydney
Hey Nick, great post- it’s really helping me out. Would love to ask you a couple of questions. Please email me if you get a chance
From your example, ab http://example.com/search.php?q=Trees
Can I use a POST file here w/o q=Trees? It would be easy if you can post search.php file.
$ ab -p ./post.txt http://example.com/search.php
ab -p ~/Documents/post.txt -T application/x-www-form-urlencoded -e http://example.com/manager/
Can we use postfile with PHP type URL, like this?
$ ab -p ~/Documents/post.txt -T application/x-www-form-urlencoded http://mdd/insert.php
Also can you post http://example.com/search.php to this article?
Thanks for the comments everyone. I will dig up the search.php file and post it. You should be able to use postfile just like this:
$ ab -p ~/Documents/post.txt -T application/x-www-form-urlencoded http://mdd/insert.php
Is that not working @Ray? Is there a way you can in your insert.php write to a static file for output to verify the _POST is coming through correctly?