写在前面
简单介绍常用测试工具之 ApacheBench(ab).
安装 ApacheBench
在 Linux 系统上安装 ApacheBench(ab)工具:
yum -y install httpd-tools
验证安装
安装完成后,验证 ab 工具是否可用:
ab -V
输出示例:
This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
参数说明
查看 ab 命令的帮助文档:
ab --help
常用参数说明:
-n
:测试会话中执行的请求总数。-c
:并发请求数,即同时发送的请求数量。-t
:测试的最大持续时间(秒)。-p
:包含 POST 数据的文件路径。-T
:POST 数据的 Content-Type 头信息。-v
:设置输出信息的详细程度(数值越大越详细)。-k
:启用 HTTP KeepAlive 功能。-H
:附加额外的 HTTP 头信息。
性能指标
测试结果中需要注意以下关键指标:
吞吐率(Requests per second)
表示服务器每秒处理的请求数,计算公式为:
[ \text{Requests per second} = \frac{\text{Complete requests}}{\text{Time taken for tests}} ]
数值越大,服务器性能越好。
并发连接数(The number of concurrent connections)
表示服务器同时处理的请求数量。
用户平均请求等待时间(Time per request)
计算公式为:
[ \text{Time per request} = \frac{\text{Time taken for tests}}{\text{Complete requests / Concurrency Level}} ]
服务器平均请求等待时间
计算公式为:
[ \text{Time per request (across all concurrent requests)} = \frac{\text{Time taken for tests}}{\text{Complete requests}} ]
它是吞吐率的倒数。
实际使用示例
测试一个 URL 的性能,模拟 10 个并发用户,共发送 100 个请求:
ab -c 10 -n 100 http://www.example.com/index.php
-c 10
:并发用户数为 10。-n 100
:总请求数为 100。http://www.example.com/index.php
:目标测试 URL。
注意事项
- 测试目标服务器应具备足够的资源,避免因测试导致服务不可用。
- 高并发测试可能对服务器产生较大压力,建议在非生产环境进行。
- 结合其他监控工具(如
top
、vmstat
)分析服务器资源使用情况。
The end.