Filters
How to search
  • Plain words find names, texts and examples.
  • "two words" finds the exact phrase.
  • --name or -x finds an option.
  • #word looks only inside commands.
  • @grep finds lessons and labs that use grep.
  • Filters: cat:network · in:labs · in:notes · level:rhcsa · ch:8 · root:yes. Put ! before a word to leave it out. OR between words means one of them.
  • ↑ ↓ to move · Enter to open · Esc to close · / to search

ab

ApacheBench: a quick HTTP benchmark from the command line — N requests, C at the same time, then requests per second and latency.

Level
DevOps tool
Group
Web
Needs root
No
Package
httpd-tools

What it does

ab comes with Apache httpd tools and is on almost every server. It sends one URL again and again and prints a summary: requests per second, time per request, failed requests and the latency percentiles. It is simple: one URL, no scripts. For real scenarios use k6.

Good for: a quick before/after check of an nginx change, a cache, or a new server size.

How to write it

ab [OPTIONS] URL

Options 14

Most used

Options

-n N
Total number of requests.
-c N
Requests at the same time (concurrency).
-t SECS
Run for this time (max).
-k
HTTP keep-alive.
-H 'Header: value'
Extra header (for example a token).
-p FILE -T TYPE
POST the file with this content type.
-u FILE
PUT the file.
-C 'name=value'
Send a cookie.
-l
Do not count different response lengths as errors.
-e FILE.csv
Write the percentiles as CSV.
-g FILE.tsv
Write all measurements (gnuplot).
-s SECS
Timeout per request.
-r
Do not stop on socket errors.
-v 2
Show headers (debug).

Examples 10

$ run as a normal user# run as root (or with sudo)

  1. 11000 requests, 50 at a time

    ab -n 1000 -c 50 https://staging.example.com/
    Requests per second:    812.40 [#/sec] (mean)
    Time per request:       61.546 [ms] (mean)
    Failed requests:        0
  2. 2With keep-alive (like real browsers)

    ab -k -n 5000 -c 100 https://staging.example.com/
  3. 3Test an API with a token

    ab -n 500 -c 20 -H "Authorization: Bearer $TOKEN" https://staging.example.com/api/products
  4. 4POST JSON

    echo '{"sku":"BA-101"}' > body.json
    ab -n 200 -c 10 -p body.json -T application/json https://staging.example.com/api/cart
  5. 5Run for 30 seconds

    ab -t 30 -c 50 https://staging.example.com/
  6. 6Dynamic pages have different lengths: do not count them as failed

    ab -l -n 1000 -c 50 https://staging.example.com/search?q=racket
  7. 7Compare before and after a change

    ab -k -n 2000 -c 50 https://staging.example.com/ | grep -E 'Requests per second|95%'
  8. 8Save the percentiles for a report

    ab -n 1000 -c 50 -e result.csv https://staging.example.com/
  9. 9Install on RHEL / Ubuntu

    dnf install httpd-tools
    apt install apache2-utils
  10. 10Debug the headers of one request

    ab -n 1 -v 2 https://staging.example.com/

Watch out

  • The URL needs a path: https://host/ (with the last slash), not https://host.
  • ab uses HTTP/1.0 and one URL; results look better than real traffic.
  • Do not benchmark production or other people's servers without permission.

Exit codes

0The test ran.
1Error (bad URL, cannot connect).

Full manual

This page explains the command in simple words. The original manual has every detail.

In your terminal: man ab