ab (Apache Bench)
Load-test an HTTP server — request count, concurrency, keep-alive and how to read the report
ab (ApacheBench, package apache2-utils) sends a given number of HTTP requests to a single URL, several at a time, and prints a report: how many requests per second the server handled, how many failed and how response times were distributed. Good for a quick "before and after" on one URL; it can't do multi-page scenarios.
Syntax
ab [options] http://host[:port]/path
The path is required: http://127.0.0.1:8080 without a trailing / gives an invalid URL error.
Options
Volume and concurrency
| Option | What it does |
|---|---|
-n N | total number of requests (default 1) |
-c N | how many requests to keep in flight at once (default 1); can't exceed -n |
-t sec | run for at most this many seconds; implies -n 50000 |
-k | keep-alive: reuse connections instead of a new TCP connection per request |
-s sec | how long to wait for each response (default 30) |
-r | don't abort the test on socket receive errors |
Request
| Option | What it does |
|---|---|
-m METHOD | custom method |
-i | HEAD instead of GET |
-p file | POST with the body from a file; set -T along with it |
-u file | PUT with the body from a file |
-T type | Content-Type for -p / -u (default text/plain) |
-H 'Name: value' | add a header; can be repeated |
-C 'name=value' | add a cookie |
-A user:password | Basic authentication |
-X proxy:port | go through a proxy |
Report
| Option | What it does |
|---|---|
-q | don't print Completed 100 requests progress |
-l | don't count varying response lengths as errors (for dynamic pages) |
-d | don't print the percentile table |
-S | don't print standard deviations |
-e file.csv | percentiles as CSV |
-g file.tsv | all measurements in gnuplot format |
-v N | debug verbosity: 2 and up prints headers |
Reading the report
Concurrency Level: 10
Time taken for tests: 0.018 seconds
Complete requests: 200
Failed requests: 0
Non-2xx responses: 20
Keep-Alive requests: 100
Requests per second: 10897.40 [#/sec] (mean)
Time per request: 0.918 [ms] (mean)
Time per request: 0.092 [ms] (mean, across all concurrent requests)
Transfer rate: 2596.65 [Kbytes/sec] received
| Line | Meaning |
|---|---|
Document Length | body size of the first response; "length errors" are counted against it |
Complete requests | how many requests got a response |
Failed requests | failures: in parentheses Connect, Receive, Length (body of a different length), Exceptions |
Non-2xx responses | responses with a non-2xx code — not included in Failed (the line only appears if there were any) |
Keep-Alive requests | how many requests went over already-open connections (only with -k) |
Requests per second | throughput — the headline number |
Time per request (first) | mean time of one request as seen by the client |
Time per request (across all) | the first one divided by -c — essentially 1/RPS |
Connection Times | Connect — TCP setup, Waiting — until the first response byte, Processing — from connection to the end of the response, Total — overall |
Percentage … served within | percentiles: 95% 4 — 95 % of requests completed within 4 ms; 100% — the slowest |
Exit codes
0 — the test ran (even if there were Failed and Non-2xx!); non-zero — the test couldn't be run: bad URL, bad options, server unreachable.
Examples
# 1000 requests, 50 at a time
ab -n 1000 -c 50 http://127.0.0.1:8080/
# same with keep-alive — usually several times faster
ab -k -n 1000 -c 50 http://127.0.0.1:8080/
# run for 10 seconds without progress and pick out the key lines
ab -q -t 10 -c 20 http://127.0.0.1:8080/ | grep -E 'Requests per second|Failed|Non-2xx|95%'
# POST with the body from a file
echo 'name=demo' > body.txt
ab -n 100 -c 10 -p body.txt -T application/x-www-form-urlencoded http://127.0.0.1:8080/api/form
# page behind Basic auth, custom header
ab -n 200 -c 10 -A admin:secret -H 'Accept: application/json' http://127.0.0.1:8080/private/
# dynamic page with varying response length
ab -l -n 500 -c 20 http://127.0.0.1:8080/dynamic
# percentiles as CSV for a chart
ab -n 1000 -c 20 -e percentiles.csv http://127.0.0.1:8080/ >/dev/null
Pitfalls
- 404 and 500 are not
Failed. Error responses count as "successful" requests and land in theNon-2xx responsesline. A high RPS with nothing but 404s means nothing — always check that line. Theabexit code is still0in this case. Failed requestswithLengthon a dynamic page is not a server error: the body just has a different length each time. Add-l.- Without a trailing
/in the URLabfails right away withinvalid URL:http://127.0.0.1:8080/, nothttp://127.0.0.1:8080. -cgreater than-nis not allowed —Cannot use concurrency level greater than total number of requests.- Without
-kevery request is a new TCP connection. The numbers come out noticeably worse than for a browser, which keeps connections open. abuses CPU itself. If it runs on the same machine as the server, they share the CPU and the result is understated.-pto a static file in nginx returns405— everything ends up inNon-2xx.- One URL is not real load. The cache serves the same page over and over; real users hit different addresses.