///Load testing your web application with Apache Bench

Load testing your web application with Apache Bench

Often you need solid metrics on the performance of your web application. Benchmarking your website can provide insight into which sections of your application might need major optimizations, and helps in dealing with scalability issues. Apache Bench, or ‘ab’, is a command line load testing utility which ships with Apache, and allows you to simulate load on a web server. In this tutorial, I’ll show you how to get started with ab.

If you have an Apache installation locally, you’ve probably got ab already. Check your path – just run `ab`. Otherwise, head to your apache/bin folder. If you’re running Windows and don’t have Apache, grab a copy of XAMPP Lite from the project page.

The ab executable is independent of Apache, and doesn’t actually care if it’s testing an Apache installation – ab will load test Apache web servers, LigHTTPD servers, anything that speaks HTTP. Still, ab will give the most accurate results when run without latency on a local server, and you probably don’t want to load-test on a production server. Given this, it’s best to setup Apache and your application locally as a staging server, then load test it with ab.

Work out the path to your ab executable – for example, mine’s D:\dev\apache\bin\ab.exe – and open up a command line window to run it.

ab: wrong number of arguments
Usage: ab [options] [http[s]://]hostname[:port]/path
Options are:
    -n requests     Number of requests to perform
    -c concurrency  Number of multiple requests to make
    -t timelimit    Seconds to max. wait for responses
    -p postfile     File containing data to POST
    -T content-type Content-type header for POSTing
    -v verbosity    How much troubleshooting info to print
    -w              Print out results in HTML tables
    -i              Use HEAD instead of GET
    -x attributes   String to insert as table attributes
    -y attributes   String to insert as tr attributes
    -z attributes   String to insert as td or th attributes
    -C attribute    Add cookie, eg. 'Apache=1234. (repeatable)
    -H attribute    Add Arbitrary header line, eg. 'Accept-Encoding: gzip'
                    Inserted after all normal header lines. (repeatable)
    -A attribute    Add Basic WWW Authentication, the attributes
                    are a colon separated username and password.
    -P attribute    Add Basic Proxy Authentication, the attributes
                    are a colon separated username and password.
    -X proxy:port   Proxyserver and port number to use
    -V              Print version number and exit
    -k              Use HTTP KeepAlive feature
    -d              Do not show percentiles served table.
    -S              Do not show confidence estimators and warnings.
    -g filename     Output collected data to gnuplot format file.
    -e filename     Output CSV file with percentages served
    -h              Display usage information (this message)
    -Z ciphersuite  Specify SSL/TLS cipher suite (See openssl ciphers)
    -f protocol     Specify SSL/TLS protocol (SSL2, SSL3, TLS1, or ALL)

Calling ab without arguments will display this list of options. We are primarily interested in -n and -c. The -n switch represents the number of requests to perform – a figure in the low thousands is generally appropriate for a web application. The -c switch represents concurrency, or how many requests to make at the same time. This value can vary depending on what you expect for your application.

Work out a URL to an average page of your web application. For example, I’m load testing a local CakePHP application, so my URL is http://cake.local/users/list. Call ab as follows:

ab -n 1000 -c 10 http://cake.local/users/list

If you need to use a proxy (e.g. for testing remote servers), use the -X switch, in the format -X proxyhost:proxyport e.g. -X proxy.local:3128.

After displaying some status reports every 100 requests or so, it will print out a fairly detailed report, including document length, bytes transferred, completed and failed requests, requests per second and some statistics on connection times.

For further information, check out the documentation page for ab.

2010-05-19T21:56:46+00:00 July 3rd, 2008|PHP|1 Comment

About the Author:

One Comment

  1. Eugene Lillard January 31, 2010 at 9:15 AM

    Thanks for the excellent article, I love reading it!

Leave A Comment