FreezeJ' Blog

http压测命令ab

2023-05-05

介绍

ab 是一个用于测试 HTTP 服务器性能的命令行工具,也叫做Apache Benchmark。它能够向服务器发送大量的请求,并统计服务器返回的响应时间和吞吐量等性能指标。

常用参数

-n: 发送请求的总次数。
-c: 并发请求数量。
-t: 测试的最大时间长度,单位为秒。
-k: 允许HTTP KeepAlive。
-H: 设置 HTTP 请求头部信息,例如 -H ‘User-Agent: Mozilla/5.0’ 。
-p: POST 请求时发送的数据文件路径,例如 -p /path/to/post-data.txt 。
-T: 设置请求的 Content-Type,例如 -T ‘application/json’。
-C: 启用 cookie,例如 -C ‘name=value’。

应用例子

发送 1000 个请求,10 个并发请求:
ab -n 1000 -c 10 http://example.com/

发送 1000 个请求,最大测试时间为 30 秒,10 个并发请求:
ab -n 1000 -c 10 -t 30 http://example.com/

发送 1000 个请求,10 个并发请求,启用 KeepAlive:
ab -n 1000 -c 10 -k http://example.com/

发送 1000 个请求,10 个并发请求,设置请求头:
ab -n 1000 -c 10 -H 'User-Agent: Mozilla/5.0' http://example.com/

发送 POST 请求,数据从文件中读取:
ab -n 100 -c 10 -p /path/to/post-data.txt -T 'application/json' http://example.com/

启用 Cookie:
ab -n 1000 -c 10 -C 'name=value' http://example.com/