CentOS 8 ab 安装与命令参数详解

在 CentOS 8 上安装 httpd-tools 的 ab 2.3 并逐参数实测 HTTP 压测命令输入与回显,涵盖 URL 目标、-n/-c/-t/-k/-q/-v/-H/-T/-p/-u/-C/-r/-s/-i/-w/-e 等参数及离线 .rpm 安装与依赖处理。

网络测试ab压测压测Linux命令行2026/10/09

CentOS 8 ab 安装与命令参数详解

目标:在 CentOS 8 上安装 Apache HTTP 服务器工具包 httpd-tools 自带的 HTTP 压测工具 ab(ApacheBench 2.3),并逐参数实测压测命令的输入与输出,涵盖 URL 目标、-n、-c、-t、-k、-q、-v、-H、-T、-p、-u、-C、-r、-s、-i、-w、-e 等参数,以及在线/离线安装与依赖处理。本文命令与输出均为 CentOS 8(ab 2.3,包 httpd-tools-2.4.37-39.module_el8.4.0+950+0577e6ac.1.x86_64)对目标站 http://172.28.0.5:8080/ 实测回显。

一、环境信息

  • 操作系统:CentOS 8
  • ab 版本:ApacheBench Version 2.3(<$Revision: 1843412 $>)
  • 安装包:httpd-tools 2.4.37-39.module_el8.4.0+950+0577e6ac.1.x86_64(提供 ab 等 HTTP 工具)
  • 压测客户端主机:本机,网卡 eth0,IP 172.28.0.3/16
  • 目标站点:172.28.0.5:8080(HTTP 服务,供压测)
  • 目标服务类型:Python http.server(SimpleHTTP,Python 3.6.8)
  • 测试文档:/index.html(19 字节)、/file2m.bin(2 MiB)
  • 工作目录:/opt/data/ab/

二、工具概述

ab(ApacheBench)是随 Apache HTTP Server 工具包一同安装的 HTTP 压测工具,用于模拟多并发客户端向目标 URL 发起请求,输出吞吐量、平均/峰值响应时间、连接耗时分布与完成的请求百分比,是评估 Web 服务性能的轻量命令行工具。它的单机高并发能力有限,更适合功能对比与粗略压测;生产级高并发建议配合 wrk/hey 等多进程工具。本文针对 CentOS 8 上的 ab 2.3 实测。

三、安装

3.1 在线安装(dnf/yum)

sudo dnf install -y httpd-tools
# 或
sudo yum install -y httpd-tools

💡 CentOS 8 上 ab 由 httpd-tools 包提供(对应 Ubuntu 的 apache2-utils),装完即得 ab 命令。

安装后验证版本与软件包状态:

ab -V
This is ApacheBench, Version 2.3 <$Revision: 1843412 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
rpm -q httpd-tools
httpd-tools-2.4.37-39.module_el8.4.0+950+0577e6ac.1.x86_64

💡 ab -V 显示版本号与版权行(Version 2.3 + $Revision),不显示编译参数;调试/详细请求信息请使用 -v 而不是 -V。

3.2 离线安装(rpm 包)

在有网络的 CentOS 8 机器上先用 dnf 下载 rpm 包(dnf download 由 dnf-plugins-core 提供):

