Use Apache Benchmark for Easy Load Testing
By Pete Freitag
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 Benchmarking tool, or ab
, and even sometimes just called Apache Bench. If you are on windows and have Apache installed, you may also have ab.exe
in your apache/bin
folder.
Apache Benchmark Simple Example
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 for
loop and a time stamp 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 Apache Benchmark, or ab
supports concurrency, this has two big advantages over simple looping or iteration tests. 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.
Testing Multiple URLS Concurrently
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:
#!/bin/sh ab -n 100 -c 10 http://127.0.0.1:8300/test.cfm > test1.txt & ab -n 100 -c 10 http://127.0.0.1:8300/scribble.cfm > test2.txt &
Apache Benchmark Options
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
Option | Description |
---|---|
-n requests | The total number of requests to make |
-c concurrency | Number of requests to make at the same time, or concurrently. |
-t timelimit | Time limit in seconds, the maximum amount of time the benchmark can take |
-s timeout | Time in seconds to wait for each request, the default is 30 seconds. |
-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. 'application/x-www-form-urlencoded' Default is 'text/plain' |
-v verbosity | Prints additional or verbose information. |
-w | Outputs the results in HTML tables |
-i | Use HEAD instead of GET |
-m method | The HTTP method to use for the requests. |
-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. JSESSIONID=1234 . You can repeat this option as many times as you need to. |
-H attribute | Add a HTTP request header line, eg. 'Accept-Encoding: gzip'
Inserted after all normal header lines. You can repeat this argument as many times as you need to. |
-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 | Enable the HTTP KeepAlive feature |
-d | Omit the percentiles served table. |
-S | Omit confidence estimators and warnings. |
-g filename | Output collected data to gnuplot format file. |
-e filename | Output the results as a CSV file with percentages served |
-r | Don't exit on socket receive errors. |
-h | Display help or usage information |
-Z ciphersuite | Specify SSL/TLS cipher suite (See openssl ciphers) |
-f protocol | Specify SSL/TLS protocol (SSL2, SSL3, TLS1, TLS1.1, TLS1.2 or ALL). Depending on the version of the tool and the version of openssl you have the choices may differ. |
-E certfile | Specify an optional client certificate chain and private key if necessary to connect to your server. |
As you can see the apache benchmark tool is pretty powerful, it doesn't have quite as many options as curl, but it can do a lot.
Where is the ab
located on a Mac?
Currently you can find the ab
binary at the path: /usr/sbin/ab
SSL Not compiled in; no https support
If you see the error message:
SSL Not compiled in; no https support
If you are on Windows try using the abs.exe
instead of the ab.exe
executable for https support on Apache Bench. I believe you may also see that error message on some older versions of the Mac binary as well. I can confirm as of at least Mac OSX Monterey (12.6) the ab binary does include support for https by default.
Use Apache Benchmark for Easy Load Testing was first published on February 05, 2009.
If you like reading about ab, apache, load, testing, benchmarking, http, or concurrency then you might also like:
Weekly Security Advisories Email
Advisory Week is a new weekly email containing security advisories published by major software vendors (Adobe, Apple, Microsoft, etc).