Ubuntu 22.04 ab 安装与命令参数详解

在 Ubuntu 22.04 上安装 apache2-utils 的 ab 2.3 并逐参数实测 HTTP 压测命令输入与回显,涵盖 URL 目标及 -n/-c/-t/-k/-q/-v/-H/-T/-p/-u/-C/-r/-s/-i/-w/-e 等参数及离线 .deb 安装。

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

Ubuntu 22.04 ab 安装与命令参数详解

目标:在 Ubuntu 22.04 上安装 Apache HTTP 服务器工具包 apache2-utils 自带的 HTTP 压测工具 ab(ApacheBench 2.3),并逐参数实测压测命令的输入与输出,涵盖 URL 目标、-n、-c、-t、-k、-q、-v、-H、-T、-p、-u、-C、-r、-s、-i、-w、-e 等参数,以及在线/离线安装。本文命令与输出均为 Ubuntu 22.04(ab 2.3,包 apache2-utils 2.4.52-1ubuntu4.24)对目标站 http://172.28.0.4:8080/ 实测回显。

一、环境信息

  • 操作系统:Ubuntu 22.04 LTS
  • ab 版本:ApacheBench Version 2.3(<$Revision: 1879490 $>)
  • 安装包:apache2-utils 2.4.52-1ubuntu4.24(提供 ab 等 HTTP 工具)
  • 压测客户端主机:本机,网卡 eth0,IP 172.28.0.2/16
  • 目标站点:172.28.0.4:8080(HTTP 服务,供压测)
  • 目标服务类型:Python http.server(SimpleHTTP,Python 3.10.12)
  • 测试文档:/index.html(19 字节)、/file1m.bin(1 MiB)
  • 工作目录:/opt/data/ab/

二、工具概述

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

三、安装

3.1 在线安装(apt)

sudo apt update
sudo apt install -y apache2-utils

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

ab -V
This is ApacheBench, Version 2.3 <$Revision: 1879490 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
dpkg -l apache2-utils
Desired=Unknown/Install/Remove/Purge/Hold
| Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend
|/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)
||/ Name           Version            Architecture Description
+++-==============-==================-============-=====================================================
ii  apache2-utils  2.4.52-1ubuntu4.24 amd64        Apache HTTP Server (utility programs for web servers)

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

3.2 离线安装(.deb 包)

在有网络的 Ubuntu 机器上先下载所需的 .deb 包,再拷贝到离线机器安装:

