Linux ab Command
ab (Apache Benchmark) is a performance testing tool bundled with the Apache HTTP server, used to measure web server performance.
The ab command tests a server's capacity by simulating multiple concurrent requests; it is a benchmarking tool commonly used by web developers and system administrators.
Main Features:
- Simple and easy to use, no complex configuration required
- Quickly obtain basic performance metrics of the server
- Supports HTTP/HTTPS protocols
- Open-source and free, provided with the Apache server
Install the ab Command
In most Linux distributions, the ab command is included in the Apache tools package:
# Ubuntu/Debian system
sudo apt-get install apache2-utils
# CentOS/RHEL system
sudo yum install httpd-tools
# Verify installation
ab -V
Basic Usage
The simplest form of the ab command:
ab -n [请求总数] -c [并发数] [测试网址]
Example: Test against a local development server
ab -n 100 -c 10 http://localhost:8080/
This command sends http://localhost:8080/ 100 requests with a concurrency of 10 (i.e., 10 requests being processed at the same time).
Common Parameters in Detail
| Parameters | Description | Example |
|---|---|---|
| -n | Total number of requests | -n 1000 |
| -c | Number of concurrent requests | -c 50 |
| -t | Test duration (seconds) | -t 60 |
| -k | Enable HTTP KeepAlive | -k |
| -H | Add custom request headers | -H "Accept-Encoding: gzip" |
| -p | POST data file | -p data.json |
| -T | POST/PUT content type | -T "application/json" |
| -v | Verbosity level | -v 4 |
Interpreting Test Results
After running the test, ab outputs a detailed performance report; focus on the following metrics:
- Requests per second: Requests per second (QPS); the higher the value, the better the performance
- Time per request: Average processing time per request
- Transfer rate: Data transfer rate
- Percentage served: Percentage of requests at different response times
- Failed requests: Number of failed requests
Sample Output Analysis:
Server Software: nginx/1.18.0
Server Hostname: localhost
Server Port: 8080
Document Path: /
Document Length: 612 bytes
Concurrency Level: 10
Time taken for tests: 0.845 seconds
Complete requests: 100
Failed requests: 0
Total transferred: 82400 bytes
HTML transferred: 61200 bytes
Requests per second: 118.34 [#/sec] (mean)
Time per request: 84.502 [ms] (mean)
Time per request: 8.450 [ms] (mean, across all concurrent requests)
Transfer rate: 95.22 [Kbytes/sec] received
Practical Application Examples
Testing POST Requests
Example
ab
-n
500
-c
20
-p
postdata.txt
-T
"application/json"
http:
//
api.example.com
/
login
postdata.txt of contents:
Example
{
"username": "testuser",
"password": "test123"
}
Testing HTTPS Sites
Example
ab
-n
1000
-c
100
https:
//
www.example.com
/
Testing with Authentication
Example
ab
-n
200
-c
10
-A
username:password http:
//
secure.example.com
/
Common Problem Solutions
Problem 1: The "apr_socket_recv: Connection reset by peer" error occurs
Solution: The server cannot handle high concurrency; try:
- Reduce the concurrency (
-c) - Increase server configuration
- Check the server error log
Problem 2: Test results fluctuate greatly
Solution:
- Extend the test duration (
-t) - Ensure a stable network
- Close other resource-consuming programs
Problem 3: SSL certificate verification fails
Solution: Add the -k parameter to ignore SSL verification (for testing environments only)
Advanced Tips
1. Combine with scripts for automated testing:
Example
#!/bin/bash
for
i
in
{
1
..
5
}
;
do
ab
-n
1000
-c
$
(
(
i
20
)
)
http:
//
localhost:
8080
/
results.txt
done
2. Visualize test results:
- Use gnuplot or other tools to draw performance curves
- Import multiple test results into Excel for comparison
3. Combine with other tools:
- Use top/vmstat to monitor server resources at the same time
- Combine with JMeter for more complex scenario testing
Linux Command Reference