sudo dnf install -y dnf-plugins-core
cd /opt/data/ab/offline
dnf download httpd-tools
httpd-tools-2.4.37-39.module_el8.4.0+950+0577e6ac 226 kB/s | 106 kB     00:00
ls -la /opt/data/ab/offline/*.rpm
-rw-r--r-- 1 root root 108519 Oct  9 04:36 /opt/data/ab/offline/httpd-tools-2.4.37-39.module_el8.4.0+950+0577e6ac.1.x86_64.rpm

将 rpm 拷贝到离线机器后,推荐用 dnf install 本地包路径安装(会自动从可用仓库解析 libapr 等依赖):

sudo dnf install -y /opt/data/ab/offline/httpd-tools-2.4.37-39.module_el8.4.0+950+0577e6ac.1.x86_64.rpm
  apr-util-1.6.1-6.el8.x86_64
  apr-util-bdb-1.6.1-6.el8.x86_64
  apr-util-openssl-1.6.1-6.el8.x86_64
  httpd-tools-2.4.37-39.module_el8.4.0+950+0577e6ac.1.x86_64

⚠️ 不要直接用 rpm -ivh 装 httpd-tools 离线包:实测 rpm -ivh httpd-tools-2.4.37-39.module_el8.4.0+950+0577e6ac.1.x86_64.rpm 会因缺少 libapr-1.so.0()(64bit)、libaprutil-1.so.0()(64bit) 报依赖错误直接失败。正确做法是用 dnf install ./httpd-tools-2.4.37-39.module_el8.4.0+950+0577e6ac.1.x86_64.rpm(本地路径 + ./),由 dnf 自动把 apr/apr-util 一并解析安装;完全无网络的内网机器需把 apr、apr-util 的 rpm 也一并离线下载后同目录安装。

安装后验证:

ab -V 2>&1 | head -1
rpm -q httpd-tools
This is ApacheBench, Version 2.3 <$Revision: 1843412 $>
httpd-tools-2.4.37-39.module_el8.4.0+950+0577e6ac.1.x86_64

四、命令实测

压测目标均为 http://172.28.0.5:8080/。/index.html 为 19 字节小页面(测吞吐/并发),/file2m.bin 为 2 MiB 大文件(测传输速率)。ab 多请求时会在 STDERR 滚动 Completed 100 requests 进度行,为使输出可读,以下对较长实测量已略去中间进度行,仅保留完整汇总段,所有字段均来自真实回显。

4.1 URL 目标与基本压测 —— ab -n 1000 -c 50 http://172.28.0.5:8080/index.html

用途:对指定 URL 发起 1000 次请求、并发 50 的基准压测,是最常用的入门命令。

参数说明:URL 必须带 http://(或 https://);-n requests 总请求数;-c concurrency 并发数(同时发起的请求数)。

ab -n 1000 -c 50 http://172.28.0.5:8080/index.html
This is ApacheBench, Version 2.3 <$Revision: 1843412 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 172.28.0.5 (be patient)
Finished 1000 requests


Server Software:        SimpleHTTP/0.6
Server Hostname:        172.28.0.5
Server Port:            8080

Document Path:          /index.html
Document Length:        19 bytes

Concurrency Level:      50
Time taken for tests:   0.376 seconds
Complete requests:      1000
Failed requests:        0
Total transferred:      203000 bytes
HTML transferred:       19000 bytes
Requests per second:    2661.41 [#/sec] (mean)
Time per request:       18.787 [ms] (mean)
Time per request:       0.376 [ms] (mean, across all concurrent requests)
Transfer rate:          527.60 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.3      0       3
Processing:     0    2   0.9      2      11
Waiting:        0    2   0.9      2      11
Total:          0    2   1.1      2      14

Percentage of the requests served within a certain time (ms)
  50%      2
  66%      2
  75%      2
  80%      3
  90%      3
  95%      4
  98%      4
  99%      6
 100%     14 (longest request)

解读:Concurrency Level=50、Complete requests=1000、Failed requests=0 表示全部成功;吞吐 Requests per second=2661.41 请求/秒;两行 Time per request 分别是在并发下的平均耗时(18.787ms)和按单请求均摊的耗时(0.376ms);Connection Times 给出连接/处理/等待/总耗时统计;Percentage... 是响应时间分布,如 99% 请求在 6ms 内完成。

4.2 按时长压测 —— ab -t 5 -c 10 http://172.28.0.5:8080/index.html

用途:以固定时长(而非固定请求数)压测,适合连续吞吐观察。

参数说明:-t timelimit 压测最长时间(秒);使用 -t 时系统自动把请求数设为 50000 兜底(帮助原文:This implies -n 50000),到点即结束。

ab -t 5 -c 10 http://172.28.0.5:8080/index.html
This is ApacheBench, Version 2.3 <$Revision: 1843412 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 172.28.0.5 (be patient)
Finished 16243 requests


Server Software:        SimpleHTTP/0.6
Server Hostname:        172.28.0.5
Server Port:            8080

Document Path:          /index.html
Document Length:        19 bytes

Concurrency Level:      10
Time taken for tests:   5.000 seconds
Complete requests:      16243
Failed requests:        0
Total transferred:      3298141 bytes
HTML transferred:       308693 bytes
Requests per second:    3248.45 [#/sec] (mean)
Time per request:       3.078 [ms] (mean)
Time per request:       0.308 [ms] (mean, across all concurrent requests)
Transfer rate:          644.14 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    1  33.6      0    2086
Processing:     0    2   7.5      2     826
Waiting:        0    2   7.4      2     826
Total:          0    3  37.0      2    2088

Percentage of the requests served within a certain time (ms)
  50%      2
  66%      2
  75%      2
  80%      2
  90%      3
  95%      3
  98%      3
  99%      4
 100%   2088 (longest request)

解读:Time taken for tests=5.000 秒即到时长自动停止;5 秒内完成 16243 次请求,Requests per second=3248.45。Finished 16243 requests 说明本次到点结束而非跑满 50000 兜底数。

4.3 开启 Keep-Alive 复用连接 —— ab -k -n 2000 -c 20 http://172.28.0.5:8080/index.html

用途:复用已建立的 TCP 连接(HTTP Keep-Alive),减少建连开销,对比连接复用对吞吐的影响。

参数说明:-k 使用 HTTP Keep-Alive(帮助原文:Use HTTP KeepAlive feature)。

ab -k -n 2000 -c 20 http://172.28.0.5:8080/index.html
This is ApacheBench, Version 2.3 <$Revision: 1843412 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 172.28.0.5 (be patient)
Finished 2000 requests


Server Software:        SimpleHTTP/0.6
Server Hostname:        172.28.0.5
Server Port:            8080

Document Path:          /index.html
Document Length:        19 bytes

Concurrency Level:      20
Time taken for tests:   0.585 seconds
Complete requests:      2000
Failed requests:        0
Keep-Alive requests:    0
Total transferred:      406000 bytes
HTML transferred:       38000 bytes
Requests per second:    3418.93 [#/sec] (mean)
Time per request:       5.850 [ms] (mean)
Time per request:       0.292 [ms] (mean, across all concurrent requests)
Transfer rate:          677.78 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.1      0       2
Processing:     0    2   0.4      2       5
Waiting:        0    2   0.4      2       5
Total:          0    2   0.4      2       5

Percentage of the requests served within a certain time (ms)
  50%      2
  66%      2
  75%      2
  80%      2
  90%      2
  95%      2
  98%      3
  99%      3
 100%      5 (longest request)

解读:Keep-Alive requests=0 说明目标 Python SimpleHTTP/0.6 服务端收到含 Keep-Alive 的请求后未对该 ab 连接标记为复用(Python http.server 以 HTTP/1.0 Connection: close 响应),因此实际仍未复用连接;吞吐与 4.1 同并发量级相当,佐证未真正复用。请以目标服务端是否支持 Keep-Alive 为准。

4.4 静默模式(大于 150 请求不再打进度) —— ab -q -n 3000 -c 30 http://172.28.0.5:8080/index.html

用途:-q 在请求数超过 150 时取消逐 100 行进度输出,只打印汇总,适合脚本化落盘。

参数说明:-q 帮助原文:Do not show progress when doing more than 150 requests。

ab -q -n 3000 -c 30 http://172.28.0.5:8080/index.html
This is ApacheBench, Version 2.3 <$Revision: 1843412 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 172.28.0.5 (be patient).....done


Server Software:        SimpleHTTP/0.6
Server Hostname:        172.28.0.5
Server Port:            8080

Document Path:          /index.html
Document Length:        19 bytes

Concurrency Level:      30
Time taken for tests:   0.874 seconds
Complete requests:      3000
Failed requests:        0
Total transferred:      609000 bytes
HTML transferred:       57000 bytes
Requests per second:    3433.08 [#/sec] (mean)
Time per request:       8.739 [ms] (mean)
Time per request:       0.291 [ms] (mean, across all concurrent requests)
Transfer rate:          680.58 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.1      0       3
Processing:     0    2   0.4      2       4
Waiting:        0    2   0.4      1       4
Total:          0    2   0.4      2       4
ERROR: The median and mean for the waiting time are more than twice the standard
       deviation apart. These results are NOT reliable.

Percentage of the requests served within a certain time (ms)
  50%      2
  66%      2
  75%      2
  80%      2
  90%      2
  95%      3
  98%      3
  99%      3
 100%      4 (longest request)

解读:加上 -q 后 3000 个请求中间不再输出 Completed x00 requests 进度,只在开始时打印 ...done,随后直接进入汇总。末尾 ERROR: ...NOT reliable 是 ab 的统计提示而非执行失败:本机响应毫秒级、均值与中位数偏差超两倍标准差所致,详情见常见问题。

4.5 详细级别——打印请求头与响应头 —— ab -v 4 -n 2 -c 1 -H "X-Auth: token123" -C "session=pmm" http://172.28.0.5:8080/index.html

用途:-v 输出排障级别(1-4)详细信息,-v 4 打印逐请求的请求头发送与响应头接收,便于核对自定义头、Cookie 是否生效。

参数说明:-v verbosity 帮助原文:How much troubleshooting info to print(0 关闭、1 报告、2-3 基本信息、4 请求级详细);-H attribute 添加任意请求头(帮助原文:Add Arbitrary header line, eg. 'Accept-Encoding: gzip',可重复);-C attribute 添加 Cookie(帮助原文:Add cookie, eg. 'Apache=1234',可重复)。

ab -v 4 -n 2 -c 1 -H "X-Auth: token123" -C "session=pmm" http://172.28.0.5:8080/index.html
This is ApacheBench, Version 2.3 <$Revision: 1843412 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 172.28.0.5 (be patient)...INFO: GET header ==
---
GET /index.html HTTP/1.0
Cookie: session=pmm
X-Auth: token123
Host: 172.28.0.5:8080
User-Agent: ApacheBench/2.3
Accept: */*