cd /opt/data/ab/offline
apt-get download apache2-utils
Get:1 http://archive.ubuntu.com/ubuntu jammy-updates/main amd64 apache2-utils amd64 2.4.52-1ubuntu4.24 [90.2 kB]
Fetched 90.2 kB in 1s (61.3 kB/s)
ls -la /opt/data/ab/offline/*.deb
-rw-r--r-- 1 root root 90222 Oct  7 18:48 /opt/data/ab/offline/apache2-utils_2.4.52-1ubuntu4.24_amd64.deb

将 .deb 拷贝到离线机器后,用 dpkg -i 安装(离线机器已具备 libapr1、libaprutil1 等依赖时直接成功):

sudo dpkg -i /opt/data/ab/offline/apache2-utils_2.4.52-1ubuntu4.24_amd64.deb
(Reading database ... 4427 files and directories currently installed.)
Preparing to unpack .../apache2-utils_2.4.52-1ubuntu4.24_amd64.deb ...
Unpacking apache2-utils (2.4.52-1ubuntu4.24) ...
Setting up apache2-utils (2.4.52-1ubuntu4.24) ...

⚠️ 若 dpkg -i 因缺少 libapr1/libaprutil1 报依赖错误,先用 apt-get download libapr1 libaprutil1 一并下载,再用 dpkg -i *.deb 一次性安装;或缺依赖时用 sudo apt install -y -f 自动补齐再复核 ab -V。

四、命令实测

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

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

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

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

ab -n 1000 -c 50 http://172.28.0.4:8080/index.html
This is ApacheBench, Version 2.3 <$Revision: 1879490 $>
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.4 (be patient)
Finished 1000 requests


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

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

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

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0   45 210.0      0    1042
Processing:     3  113 830.3      8   13250
Waiting:        1  111 830.4      6   13250
Total:          3  158 968.8      8   14274

Percentage of the requests served within a certain time (ms)
  50%      8
  66%      9
  75%      9
  80%     10
  90%     12
  95%     16
  98%   2692
  99%   4360
 100%  14274 (longest request)

解读:Concurrency Level=50、Complete requests=1000、Failed requests=0 表示全部成功;吞吐 Requests per second=70.05 请求/秒;两行 Time per request 分别是在并发下的平均耗时(713.817ms)和按单请求均摊的耗时(14.276ms,= Time taken / Complete requests);Connection Times 给出连接/处理/等待/总耗时五档统计;末尾的百分位段(Percentage of the requests served...)是响应时间分布,如 99% 请求在 4360ms 内完成。Transfer rate=14.02 Kbytes/s 为有效接收速率。

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

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

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

ab -t 5 -c 10 http://172.28.0.4:8080/index.html
This is ApacheBench, Version 2.3 <$Revision: 1879490 $>
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.4 (be patient)
Finished 4178 requests


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

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

Concurrency Level:      10
Time taken for tests:   5.003 seconds
Complete requests:      4178
Failed requests:        0
Total transferred:      857253 bytes
HTML transferred:       79401 bytes
Requests per second:    835.16 [#/sec] (mean)
Time per request:       11.974 [ms] (mean)
Time per request:       1.197 [ms] (mean, across all concurrent requests)
Transfer rate:          167.35 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    4  64.0      0    1064
Processing:     1    7   2.8      7      32
Waiting:        1    6   2.5      5      26
Total:          1   11  64.2      7    1072

Percentage of the requests served within a certain time (ms)
  50%      7
  66%      8
  75%      9
  80%      9
  90%     11
  95%     13
  98%     16
  99%     19
 100%   1072 (longest request)

解读:Time taken for tests=5.003 秒即到时长自动停止;5 秒内完成 4178 次请求,Requests per second=835.16,单位吞吐(每请求均耗时 1.197ms)显著高于 4.1 的高并发场景,反映并发过高时单机 Python 服务端排队导致的吞吐下降。

4.3 开启 Keep-Alive 复用连接 —— ab -k -n 2000 -c 20 http://172.28.0.4: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.4:8080/index.html
This is ApacheBench, Version 2.3 <$Revision: 1879490 $>
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.4 (be patient)
Finished 2000 requests


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

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

Concurrency Level:      20
Time taken for tests:   2.474 seconds
Complete requests:      2000
Failed requests:        0
Keep-Alive requests:    0
Total transferred:      410000 bytes
HTML transferred:       38000 bytes
Requests per second:    808.36 [#/sec] (mean)
Time per request:       24.741 [ms] (mean)
Time per request:       1.237 [ms] (mean, across all concurrent requests)
Transfer rate:          161.83 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0   12 114.8      0    2051
Processing:     1    9  29.4      7     830
Waiting:        1    8  29.4      5     830
Total:          1   21 135.5      7    2055

Percentage of the requests served within a certain time (ms)
  50%      7
  66%      8
  75%      8
  80%      9
  90%     11
  95%     14
  98%     20
  99%   1027
 100%   2055 (longest request)

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

4.4 静默模式(大于 150 请求不再打进度) —— ab -q -n 3000 -c 30 http://172.28.0.4: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.4:8080/index.html
This is ApacheBench, Version 2.3 <$Revision: 1879490 $>
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.4 (be patient).....done


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

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

Concurrency Level:      30
Time taken for tests:   7.646 seconds
Complete requests:      3000
Failed requests:        0
Total transferred:      615000 bytes
HTML transferred:       57000 bytes
Requests per second:    392.38 [#/sec] (mean)
Time per request:       76.456 [ms] (mean)
Time per request:       2.549 [ms] (mean, across all concurrent requests)
Transfer rate:          78.55 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0   15 121.9      0    1063
Processing:     0   23 238.3      7    6594
Waiting:        0   22 238.3      6    6593
Total:          1   38 321.5      7    7642

Percentage of the requests served within a certain time (ms)
  50%      7
  66%      8
  75%      9
  80%     10
  90%     12
  95%     14
  98%     18
  99%   1053
 100%   7642 (longest request)

解读:加上 -q 后 3000 个请求中间不再输出 Completed x00 requests 进度,只在开始时打印 .....done(-q 下请求数量虽超过 150 但进度被取消,仅剩结束标记),随后直接进入汇总。

4.5 详细级别——打印请求头与响应头 —— ab -v 4 -n 2 -c 1 -H "X-Auth: token123" -C "session=pmm" http://172.28.0.4: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.4:8080/index.html
This is ApacheBench, Version 2.3 <$Revision: 1879490 $>
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.4 (be patient)...INFO: GET header ==
---
GET /index.html HTTP/1.0
Cookie: session=pmm
X-Auth: token123
Host: 172.28.0.4:8080
User-Agent: ApacheBench/2.3
Accept: */*


