Test Simple HTTP Loads With Apache Bench

Pete Freitag Expert Author Article Date: 2009-02-05

If you have access to a Mac or Linux server, chances are you may already have a really simple http load generating tool installed called Apache Bench, or ab. If you are on windows and have Apache installed, you may also have ab.exe in your apache/bin folder.

Suppose we want to see how fast Yahoo can handle 100 requests, with a maximum of 10 requests running concurrently:

ab -n 100 -c 10 http://www.yahoo.com/

It will then generate output as follows:
Concurrency Level: 10
Time taken for tests: 1.889 seconds
Complete requests: 100
Failed requests: 0
Write errors: 0
Total transferred: 1003100 bytes
HTML transferred: 949000 bytes
Requests per second: 52.94 [#/sec] (mean)
Time per request: 188.883 [ms] (mean)
Time per request: 18.888 [ms] (mean, across all concurrent requests)
Transfer rate: 518.62 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 57 59 1.7 59 64
Processing: 117 126 7.5 124 162
Waiting: 57 62 7.0 60 98
Total: 175 186 8.0 184 224
Percentage of the requests served within a certain time (ms)
50% 184
66% 186
75% 187
80% 188
90% 192
95% 203
98% 216
99% 224
100% 224 (longest request)

As you can see this is very useful information, it returned requests at a rate of 52.94 requests per second, the fastest request was 175ms, the slowest 224ms

So the next time you are tempted to whip out cfloop and GetTickCount to do some benchmarking on a piece of code, give ab a try, it's easy to use, and will yield much more realistic results.

Because ab supports concurrency, this has two big advantages over cfloop. The main one is that it allows you to test how your code runs concurrently, this can help you identify any possible race conditions, or locking issues. Concurrent requests are also a more natural simulation of load than loops.

Suppose you wanted to test multiple url's concurrently as well? You can do this by creating a shell script, with multiple ab calls. At the end of each line place an & this makes the command run in the background, and lets the next command start execution. You will also want to redirect the output to a file for each url using > filename For example:
ab -n 100 -c 10 > test1.txt &
ab -n 100 -c 10 > test2.txt &

The usage info from the ab version installed on my Mac (v2.3) is listed below. As you can see there are many useful options for outputting results, and sending additional data in the request.
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
    -b windowsize Size of TCP send/receive buffer, in bytes
    -p postfile File containing data to POST. Remember also to set -T
    -T content-type Content-type header for POSTing, eg.
        Default is 'text/plain'
    -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
    -r Don't exit on socket receive errors.
    -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)


About the Author:
Pete Freitag (http://www.petefreitag.com/) is a software engineer, and web developer located in central new york. Pete specializes in the HTTP protocol, web services, xml, java, and coldfusion. In 2003 Pete published the ColdFusion MX Developers Cookbook with SAMs Publishing.

Pete owns a Firm called Foundeo (http://foundeo.com/) that specializes in Web Consulting, and Products for Web Developers.

Test Simple HTTP Loads With Apache Bench