---
LOG: header received:
HTTP/1.0 200 OK
Server: SimpleHTTP/0.6 Python/3.6.8
Date: Fri, 09 Oct 2026 04:36:16 GMT
Content-type: text/html
Content-Length: 19
Last-Modified: Fri, 09 Oct 2026 04:27:10 GMT


LOG: Response code = 200
LOG: header received:
HTTP/1.0 200 OK
Server: SimpleHTTP/0.6 Python/3.6.8
Date: Fri, 09 Oct 2026 04:36:16 GMT
Content-type: text/html
Content-Length: 19
Last-Modified: Fri, 09 Oct 2026 04:27:10 GMT


LOG: Response code = 200
..done


Server Software:        SimpleHTTP/0.6
Server Hostname:        172.28.0.5
Server Port:            8080

Document Path:          /index.html
Document Length:        19 bytes

Concurrency Level:      1
Time taken for tests:   0.002 seconds
Complete requests:      2
Failed requests:        0
Total transferred:      406 bytes
HTML transferred:       38 bytes
Requests per second:    1150.75 [#/sec] (mean)
Time per request:       0.869 [ms] (mean)
Time per request:       0.869 [ms] (mean, across all concurrent requests)
Transfer rate:          228.13 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.0      0       0
Processing:     1    1   0.1      1       1
Waiting:        1    1   0.1      1       1
Total:          1    1   0.1      1       1

Percentage of the requests served within a certain time (ms)
  50%      1
  66%      1
  75%      1
  80%      1
  90%      1
  95%      1
  98%      1
  99%      1
 100%      1 (longest request)

解读:INFO: GET header == 后列出的请求头中可见 Cookie: session=pmm(来自 -C)与 X-Auth: token123(来自 -H),均插入在 Host 等常规头之前;LOG: header received: 后是目标服务端逐请求返回的响应头(HTTP/1.0 200 OK、Server: SimpleHTTP/0.6 Python/3.6.8 等)。-v 4 只作少量请求(-n 2 -c 1)调试用,量大时不要这么用。

4.6 自定义请求头 —— 单一用途演示 ab -n 20 -c 2 -H "X-Test-Header: ab-demo" http://172.28.0.5:8080/index.html

用途:单发一个自定义头即可用于功能验证(详见 4.5 的多头场景),此处展示常规汇总回显。

参数说明:-H "Header: value" 任意请求头,可多次重复以追加。

ab -n 20 -c 2 -H "X-Test-Header: ab-demo" http://172.28.0.5:8080/index.html
This is ApacheBench, Version 2.3 <$Revision: 1843412 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 172.28.0.5 (be patient).....done


Server Software:        SimpleHTTP/0.6
Server Hostname:        172.28.0.5
Server Port:            8080

Document Path:          /index.html
Document Length:        19 bytes

Concurrency Level:      2
Time taken for tests:   0.009 seconds
Complete requests:      20
Failed requests:        0
Total transferred:      4060 bytes
HTML transferred:       380 bytes
Requests per second:    2315.89 [#/sec] (mean)
Time per request:       0.864 [ms] (mean)
Time per request:       0.432 [ms] (mean, across all concurrent requests)
Transfer rate:          459.11 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.1      0       0
Processing:     0    1   0.1      1       1
Waiting:        0    1   0.2      1       1
Total:          0    1   0.2      1       1

Percentage of the requests served within a certain time (ms)
  50%      1
  66%      1
  75%      1
  80%      1
  90%      1
  95%      1
  98%      1
  99%      1
 100%      1 (longest request)

解读:小并发(-c 2)+ 少量请求(-n 20)时吞吐 2315.89 请求/秒,Total transferred 4060 字节 = 19 字节 × 20 请求 + 响应头开销。-H 对 ab 汇总无影响,只改变实际发出的请求头。

4.7 发送 POST 数据 —— -p 与 -T(对不支持 POST 的目标会返回非 2xx)

用途:-p postfile 指定 POST 数据文件(帮助原文:File containing data to POST. Remember also to set -T);-T content-type 指定请求体的 Content-Type(帮助原文:Default is 'text/plain')。

参数说明:POST 需同时给出数据文件与 -T;ab 会自动发出 POST /path 请求并在汇总段显示 Total body sent。

实测数据文件:

cat /opt/data/ab/post.txt
POSTDATA_AB_TEST
ab -n 20 -c 2 -p /opt/data/ab/post.txt -T "application/x-www-form-urlencoded" http://172.28.0.5:8080/
This is ApacheBench, Version 2.3 <$Revision: 1843412 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 172.28.0.5 (be patient).....done


Server Software:        SimpleHTTP/0.6
Server Hostname:        172.28.0.5
Server Port:            8080

Document Path:          /
Document Length:        497 bytes

Concurrency Level:      2
Time taken for tests:   0.007 seconds
Complete requests:      20
Failed requests:        0
Non-2xx responses:      20
Total transferred:      13880 bytes
Total body sent:        3380
HTML transferred:       9940 bytes
Requests per second:    2987.30 [#/sec] (mean)
Time per request:       0.669 [ms] (mean)
Time per request:       0.335 [ms] (mean, across all concurrent requests)
Transfer rate:          2024.60 [Kbytes/sec] received
                        493.02 kb/s sent
                        2517.62 kb/s total

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.1      0       0
Processing:     0    1   0.2      1       1
Waiting:        0    0   0.2      0       1
Total:          0    1   0.2      1       1

Percentage of the requests served within a certain time (ms)
  50%      1
  66%      1
  75%      1
  80%      1
  90%      1
  95%      1
  98%      1
  99%      1
 100%      1 (longest request)

解读:目标为 Python SimpleHTTP/0.6,不支持 POST(返回 501/405),故 Non-2xx responses=20、Document Length=497 为错误页字节数;Total body sent=3380 字节即 20 次请求体(16 字节 × 20 = 320)加 HTTP 头开销。解读要点:Failed requests=0 不表示成功——ab 把「HTTP 层面已返回、但非 2xx」计入 Non-2xx responses,压测时务必要看 Non-2xx responses 是否为 0。

4.8 发送 PUT 数据 —— -u 与 -T

用途:-u putfile 指定 PUT 数据文件(帮助原文:File containing data to PUT. Remember also to set -T),用法与 -p 同理。

cat /opt/data/ab/put.txt
PUTDATA_AB_TEST
ab -n 20 -c 2 -u /opt/data/ab/put.txt -T "text/plain" http://172.28.0.5:8080/
This is ApacheBench, Version 2.3 <$Revision: 1843412 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 172.28.0.5 (be patient).....done


Server Software:        SimpleHTTP/0.6
Server Hostname:        172.28.0.5
Server Port:            8080

Document Path:          /
Document Length:        496 bytes

Concurrency Level:      2
Time taken for tests:   0.009 seconds
Complete requests:      20
Failed requests:        0
Non-2xx responses:      20
Total transferred:      13840 bytes
Total body sent:        2880
HTML transferred:       9920 bytes
Requests per second:    2322.88 [#/sec] (mean)
Time per request:       0.861 [ms] (mean)
Time per request:       0.430 [ms] (mean, across all concurrent requests)
Transfer rate:          1569.76 [Kbytes/sec] received
                        326.66 kb/s sent
                        1896.41 kb/s total

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.1      0       1
Processing:     0    1   0.2      1       1
Waiting:        0    1   0.2      1       1
Total:          0    1   0.3      1       1

Percentage of the requests served within a certain time (ms)
  50%      1
  66%      1
  75%      1
  80%      1
  90%      1
  95%      1
  98%      1
  99%      1
 100%      1 (longest request)

解读:与 4.7 相同,Python SimpleHTTP/0.6 不支持 PUT,返回非 2xx;Total body sent=2880 字节(15 字节 × 20 次 + 头开销)。目标若是支持 PUT 的接口(如 Tomcat/OpenAPI 服务),此输出中 Non-2xx responses 应为 0。

用途:-C 模拟携带 Cookie(帮助原文:Add cookie, eg. 'Apache=1234'),可重复指定多个 Cookie 键值对;如需完整 Cookie: a=1; b=2 头也可用 -H "Cookie: a=1; b=2"。

参数说明:-C "sessionid=ab123" 即发送 Cookie: sessionid=ab123;验证发送是否生效见 4.5 的 -v 4 回显。

ab -n 20 -c 2 -C "sessionid=ab123" http://172.28.0.5:8080/index.html
This is ApacheBench, Version 2.3 <$Revision: 1843412 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 172.28.0.5 (be patient).....done


Server Software:        SimpleHTTP/0.6
Server Hostname:        172.28.0.5
Server Port:            8080

Document Path:          /index.html
Document Length:        19 bytes

Concurrency Level:      2
Time taken for tests:   0.008 seconds
Complete requests:      20
Failed requests:        0
Total transferred:      4060 bytes
HTML transferred:       380 bytes
Requests per second:    2511.62 [#/sec] (mean)
Time per request:       0.796 [ms] (mean)
Time per request:       0.398 [ms] (mean, across all concurrent requests)
Transfer rate:          497.91 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.0      0       0
Processing:     0    1   0.1      1       1
Waiting:        0    1   0.1      1       1
Total:          1    1   0.2      1       1

Percentage of the requests served within a certain time (ms)
  50%      1
  66%      1
  75%      1
  80%      1
  90%      1
  95%      1
  98%      1
  99%      1
 100%      1 (longest request)

解读:-C 对小页面基准性能无影响,汇总字段与普通 GET 一致;其效果体现在实际请求头里的 Cookie: 行(见 4.5 回显)。

4.10 出错继续(socket 接收错误不退出) —— -r(对非监听端口)

用途:-r 帮助原文:Don't exit on socket receive errors。当目标不可达/中途断开时,默认 ab 收到 apr_socket_recv 错误即退出;加 -r 则继续跑完剩余请求。

先看默认行为(对非监听端口 9999,不加 -r):

ab -n 5 -c 2 http://172.28.0.5:9999/
This is ApacheBench, Version 2.3 <$Revision: 1843412 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 172.28.0.5 (be patient)...apr_socket_recv: Connection refused (111)

进程退出码为 111:

ab -n 5 -c 2 http://172.28.0.5:9999/ >/dev/null 2>&1; echo "RC=$?"
RC=111

加 -r 后继续完成:

ab -r -n 10 -c 2 http://172.28.0.5:9999/
This is ApacheBench, Version 2.3 <$Revision: 1843412 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 172.28.0.5 (be patient).....done


Server Software:
Server Hostname:        172.28.0.5
Server Port:            9999

Document Path:          /
Document Length:        0 bytes

Concurrency Level:      2
Time taken for tests:   0.000 seconds
Complete requests:      10
Failed requests:        15
   (Connect: 0, Receive: 10, Length: 0, Exceptions: 5)
Total transferred:      0 bytes
HTML transferred:       0 bytes
Requests per second:    29239.77 [#/sec] (mean)
Time per request:       0.068 [ms] (mean)
Time per request:       0.034 [ms] (mean, across all concurrent requests)
Transfer rate:          0.00 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.0      0       0
Processing:     0    0   0.0      0       0
Waiting:        0    0   0.0      0       0
Total:          0    0   0.0      0       0

Percentage of the requests served within a certain time (ms)
  50%      0
  66%      0
  75%      0
  80%      0
  90%      0
  95%      0
  98%      0
  99%      0
 100%      0 (longest request)

解读:加 -r 后 ab 不再因连接被拒而中途退出,Complete requests=10 跑完,但 Failed requests=15(含 Receive 10、Exceptions 5),Total transferred=0。Failed requests 大于 Complete requests 说明有请求在建立连接后接收失败被计错——这是 -r 场景下的典型正常表现,压测里若端口可达但出现大量 failed 需排查服务与网络。

4.11 超时控制 —— -s 1(单请求等待超时)

用途:-s timeout 设置单次请求的最大等待时间(秒),默认 30 秒(见 ab -h,Default is 30 seconds)。对响应慢的目标调低可缩短整体压测时长;目标响应正常时 -s 1 不影响结果。

ab -s 1 -n 500 -c 10 http://172.28.0.5:8080/index.html
This is ApacheBench, Version 2.3 <$Revision: 1843412 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 172.28.0.5 (be patient)
Finished 500 requests


Server Software:        SimpleHTTP/0.6
Server Hostname:        172.28.0.5
Server Port:            8080

Document Path:          /index.html
Document Length:        19 bytes

Concurrency Level:      10
Time taken for tests:   0.177 seconds
Complete requests:      500
Failed requests:        0
Total transferred:      101500 bytes
HTML transferred:       9500 bytes
Requests per second:    2816.98 [#/sec] (mean)
Time per request:       3.550 [ms] (mean)
Time per request:       0.355 [ms] (mean, across all concurrent requests)
Transfer rate:          558.44 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.1      0       2
Processing:     0    2   0.8      2       6
Waiting:        0    2   0.8      2       6
Total:          0    2   0.8      2       6

Percentage of the requests served within a certain time (ms)
  50%      2
  66%      2
  75%      2
  80%      3
  90%      3
  95%      3
  98%      4
  99%      5
 100%      6 (longest request)

解读:目标响应约 0~6ms,远低于 1s 超时,故 Failed requests=0、进程正常跑完。若目标无响应超过 -s 指定的秒数,ab 会把这些请求计为超时(failed),并可能整体提前结束。

4.12 使用 HEAD 请求 —— -i

用途:-i 帮助原文:Use HEAD instead of GET,只取响应头、不取响应体,常用于 HEAD 接口联通性压测。

ab -i -n 50 -c 5 http://172.28.0.5:8080/index.html
This is ApacheBench, Version 2.3 <$Revision: 1843412 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 172.28.0.5 (be patient).....done


Server Software:        SimpleHTTP/0.6
Server Hostname:        172.28.0.5
Server Port:            8080

Document Path:          /index.html
Document Length:        0 bytes

Concurrency Level:      5
Time taken for tests:   0.013 seconds
Complete requests:      50
Failed requests:        0
Total transferred:      9200 bytes
HTML transferred:       0 bytes
Requests per second:    3841.13 [#/sec] (mean)
Time per request:       1.302 [ms] (mean)
Time per request:       0.260 [ms] (mean, across all concurrent requests)
Transfer rate:          690.20 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.1      0       0
Processing:     0    1   0.4      1       2
Waiting:        0    1   0.4      1       2
Total:          0    1   0.3      1       2

Percentage of the requests served within a certain time (ms)
  50%      1
  66%      1
  75%      1
  80%      1
  90%      2
  95%      2
  98%      2
  99%      2
 100%      2 (longest request)

解读:Document Path 仍是 /index.html,但 Document Length=0、HTML transferred=0 bytes,只传了响应头;Total transferred=9200 字节为 50 次响应头累计。

4.13 大文件压测(传输速率) —— ab -n 200 -c 20 http://172.28.0.5:8080/file2m.bin

用途:以 2 MiB 文件为目标,观察吞吐与 Transfer rate(有效传输速率),用于带宽/传输评估。

ab -n 200 -c 20 http://172.28.0.5:8080/file2m.bin
This is ApacheBench, Version 2.3 <$Revision: 1843412 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 172.28.0.5 (be patient)
Finished 200 requests


Server Software:        SimpleHTTP/0.6
Server Hostname:        172.28.0.5
Server Port:            8080

Document Path:          /file2m.bin
Document Length:        2097152 bytes

Concurrency Level:      20
Time taken for tests:   0.844 seconds
Complete requests:      200
Failed requests:        0
Total transferred:      419471200 bytes
HTML transferred:       419430400 bytes
Requests per second:    236.94 [#/sec] (mean)
Time per request:       84.411 [ms] (mean)
Time per request:       4.221 [ms] (mean, across all concurrent requests)
Transfer rate:          485294.32 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.1      0       1
Processing:     9   25   6.6     23      52
Waiting:        0   21   6.1     20      45
Total:          9   25   6.6     23      52

Percentage of the requests served within a certain time (ms)
  50%     23
  66%     25
  75%     26
  80%     27
  90%     32
  95%     45
  98%     47
  99%     52
 100%     52 (longest request)

解读:大文件下 Document Length=2097152(2MiB)、Transfer rate=485294.32 Kbytes/s(约 473MB/s)反映内网传输能力强;单请求 Time per request(mean)=84.411ms 主要花在传体上,故小页面吞吐高、大文件吞吐低但传输速率高。压测大文件主要看 Transfer rate 与 Document Length,小页面主要看 Requests per second。

4.14 输出到 CSV 文件 —— -e filename(百分比-耗时表)

用途:-e filename 帮助原文:Output CSV file with percentages served,把百分位-响应时间关系写为 CSV,便于绘图/归档。

ab -n 100 -c 5 -e /tmp/ab-percent.csv http://172.28.0.5:8080/index.html
cat /tmp/ab-percent.csv
Percentage served,Time in ms
0,0.705
1,0.764
2,0.923
3,1.103
4,1.133
5,1.134
10,1.208
20,1.288
30,1.336
40,1.379
50,1.417
60,1.479
70,1.539
80,1.601
90,1.694
95,1.825
98,1.895
99,1.999
100,1.999

解读:CSV 首行为表头 Percentage served,Time in ms,其后每行「百分位,毫秒」,如 50% 请求在 1.417ms 内完成、99% 在 1.999ms 内完成。可配合 -g(输出 gnuplot 格式)或用 -w(HTML 表格输出,见 4.15)。

4.15 HTML 表格输出 —— -w

用途:-w 帮助原文:Print out results in HTML tables,把汇总表格输出为 HTML <table>,适合贴到报告里。

ab -w -n 20 -c 2 http://172.28.0.5:8080/index.html > /tmp/ab-report.html
wc -l /tmp/ab-report.html
29 /tmp/ab-report.html
head -22 /tmp/ab-report.html
<p>
 This is ApacheBench, Version 2.3 <i>&lt;$Revision: 1843412 $&gt;</i><br>
 Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/<br>
 Licensed to The Apache Software Foundation, http://www.apache.org/<br>
</p>
<p>
..done


<table >
<tr ><th colspan=2 bgcolor=white>Server Software:</th><td colspan=2 bgcolor=white>SimpleHTTP/0.6</td></tr>
<tr ><th colspan=2 bgcolor=white>Server Hostname:</th><td colspan=2 bgcolor=white>172.28.0.5</td></tr>
<tr ><th colspan=2 bgcolor=white>Server Port:</th><td colspan=2 bgcolor=white>8080</td></tr>
<tr ><th colspan=2 bgcolor=white>Document Path:</th><td colspan=2 bgcolor=white>/index.html</td></tr>
<tr ><th colspan=2 bgcolor=white>Document Length:</th><td colspan=2 bgcolor=white>19 bytes</td></tr>
<tr ><th colspan=2 bgcolor=white>Concurrency Level:</th><td colspan=2 bgcolor=white>2</td></tr>
<tr ><th colspan=2 bgcolor=white>Time taken for tests:</th><td colspan=2 bgcolor=white>0.007 seconds</td></tr>
<tr ><th colspan=2 bgcolor=white>Complete requests:</th><td colspan=2 bgcolor=white>20</td></tr>
<tr ><th colspan=2 bgcolor=white>Failed requests:</th><td colspan=2 bgcolor=white>0</td></tr>
<tr ><th colspan=2 bgcolor=white>Total transferred:</th><td colspan=2 bgcolor=white>4060 bytes</td></tr>
</html>

解读:输出主体是一张 HTML 表格(<table >),首部把版权信息与进度输出为 <p> 段落,随后以 <tr ><th colspan=2 bgcolor=white>字段名:</th> 逐行列出服务端信息与汇总,末行 </html> 收尾;-x/-y/-z 可分别给 <table>/<tr>/<td> 注入属性,-w -i 可组合生成 HEAD 请求的 HTML 报告。

五、帮助输出

5.1 ab -h(帮助全文,CentOS 8 / httpd-tools 2.4.37)

Usage: ab [options] [http[s]://]hostname[:port]/path
Options are:
    -n requests     Number of requests to perform
    -c concurrency  Number of multiple requests to make at a time
    -t timelimit    Seconds to max. to spend on benchmarking
                    This implies -n 50000
    -s timeout      Seconds to max. wait for each response
                    Default is 30 seconds
    -b windowsize   Size of TCP send/receive buffer, in bytes
    -B address      Address to bind to when making outgoing connections
    -p postfile     File containing data to POST. Remember also to set -T
    -u putfile      File containing data to PUT. Remember also to set -T
    -T content-type Content-type header to use for POST/PUT data, eg.
                    'application/x-www-form-urlencoded'
                    Default is 'text/plain'
    -v verbosity    How much troubleshooting info to print
    -w              Print out results in HTML tables
    -i              Use HEAD instead of GET
    -x attributes   String to insert as table attributes
    -y attributes   String to insert as tr attributes
    -z attributes   String to insert as td or th attributes
    -C attribute    Add cookie, eg. 'Apache=1234'. (repeatable)
    -H attribute    Add Arbitrary header line, eg. 'Accept-Encoding: gzip'
                    Inserted after all normal header lines. (repeatable)
    -A attribute    Add Basic WWW Authentication, the attributes
                    are a colon separated username and password.
    -P attribute    Add Basic Proxy Authentication, the attributes
                    are a colon separated username and password.
    -X proxy:port   Proxyserver and port number to use
    -V              Print version number and exit
    -k              Use HTTP KeepAlive feature
    -d              Do not show percentiles served table.
    -S              Do not show confidence estimators and warnings.
    -q              Do not show progress when doing more than 150 requests
    -l              Accept variable document length (use this for dynamic pages)
    -g filename     Output collected data to gnuplot format file.
    -e filename     Output CSV file with percentages served
    -r              Don't exit on socket receive errors.
    -m method       Method name
    -h              Display usage information (this message)
    -I              Disable TLS Server Name Indication (SNI) extension
    -Z ciphersuite  Specify SSL/TLS cipher suite (See openssl ciphers)
    -f protocol     Specify SSL/TLS protocol
                    (SSL2, TLS1, TLS1.1, TLS1.2 or ALL)
    -E certfile     Specify optional client certificate chain and private key

5.2 ab -V(版本行)

This is ApacheBench, Version 2.3 <$Revision: 1843412 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

💡 版本标识为 Version 2.3 <$Revision: 1843412 $>;$Revision 随发行版不同而异(CentOS 8 为 1843412,Ubuntu 22.04 为 1879490),此行的存在标志 HTTP server benchmarking tool 即 ab 本体。

六、常见问题

6.1 为什么 Failed requests 为 0 但实际都是错误响应?

Q:压测时看到 Failed requests: 0,但服务端其实一直返回错误页面? A:ab 把「HTTP 请求已发出并收到响应」都算作成功,Failed requests 只统计连接失败/接收异常的请求;HTTP 层面返回非 2xx(如 404、501、405)会计入 Non-2xx responses。判断成功必须同时检查 Failed requests(应为 0)与 Non-2xx responses(应为 0)。本环境 Python SimpleHTTP 不支持 POST/PUT,用 -p/-u 压测时即出现 Non-2xx responses=20 而 Failed requests=0。

6.2 目标不可达时报 apr_socket_recv: Connection refused (111)

Q:ab 直接以 apr_socket_recv: Connection refused (111) 退出、RC=111 是怎么回事? A:目标端口未监听/网络不可达。默认 ab 遇到 socket 接收错误即中止(退出码 111);需要「遇错继续」时加 -r(见 4.10),此时跑完 Complete requests、失败计入 Failed requests(Receive/Exceptions 分类)。

6.3 为什么 Requests per second 很小、Time per request 差异大?

Q:同样命令多次跑,Requests per second 波动大,且 Time per request 有差别? A:目标为单线程 Python http.server,高并发下请求在服务端排队,吞吐会明显下降。压测时要在目标服务大致稳定的环境下取多次均值,并在同样并发档位下比较;分析需同时看 Requests per second、Connection Times(尤其 Processing/Wait)与百分位表。

6.4 输出里出现 ERROR: The median and mean ... are more than twice the standard deviation apart. These results are NOT reliable?

Q:汇总里多了一行 ERROR: ...NOT reliable 是失败了吗? A:不是执行失败,而是 ab 的统计提示:当某耗时维度(如 Waiting/Processing/Total)的均值与中位数偏差超过两倍标准差时(常见于 -n/-c 较小或长尾明显),ab 认为该统计不可靠并打印提示。可增大 -n/-c、或关注百分位表而非单看均值来平滑波动。

6.5 为什么 -t 明明设了时长却没跑满?

Q:-t 10 但 Time taken for tests 只有几秒? A:-t 是最长时长(max. to spend),默认 -n 50000 兜底;若请求提前全部完成(如大文件很快发完 50000 次)则提前结束。需要严格跑满时长请用较大的 -n 或不设 -n 让兜底值生效,并观察 Finished X requests。

6.6 离线安装时 rpm -ivh 报缺 libapr-1.so.0 依赖

Q:离线机器上 rpm -ivh httpd-tools-2.4.37-39.module_el8.4.0+950+0577e6ac.1.x86_64.rpm 报 libapr-1.so.0()(64bit) is needed? A:httpd-tools 依赖 apr/apr-util 运行库,rpm -ivh 不解析依赖。改用 dnf install /opt/data/ab/offline/httpd-tools-2.4.37-39.module_el8.4.0+950+0577e6ac.1.x86_64.rpm(本地路径 + ./),让 dnf 自动解析并安装 apr、apr-util(见 3.2);完全断网环境需把 apr、apr-util、apr-util-bdb、apr-util-openssl 等 rpm 一并离线下载后同目录安装。

七、参考链接

CentOS 8 ab 安装与命令参数详解 - Quzuo