---
LOG: header received:
HTTP/1.0 200 OK
Server: SimpleHTTP/0.6 Python/3.10.12
Date: Fri, 09 Oct 2026 04:35:41 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.10.12
Date: Fri, 09 Oct 2026 04:35:41 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.4
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:      410 bytes
HTML transferred:       38 bytes
Requests per second:    882.22 [#/sec] (mean)
Time per request:       1.133 [ms] (mean)
Time per request:       1.133 [ms] (mean, across all concurrent requests)
Transfer rate:          176.62 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.1      0       0
Processing:     1    1   0.0      1       1
Waiting:        1    1   0.0      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.10.12 等)。-v 4 只作少量请求(-n 2 -c 1)调试用,量大时不要这么用。

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

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

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

ab -n 20 -c 2 -H "X-Test-Header: ab-demo" http://172.28.0.4:8080/index.html
This is ApacheBench, Version 2.3 <$Revision: 1879490 $>
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.4 (be patient).....done


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

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

Concurrency Level:      2
Time taken for tests:   0.024 seconds
Complete requests:      20
Failed requests:        0
Total transferred:      4100 bytes
HTML transferred:       380 bytes
Requests per second:    838.93 [#/sec] (mean)
Time per request:       2.384 [ms] (mean)
Time per request:       1.192 [ms] (mean, across all concurrent requests)
Transfer rate:          167.95 [Kbytes/sec] received

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

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

解读:小并发(-c 2)+ 少量请求(-n 20)时吞吐 838.93 请求/秒,Total transferred 4100 字节 = 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.4:8080/
This is ApacheBench, Version 2.3 <$Revision: 1879490 $>
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.4 (be patient).....done


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

Document Path:          /
Document Length:        497 bytes

Concurrency Level:      2
Time taken for tests:   0.018 seconds
Complete requests:      20
Failed requests:        0
Non-2xx responses:      20
Total transferred:      13920 bytes
Total body sent:        3380
HTML transferred:       9940 bytes
Requests per second:    1131.03 [#/sec] (mean)
Time per request:       1.768 [ms] (mean)
Time per request:       0.884 [ms] (mean, across all concurrent requests)
Transfer rate:          768.75 [Kbytes/sec] received
                        186.66 kb/s sent
                        955.41 kb/s total

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.1      0       0
Processing:     1    2   0.5      1       3
Waiting:        0    1   0.5      1       3
Total:          1    2   0.4      2       3
ERROR: The median and mean for the processing 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%      3 (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.4:8080/
This is ApacheBench, Version 2.3 <$Revision: 1879490 $>
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.4 (be patient).....done


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

Document Path:          /
Document Length:        496 bytes

Concurrency Level:      2
Time taken for tests:   0.019 seconds
Complete requests:      20
Failed requests:        0
Non-2xx responses:      20
Total transferred:      13880 bytes
Total body sent:        2880
HTML transferred:       9920 bytes
Requests per second:    1080.38 [#/sec] (mean)
Time per request:       1.851 [ms] (mean)
Time per request:       0.926 [ms] (mean, across all concurrent requests)
Transfer rate:          732.21 [Kbytes/sec] received
                        151.93 kb/s sent
                        884.14 kb/s total

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

Percentage of the requests served within a certain time (ms)
  100%      2 (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.4:8080/index.html
This is ApacheBench, Version 2.3 <$Revision: 1879490 $>
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.4 (be patient).....done


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

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

Concurrency Level:      2
Time taken for tests:   0.025 seconds
Complete requests:      20
Failed requests:        0
Total transferred:      4100 bytes
HTML transferred:       380 bytes
Requests per second:    806.45 [#/sec] (mean)
Time per request:       2.480 [ms] (mean)
Time per request:       1.240 [ms] (mean, across all concurrent requests)
Transfer rate:          161.47 [Kbytes/sec] received

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

Percentage of the requests served within a certain time (ms)
  50%      2
  66%      2
  75%      2
  80%      2
  90%      3
  95%      4
  98%      4
  99%      4
 100%      4 (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.4:9999/
This is ApacheBench, Version 2.3 <$Revision: 1879490 $>
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.4 (be patient)...apr_socket_recv: Connection refused (111)

进程退出码为 111:

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

加 -r 后继续完成:

ab -r -n 10 -c 2 http://172.28.0.4:9999/
This is ApacheBench, Version 2.3 <$Revision: 1879490 $>
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.4 (be patient).....done


Server Software:
Server Hostname:        172.28.0.4
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:    24570.02 [#/sec] (mean)
Time per request:       0.081 [ms] (mean)
Time per request:       0.041 [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.4:8080/index.html
This is ApacheBench, Version 2.3 <$Revision: 1879490 $>
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.4 (be patient)
Finished 500 requests


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

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

Concurrency Level:      10
Time taken for tests:   0.558 seconds
Complete requests:      500
Failed requests:        0
Total transferred:      102500 bytes
HTML transferred:       9500 bytes
Requests per second:    895.68 [#/sec] (mean)
Time per request:       11.165 [ms] (mean)
Time per request:       1.116 [ms] (mean, across all concurrent requests)
Transfer rate:          179.31 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.0      0       1
Processing:     1    7   2.0      6      14
Waiting:        1    5   1.7      5      12
Total:          2    7   2.0      6      14

Percentage of the requests served within a certain time (ms)
  50%      6
  66%      7
  75%      8
  80%      8
  90%      9
  95%     10
  98%     12
  99%     12
 100%     14 (longest request)

解读:目标响应约 1~14ms,远低于 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.4:8080/index.html
This is ApacheBench, Version 2.3 <$Revision: 1879490 $>
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.4 (be patient).....done


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

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

Concurrency Level:      5
Time taken for tests:   0.062 seconds
Complete requests:      50
Failed requests:        0
Total transferred:      9300 bytes
HTML transferred:       0 bytes
Requests per second:    809.73 [#/sec] (mean)
Time per request:       6.175 [ms] (mean)
Time per request:       1.235 [ms] (mean, across all concurrent requests)
Transfer rate:          147.08 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.1      0       0
Processing:     1    6   1.9      6       9
Waiting:        1    5   1.8      5       9
Total:          1    6   1.8      6      10

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

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

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

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

ab -n 200 -c 20 http://172.28.0.4:8080/file1m.bin
This is ApacheBench, Version 2.3 <$Revision: 1879490 $>
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.4 (be patient)
Finished 200 requests


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

Document Path:          /file1m.bin
Document Length:        1048576 bytes

Concurrency Level:      20
Time taken for tests:   0.491 seconds
Complete requests:      200
Failed requests:        0
Total transferred:      209756400 bytes
HTML transferred:       209715200 bytes
Requests per second:    407.52 [#/sec] (mean)
Time per request:       49.078 [ms] (mean)
Time per request:       2.454 [ms] (mean, across all concurrent requests)
Transfer rate:          417381.15 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.4      0       5
Processing:     4   18   8.1     17      49
Waiting:        1    9   4.8      8      28
Total:          5   18   8.2     17      49

Percentage of the requests served within a certain time (ms)
  50%     17
  66%     20
  75%     23
  80%     25
  90%     29
  95%     34
  98%     43
  99%     47
 100%     49 (longest request)

解读:大文件下 Document Length=1048576(1MiB)、Transfer rate=417381.15 Kbytes/s(约 407MB/s)反映内网传输能力强;单请求 Time per request(mean)=49.078ms 主要花在传体上,故小页面吞吐高、大文件吞吐低但传输速率高。压测大文件主要看 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.4:8080/index.html
cat /tmp/ab-percent.csv
Percentage served,Time in ms
0,1.476
1,2.453
2,2.587
3,2.929
4,3.006
5,3.164
10,3.576
20,4.089
30,4.708
40,5.041
50,5.510
60,6.014
70,6.324
80,6.669
90,7.723
95,8.125
98,9.568
99,10.028
100,10.028

解读:CSV 首行为表头 Percentage served,Time in ms,其后每行「百分位,毫秒」,如 50% 请求在 5.510ms 内完成、99% 在 10.028ms 内完成。可配合 -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.4:8080/index.html > /tmp/ab-report.html
wc -l /tmp/ab-report.html
105 /tmp/ab-report.html
head -20 /tmp/ab-report.html
This is ApacheBench, Version 2.3 <$Revision: 1879490 $>
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.4 (be patient).....done
<HTML>
<HEAD>
<TITLE>Benchmark Report</TITLE>
</HEAD>
<BODY>
<H1>Benchmark Report</H1>
<TABLE BORDER=1>
<TR><TH COLSPAN=2>Benchmarking 172.28.0.4 (be patient)&nbsp;done</TH></TR>
<TR><TH COLSPAN=2>Server Software: SimpleHTTP/0.6</TH></TR>
<TR><TH COLSPAN=2>Server Hostname: 172.28.0.4</TH></TR>
<TR><TH COLSPAN=2>Server Port: 8080</TH></TR>
<TR><TH COLSPAN=2>Document Path: /index.html</TH></TR>
<TR><TH COLSPAN=2>Document Length: 19 bytes</TH></TR>

解读:输出主体是一张 HTML 表格(<TABLE BORDER=1>),首行标题 Benchmark Report,以 <TR><TH COLSPAN=2> 逐行列出服务端信息与汇总;-x/-y/-z 可分别给 <table>/<tr>/<td> 注入属性,-w -i 可组合生成 HEAD 请求的 HTML 报告。

五、帮助输出

5.1 ab -h(帮助全文,Ubuntu 22.04 / apache2-utils 2.4.52)

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: 1879490 $>
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: 1879490 $>;$Revision 随发行版不同而异(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,高并发下请求在服务端排队,吞吐会明显下降(见 4.1 的 70 请求/秒 vs 4.2 低并发的 835 请求/秒)。压测时要在目标服务大致稳定的环境下取多次均值,并在同样并发档位下比较;分析需同时看 Requests per second、Connection Times(尤其 Processing/Wait)与百分位表。

6.4 输出里出现 ERROR: ... NOT reliable 的统计提示是失败了吗?

Q:汇总里多了一行 ERROR: 开头的统计提示(The median and mean ... are more than twice the standard deviation apart. These results are NOT reliable)是失败了吗? A:不是执行失败,而是 ab 的统计提示:当某耗时维度(如 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。

七、参考链接