使用Apache Bench进行简单压测
Author: Rethink
Date: 2018/12/07
ApacheBench Version 2.3
Apache Bench(简称为ab)是 Apache 提供用于对Apache http server进行基准测试的工具。但是由于其安装和使用简单,所以也可以用于对HTTP接口的压力测试和性能测试。ab是一个命令行工具,使用ab命令可以模拟多线程并发请求,并且对负载机的要求很低,既不会占用很高CPU,也不会占用很多内存,但却会给目标服务器造成巨大的负载,其原理类似DDOS/CC攻击。
ab可以提供需要的基本性能指标;但是缺点就是没有图形化结果,不能监控。
Tips:在带宽不足的情况下,最好是本机进行测试,建议使用内网的另一台或者多台服务器通过内网进行测试,这样得出的数据,准确度会高很多。远程对web服务器进行压力测试,往往效果不理想(因为网络延时过大或带宽不足)
安装
Apache本身会自带ab,如果没有安装Apache,以下方法可以用来便捷的安装ab工具:
-
Ubuntu
apt-cache install apache2-util
-
CentOS
yum install httpd-tools
-
Windows
Windows下可以在官网下载最新版本 ,然后按照引导安装即可。
使用以上方法安装完成后,在已添加环境变量的情况下,可以直接使用ab -V
检查是否安装成功。
>ab -V
This is ApacheBench, Version 2.3 <$Revision: 1843412 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/</pre>
使用
Usage: ab [options] http://hostname[:port]/path
Options
ab -h
Options are:
-V 显示版本号并退出
-n requests 总请求数,默认值为1
-c concurrency 并发请求数,默认值为1,其值不能超过总请求数。建议是可以被总请求数整除,如 -n 100 –c 10模拟10个并发,每个并发10个请求。
-t timelimit 测试所用的时间上限(单位为秒),它可以使对服务器的测试限制在一个固定的总时间以内,其内部隐含的默认值等于 -n 50000
-s timeout 每次响应的最大等待时间(单位为秒),默认值为30s.注意s是小写的
-p postfile 用于存放发起post请求时的数据的文件地址,不要忘记设置请求头。文件内容的格式要视请求头决定。
-T content-type POST请求所使用的Content-type头信息,如 -T “application/x-www-form-urlencoded” ,默认值为“text/plain”
-H attribute 添加头部Header,需要注意custome-header的格式为 key:value,eg. User-Agent: ApacheBench/2.3', 这也是ab的默认值User-Agent
-C attribute 对请求附加Cookie,格式为 key=value ,注意和-H区分!
-m method 设置请求方法
-v verbosity 显示调试信息,verbosity为数字,可以看做是输出信息的级别。3或更大值可以显示响应代码(404, 200等), 2或更大值可以显示警告和其他信息。
-X proxy:port 设置代理地址以及端口信息
-w 以HTML表的格式输出结果。
-r 抛出异常继续执行测试任务
-i 使用HEAD请求代替GET
-b windowsize Size of TCP send/receive buffer, in bytes
-B address Address to bind to when making outgoing connections
-u putfile File containing data to PUT. Remember also to set -T
-x attributes String to insert as table attributes
-y attributes String to insert as tr attributes
-z attributes String to insert as td or th attributes
-A attribute Add Basic WWW Authentication, the attributes
are a colon separated username and password.
-P attribute Add Basic Proxy Authentication, the attributes
are a colon separated username and password.
-k Use HTTP KeepAlive feature
-d Do not show percentiles served table.
-S Do not show confidence estimators and warnings.
-q Do not show progress when doing more than 150 requests
-l Accept variable document length (use this for dynamic pages)
-g filename Output collected data to gnuplot format file.
-e filename Output CSV file with percentages served
-h Display usage information (this message)</pre>
Usage
> ab -n 10 -c 10 http://cnblogs.com/
# Get请求,要注意“&”是ab的保留运算符,使用时要用双引号引起来
> ab -n 10 -c 10 http://httpbin.org/get?name=rethink"&"age=3
# Post请求,post_data文件中的内容要和所指定的Content-Type对应(默认text/plain)
> ab -n 10 -c 10 -p E:\post_data1.txt -T application/x-www-form-urlencoded http://httpbin.org/post
> ab -n 10 -c 10 -p E:\post_data2.txt -T application/json http://httpbin.org/post
# post_data1中的数据为:name=rethink&age=3&method=post
# post_data1中的数据为:{"name":"Rethink","method":"post","age":3}
# 使用`-w`参数将结果以HTML的格式输出,然后将结果写入到本地文件中:
> ab -n 2 -c 2 -v 2 -w http://httpbin.org/get?name=rethink"&"age=3 > E:\report.html
注意事项:
-
result.html中会打印每次请求的请求头信息,请求总数较大时,重定向结果输出时可以不指定
-v
参数; -
使用
-H Content-Type:application/json
不能代替-T "application/json"
, 使用前者服务器可能会返回400 bad requests; -
如果提示
ab: invalid URL
,可能是URL最右边缺少/,例如 http://www.example.com 需要改为http://www.example.com/ ; -
不支持发送https请求;
-
postfile注意使用正确的编码格式,否则请求参数服务器端可能无法识别;
-
调试请求时,对接口返回的中文字符的支持不友好,会显示乱码;
结果分析
从上面可以看到ab支持参数很多,但一般来说只有-c
和-n
参数是必要的,例如:
>PS > ab -n 10 -c 10 http://httpbin.org/get?name=rethink"&"age=3
This is ApacheBench, Version 2.3 <$Revision: 1843412 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking cnblogs.com (be patient).....done
Server Software: gunicorn/19.9.0 服务器类型
Server Hostname: httpbin.org 域名
Server Port: 80 web端口
Document Path: /get?name=rethink&age=3 测试的页面路径
Document Length: 280 bytes 测试的页面大小
Concurrency Level: 10 测试的并发数
Time taken for tests: 2.629 seconds 整个测试活动持续的时间
Complete requests: 10 完成的请求数量(只包含状态码==200的请求)
Failed requests: 0 失败的请求数量(包含状态码!=200的请求和超时的请求)
Non-2xx responses: 10 状态码不等于2xx的响应数
Total transferred: 5210 bytes 整个过程中的网络传输量
HTML transferred: 2800 bytes 整个过程中的HTML内容传输量
Requests per second: 3.80 [/sec] (mean) 每秒的响应请求数(QPS),数值等于 请求总数(-n)/Time taken for tests,后面括号中的mean表示这是一个平均值
Time per request: 2629.486 [ms] (mean) 用户平均请求等待时间=concurrency * timetaken * 1000 / done
Time per request: 12.893 [ms] (mean, across all concurrent requests) 每个连接请求实际运行时间的平均值
Transfer rate: 25.15 [Kbytes/sec] received 传输速率
Connection Times (ms)
min mean[+/-sd] median max
Connect: 210 216 6.1 216 229
Processing: 229 1022 658.7 1081 2181
Waiting: 229 827 558.2 866 1731
Total: 445 1238 659.4 1291 2399
Percentage of the requests served within a certain time (ms)
50% 1291 整体响应时间的分布比
66% 1507
75% 1731
80% 1949
90% 2399 表示90%的请求在1291ms内得到响应
95% 2399
98% 2399
99% 2399
100% 2399 (longest request)