# ApacheBench (ab) — Benchmark Web Servers from the Command Line

> Practical guide to ApacheBench (ab) — HTTP load tests, concurrency, Keep-Alive, POST data and TLS benchmarks on the command line.

Source: https://www.jpkc.com/db/en/cheatsheets/networking/ab/

<!-- PROSE:intro -->
ApacheBench is the classic HTTP load-testing tool from the Apache project: a single command fires hundreds or thousands of requests at a web endpoint and instantly returns throughput, response-time percentiles and error counts. It ships on any system with apache2-utils installed – ideal for a quick smoke test after a deployment or a first-pass stress check before a traffic spike. This guide covers the most-used options, from a basic GET benchmark to HTTPS and POST-JSON testing.
<!-- PROSE:intro:end -->

## Basic Benchmarks

`ab -n <requests> -c <concurrent> <url>` — Run a benchmark with specified requests and concurrency.

```bash
ab -n 1000 -c 10 http://localhost:8080/
```

`ab -n 100 <url>` — Simple benchmark with 100 requests (1 concurrent).

```bash
ab -n 100 http://localhost/
```

`ab -t <seconds> -c <concurrent> <url>` — Benchmark for a fixed duration instead of request count.

```bash
ab -t 30 -c 50 http://localhost:8080/
```

`ab -k -n <requests> -c <concurrent> <url>` — Use HTTP Keep-Alive (persistent connections).

```bash
ab -k -n 5000 -c 100 http://localhost:8080/
```

## POST & PUT Data

`ab -p <file> -T <content-type> <url>` — Send POST data from a file.

```bash
ab -p data.json -T 'application/json' -n 100 -c 10 http://localhost:8080/api
```

`ab -u <file> -T <content-type> <url>` — Send PUT data from a file.

```bash
ab -u update.json -T 'application/json' -n 50 http://localhost:8080/api/item
```

`ab -p <file> -T 'application/x-www-form-urlencoded' <url>` — POST form data.

```bash
ab -p form.txt -T 'application/x-www-form-urlencoded' -n 100 http://localhost/login
```

## Headers & Authentication

`ab -H '<header>' <url>` — Add a custom HTTP header.

```bash
ab -H 'Authorization: Bearer token123' -n 100 http://localhost:8080/api
```

`ab -H 'Accept: application/json' <url>` — Set Accept header for API requests.

```bash
ab -H 'Accept: application/json' -n 100 http://localhost:8080/api
```

`ab -A <user>:<pass> <url>` — Use HTTP Basic Authentication.

```bash
ab -A admin:secret -n 100 http://localhost/admin/
```

`ab -C '<cookie>' <url>` — Send a cookie with requests.

```bash
ab -C 'session=abc123' -n 100 http://localhost/dashboard
```

## SSL & Proxy

`ab -n <requests> -c <concurrent> https://<url>` — Benchmark an HTTPS endpoint.

```bash
ab -n 1000 -c 20 https://example.com/
```

`ab -f <protocol> <url>` — Force a specific SSL/TLS protocol.

```bash
ab -f TLS1.2 -n 100 https://example.com/
```

`ab -X <proxy:port> <url>` — Use a proxy server.

```bash
ab -X localhost:8888 -n 100 http://example.com/
```

## Output & Reporting

`ab -v <level> <url>` — Set verbosity level (1=warnings, 2=headers, 3+=body).

```bash
ab -v 2 -n 10 http://localhost:8080/
```

`ab -g <file> <url>` — Export results as TSV (for gnuplot).

```bash
ab -g results.tsv -n 1000 -c 50 http://localhost:8080/
```

`ab -e <file> <url>` — Export percentile data as CSV.

```bash
ab -e percentiles.csv -n 1000 -c 50 http://localhost:8080/
```

`ab -r <url>` — Don't exit on socket receive errors.

```bash
ab -r -n 5000 -c 200 http://localhost:8080/
```

`ab -s <timeout> <url>` — Set maximum seconds to wait for a response (default 30).

```bash
ab -s 60 -n 100 http://slow-api.example.com/
```

<!-- PROSE:outro -->
## Conclusion

ApacheBench is not a replacement for full-featured load-testing frameworks – but for a quick smoke test after a deployment or an early indicator of performance regressions it is unbeatable in its simplicity. If you need percentile charts, HTTP/2 support or more realistic load profiles, take a look at `wrk` or `h2load`.

## Further Reading

- [ab – official Apache documentation](https://httpd.apache.org/docs/current/programs/ab.html) – reference and all options
- [ab(1) – manual page](https://manpages.debian.org/stable/apache2-utils/ab.1.en.html) – every flag at a glance
- [ApacheBench – Wikipedia](https://en.wikipedia.org/wiki/ApacheBench) – background and history
<!-- PROSE:outro:end -->

## Related Commands

- [wrk](https://www.jpkc.com/db/en/cheatsheets/networking/wrk/) – modern HTTP benchmarker with Lua scripting for realistic load profiles
- [siege](https://www.jpkc.com/db/en/cheatsheets/networking/siege/) – HTTP/HTTPS load tester with URL lists and session support
- [h2load](https://www.jpkc.com/db/en/cheatsheets/networking/h2load/) – benchmarking tool designed for HTTP/2 and HTTP/3

