安装
1.
mac 系统上会携带ab工具
2.
centos上,如果没有的话,使用以下命令安装:
sudo yum -y install httpd-tools
3.
ubuntu安装:
sudo apt-get install apache2-utils
操作手册
ab --help
| 参数 | 含义 | 解释 |
| -n | requests | 总请求数 |
| -c | concurrency | 并发数 |
| -t | timelimit | 测试执行最大秒数,默认值为50000 |
| -s | timeout | 请求最大等待时长,默认30s |
| -b | windowsize | TCP发送/接收的缓冲大小(单位:字节) |
| -B | address | Address to bind to when making outgoing connections |
| -p | postfile | 发送POST请求时需要上传的文件,文件格式如"a=1&b=2"或者json格式。使用方法是 -p test.json; 配合-T |
| -u | putfile | 发送PUT请求时需要上传的文件。配合-T |
| -T | content-type | 设置Content-Type;如:-T "application/json" 默认为text/plain; 配合-p |
| -v | verbosity | 打印更多的信息 |
| -w | 以HTML表格形式打印结果 | |
| -i | 使用HEAD请求代替GET请求 | |
| -x | attributes | 插入字符串作为table标签的属性 |
| -y | attributes | 插入字符串作为tr标签的属性 |
| -z | attributes | 插入字符串作为td标签的属性 |
| -C | attribute | 添加cookie信息,如:'Apache=1234', 可重复添加 |
| -H | attribute | Add Arbitrary header line, eg. 'Accept-Encoding: gzip' Inserted after all normal header lines. (repeatable) |
| -A | attribute | 添加一个基本的网络认证信息,用户名和密码之间用英文冒号隔开 |
| -P | attribute | 添加一个基本的代理认证信息,用户名和密码之间用英文冒号隔开。如-P proxy-auth-username:password |
| -X | proxy:port | 指定使用的代理服务器和端口号,例如:"126.10.10.3:88"。 |
| -V | 显示版本号并退出。 | |
| -k | 使用HTTP的KeepAlive特性 | |
| -d | 不显示百分比。 | |
| -S | 不显示预估和警告信息 | |
| -q | 超过150个请求后不显示进度 | |
| -l | 接受可变文档长度(用于动态页面) | |
| -g | filename | filename 输出结果信息到gnuplot格式的文件中 |
| -e | filename | filename 输出结果信息到CSV格式的文件中 |
| -r | filename 输出结果信息到CSV格式的文件中 | |
| -m | method | method 方法名 |
| -h | method 方法名 | |
| -I | 禁用 TLS 服务器名称指示扩展名 | |
| -Z | ciphersuite | 指定 SSL/TLS 密码套件 |
| -f | protocol | 指定 SSL/TLS 协议 |
基本用法
•ab通过content length来确定请求是否成功,如果后面的length与之前不一样,就认为是失败; •测试过程中,先固定-n 数量,通过调整 -c,去测试服务器的最大qps•固定-c, 调整 -n 去测试服务器稳定性
1. GET方法
-m:请求方法;-n: 总共请求数 ;-c: 并发数;-C: 携带的cookie;-H: 携带的header;
ab -m GET -n 1000 -c 100 -C 'token=abcd' -H 'Accept-Encoding: gzip' http://www.test.com/
2.POST方法
-T:Content-type 类型,用在post、put方法上 ;-p: post请求方法的文件地址,需要与-T一起使用;
ab -T 'application/json' -p ./test.json -n 1000 -c 100 http://www.test.com/
test.json文件
{ "key": "value"}
结果分析
| 名字 | 含义 |
| Server Software | web服务器软件及版本 |
| Server Hostname | 请求的地址 |
| Server Port | 请求的端口 |
| Document Path | 请求的页面路径 |
| Document Length | 页面大小 |
| Concurrency Level | 并发数 |
| Time taken for tests | 测试总共花费的时间 |
| Complete requests | 完成的请求数 |
| Failed requests | 失败的请求数 |
| Write errors | 写入错误 |
| Total transferred | 总共传输字节数,包含http的头信息等 |
| HTML transferred | html字节数,实际的页面传递字节数 |
| Requests per second | 每秒处理的请求数,服务器的吞吐量(重要) |
| Time per request | 平均数,用户平均请求等待时间 |
| Time per request (across all concurrent requests) | 服务器平均处理时间 |
| Transfer rate | 平均传输速率(每秒收到的速率) |
注意⚠️
•ab: Cannot use concurrency level greater than total number of requests 并发数不能大于请求数 •Too many open files in system () 并发数太多,通过 ulimit -n 修改•Connection reset by peer 超过服务器允许的并发量
•Invalid Concurrency [Range 0..20000] ab默认并发数不能超过20000