ab测试简单易用。
只需要一条命令即可完成性能测试要求,使用简单,结果清晰。特别适合快速需要相关性能指标的情况。一般关注tps和时延。
再结合其他资源监控工具,比如nmon监控当前系统资源。
ab -c 1000 -n 500000 "http://192.168.142.75:10000/xs_api/video/search?fetchNum=5&ip=192.168.142.145&startIndex=1&location=1&keyword=%E5%BF%8D%E8%80%85%E7%A5%9E%E9%BE%9F"
输入ab 即可查看到。相关参数配置一大堆,摘录如下。
ab [可选的参数选项] 需要进行压力测试的url
此外,我们再根据上面的用法介绍界面来详细了解每个参数选项的作用。
-
-n
- 即requests,用于指定压力测试总共的执行次数。 -c
- 即concurrency,用于指定 压力测试的并发数。 -t
- 即timelimit,等待响应的最大时间(单位:秒)。 -b
- 即windowsize,TCP发送/接收的缓冲大小(单位:字节)。 -p
-
即postfile,发送POST请求时需要上传的文件,此外还必须设置
-T
参数。
-u
-
即putfile,发送PUT请求时需要上传的文件,此外还必须设置
-T
参数。
-T
-
即content-type,用于设置Content-Type请求头信息,例如:
application/x-www-form-urlencoded
,默认值为text/plain
。
-v
- 即verbosity,指定打印帮助信息的冗余级别。 -w
- 以HTML表格形式打印结果。 -i
- 使用HEAD请求代替GET请求。 -x
- 插入字符串作为table标签的属性。 -y
- 插入字符串作为tr标签的属性。 -z
- 插入字符串作为td标签的属性。 -C
- 添加cookie信息,例如:"Apache=1234"(可以重复该参数选项以添加多个)。 -H
- 添加任意的请求头,例如:"Accept-Encoding: gzip",请求头将会添加在现有的多个请求头之后(可以重复该参数选项以添加多个)。 -A
- 添加一个基本的网络认证信息,用户名和密码之间用英文冒号隔开。 -P
- 添加一个基本的代理认证信息,用户名和密码之间用英文冒号隔开。 -X
- 指定使用的 代理服务器和端口号,例如:"126.10.10.3:88"。 -V
- 打印版本号并退出。 -k
- 使用HTTP的KeepAlive特性。 -k
- 使用HTTP的KeepAlive特性。 -d
- 不显示百分比。 -S
- 不显示预估和警告信息。 -g
- 输出结果信息到gnuplot格式的文件中。 -e
- 输出结果信息到CSV格式的文件中。 -r
- 指定接收到错误信息时不退出程序。 -h
Server Software: Apache/2.2.25 (服务器软件名称及版本信息)
Server Hostname: localhost (服务器主机名)
Server Port: 80 (服务器端口)
Document Path: /index.php (供测试的URL路径) Document Length: 10 bytes (供测试的URL返回的文档大小) Concurrency Level: 100 (并发数) Time taken for tests: 0.247 seconds (压力测试消耗的总时间) Complete requests: 1000 (压力测试的总次数) Failed requests: 0 (失败的请求数) Write errors: 0 (网络连接写入错误数) Total transferred: 198000 bytes (传输的总数据量) HTML transferred: 10000 bytes (HTML文档的总数据量) Requests per second: 4048.34 [#/sec] (mean) (平均每秒的请求数) Time per request: 24.701 [ms] (mean) (所有并发用户(这里是100)都请求一次的平均时间) Time per request: 0.247 [ms] (mean, across all concurrent requests) (单个用户请求一次的平均时间) Transfer rate: 782.78 [Kbytes/sec] received (传输速率,单位:KB/s)
本次测试结果,tps 4908,时延203。
有时候还会结合其他指标。
Failed requests 数量
最大时延。
root@ubuntu:/opt/test# ab -c 1000 -n 500000 "http://192.168.142.75:10000/xs_api/video/search?fetchNum=5&ip=192.168.142.145&startIndex=1&location=1&keyword=%E5%BF%8D%E8%80%85%E7%A5%9E%E9%BE%9F"
This is ApacheBench, Version 2.3 <$Revision: 1706008 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking 192.168.142.75 (be patient)
Completed 50000 requests
Completed 100000 requests
Completed 150000 requests
Completed 200000 requests
Completed 250000 requests
Completed 300000 requests
Completed 350000 requests
Completed 400000 requests
Completed 450000 requests
Completed 500000 requests
Finished 500000 requests
Server Software:
Server Hostname: 192.168.142.75
Server Port: 10000
Document Path: /xs_api/video/search?fetchNum=5&ip=192.168.142.145&startIndex=1&location=1&keyword=%E5%BF%8D%E8%80%85%E7%A5%9E%E9%BE%9F
Document Length: 1546 bytes
Concurrency Level: 1000
Time taken for tests: 101.859 seconds
Complete requests: 500000
Failed requests: 0
Total transferred: 844000000 bytes
HTML transferred: 773000000 bytes
Requests per second: 4908.74 [#/sec] (mean)
Time per request: 203.718 [ms] (mean)
Time per request: 0.204 [ms] (mean, across all concurrent requests)
Transfer rate: 8091.75 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 4.1 0 999
Processing: 7 203 124.6 186 2365
Waiting: 7 203 124.6 186 2365
Total: 7 203 125.1 186 2383
Percentage of the requests served within a certain time (ms)
50% 186
66% 229
75% 261
80% 283
90% 349
95% 412
98% 495
99% 563
100% 2383 (longest request)
本地文件句柄数限制。
ab -c 2000 时候,会报 open file 过大,此时需要设置。
ulimit -a 查看默认为pen files (-n) 1024,
ulimit -n 65535 设置大些。