Apache Bench(ab)是一个用于基准测试HTTP Web服务器的命令行工具,广泛用于评估和优化Web服务器的性能。以下是关于Apache Bench的详细介绍,包括其功能、使用方法、常用参数和输出结果解析。
功能
-
性能测试:通过模拟多个并发用户访问服务器,ab可以测量服务器的吞吐量、响应时间等性能指标。
-
简单易用:ab是一个非常简单的工具,只需在命令行中输入几个参数即可运行。
-
支持多种HTTP方法:ab支持GET、POST等HTTP方法,可以模拟不同的请求类型。
安装
查是否安装了ab
bash
ab -V

常用参数
-
-n
:指定请求的总数。 -
-c
:指定并发请求的数量。 -
-t
:指定测试的总时间(秒)。 -
-p
:指定包含POST数据的文件。 -
-T
:指定Content-Type头信息。 -
-H
:添加自定义头信息。 -
-C
:添加Cookie信息。 -
-k
:启用HTTP KeepAlive功能,即在一次TCP连接中发送多个请求。
示例命令
基本请求
bash
ab -n 100 -c 10 http://www.baidu.com/
这个命令会向http://www.baidu.com/
发送100个请求,其中10个请求是并发的。


POST请求
bash
ab -n 50 -c 5 -p post_demo_data.txt -T application/x-www-form-urlencoded http://www.baidu.com/
这个命令会向http://www.baidu.com/
发送50个POST请求,其中5个请求是并发的,POST数据从post_demo_data.txt
文件中读取。


带自定义头信息的请求
bash
ab -n 50 -c 5 -H "Authorization: Bearer token123456789456123" http://www.baidu.com/
这个命令会向http://www.baidu.com/
发送50个请求,其中5个请求是并发的,并添加了自定义的Authorization头信息。

输出结果解析
ab的输出结果包括以下信息:
-
Server Software:服务器软件信息。
-
Server Hostname:服务器主机名。
-
Server Port:服务器端口。
-
Document Path:请求的文档路径。
-
Document Length:文档长度(字节)。
-
Concurrency Level:并发级别,即并发请求的数量。
-
Time taken for tests:测试总时间(秒)。
-
Complete requests:完成的请求总数。
-
Failed requests:失败的请求总数。
-
Total transferred:总传输字节数。
-
HTML transferred:HTML文档传输字节数。
-
Requests per second:每秒请求数(吞吐量)。
-
Time per request:每个请求的平均响应时间(毫秒)。
-
Time per request (across all concurrent requests):所有并发请求的平均响应时间(毫秒)。
-
Transfer rate:传输速率(Kbytes/sec)。
-
Percentage of the requests served within a certain time (ms):请求在一定时间内完成的百分比。
