Linux ab Command

Linux 命令大全Complete Linux Command Guide

ab (Apache Benchmark) is a performance testing tool bundled with the Apache HTTP server, used to measure the performance of web servers.

The ab command tests the server's processing capability by simulating multiple concurrent requests, and is a commonly used benchmarking tool for web developers and system administrators.

Key Features:

  • Simple and easy to use, no complex configuration required
  • Quickly obtain basic server performance metrics
  • Supports HTTP/HTTPS protocols
  • Open source and free, bundled with the Apache server

Installing the ab Command

In most Linux distributions, the ab command is included in the Apache tools package:

# Ubuntu/Debian 系统
sudo apt-get install apache2-utils

# CentOS/RHEL 系统
sudo yum install httpd-tools

# 验证安装
ab -V

Basic Usage

The simplest ab command format:

ab -n [请求总数] -c [并发数] [测试网址]

Example: Test against a local development server

ab -n 100 -c 10 http://localhost:8080/

This command will send tohttp://localhost:8080/100 requests with a concurrency of 10 (i.e., 10 requests being processed at the same time).


Detailed Explanation of Common Parameters

Parameter 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 a custom request header -H "Accept-Encoding: gzip"
-p POST data file -p data.json
-T POST/PUT content type -T "application/json"
-v Verbose output level -v 4

Interpreting Test Results

After running the test, ab outputs a detailed performance report. Focus on the following metrics:

  1. Requests per second: Number of requests processed per second (QPS); the higher the value, the better the performance
  2. Time per request: Average processing time per request
  3. Transfer rate: Data transfer rate
  4. Percentage served: Percentage of requests with different response times
  5. 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.txtContent:

Example

{
  "username": "testuser",
  "password": "test123"
}

Testing HTTPS Websites

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/

Troubleshooting Common Issues

Problem 1: An error "apr_socket_recv: Connection reset by peer" occurs
Solution: The server cannot handle high concurrency. Try:

  • Lower the concurrency level (-c)
  • Increase server configuration
  • Check server error logs

Problem 2: Test results fluctuate significantly
Solution:

  • : Extend the test duration (-t)
  • Ensure network stability
  • Close other programs that consume resources

Problem 3: SSL certificate verification fails
Solution: Add-kparameter to ignore SSL verification (for test environment only)


Advanced Tips

1. Automate tests with scripts:

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 plot performance curves
  • Import multiple test results into Excel for comparison

3. Combine with other tools:

  • Use top/vmstat simultaneously to monitor server resources
  • Combine with JMeter for more complex scenario testing

Linux 命令大全Complete Linux Command Guide

Other Extensions