ApacheBench 是 Apache 服务器自带的一个web压力测试工具,简称ab。ab命令会创建多个并发访问线程,模拟多个访问者同时对某一URL地址进行访问。它的测试目标是基于URL的,因此,既可以用来测试Apache的负载压力,也可以测试nginx、lighthttp、tomcat、IIS等其它Web服务器的压力。ab命令对发出负载的计算机要求很低,既不会占用很高CPU,也不会占用很多内存,但却会给目标服务器造成巨大的负载,可能会造成目标服务器资源耗尽,严重时可能会导致死机。
使用 ab -h 命令查看其支持的参数,如下所示:
Usage: ab [options] [http[s]://]hostname[:port]/path Options are: -n requests Number of requests to perform -c concurrency Number of multiple requests to make -t timelimit Seconds to max. wait for responses -b windowsize Size of TCP send/receive buffer, in bytes -p postfile File containing data to POST. Remember also to set -T -u putfile File containing data to PUT. Remember also to set -T -T content-type Content-type header for POSTing, eg. 'application/x-www-form-urlencoded' Default is 'text/plain' -v verbosity How much troubleshooting info to print -w Print out results in HTML tables -i Use HEAD instead of GET -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 -C attribute Add cookie, eg. 'Apache=1234. (repeatable) -H attribute Add Arbitrary header line, eg. 'Accept-Encoding: gzip' Inserted after all normal header lines. (repeatable) -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. -X proxy:port Proxyserver and port number to use -V Print version number and exit -k Use HTTP KeepAlive feature -d Do not show percentiles served table. -S Do not show confidence estimators and warnings. -g filename Output collected data to gnuplot format file. -e filename Output CSV file with percentages served -r Don't exit on socket receive errors. -h Display usage information (this message) -Z ciphersuite Specify SSL/TLS cipher suite (See openssl ciphers) -f protocol Specify SSL/TLS protocol (SSL2, SSL3, TLS1, or ALL)
下面看一个例子:
[root@nginx-dev ~]# ab -n 20000 -c 200 -p "post.txt" -T "application/json" "xxxxxxxxxxxxxxx.ad-survey.com/store/match" Benchmarking xxxxxxxxxxxxxxx.ad-survey.com (be patient) Completed 2000 requests Completed 4000 requests Completed 6000 requests Completed 8000 requests Completed 10000 requests Completed 12000 requests Completed 14000 requests Completed 16000 requests Completed 18000 requests Completed 20000 requests Finished 20000 requests Server Software: nginx Server Hostname: xxxxxxxxxxxxxxx.ad-survey.com Server Port: 80 Document Path: /store/match Document Length: 5961 bytes Concurrency Level: 200 Time taken for tests: 97.578 seconds Complete requests: 20000 Failed requests: 16525 (Connect: 0, Receive: 0, Length: 16525, Exceptions: 0) Total transferred: 84152648 bytes Total body sent: 59680000 HTML transferred: 80532648 bytes Requests per second: 204.97 [#/sec] (mean) Time per request: 975.775 [ms] (mean) Time per request: 4.879 [ms] (mean, across all concurrent requests) Transfer rate: 842.21 [Kbytes/sec] received 597.28 kb/s sent 1439.49 kb/s total Connection Times (ms) min mean[+/-sd] median max Connect: 0 0 0.6 0 30 Processing: 17 968 895.8 793 7349 Waiting: 17 968 895.7 793 7349 Total: 18 968 895.7 793 7349 Percentage of the requests served within a certain time (ms) 50% 793 66% 1110 75% 1378 80% 1591 90% 2160 95% 2611 98% 3162 99% 3640 100% 7349 (longest request)
以上命令中用到的几个参数说明下:
-n 20000:请求总数为20000
-c 200:并发数为200
-p "post.txt":POST请求携带的数据在当前目录的"post.txt"文件中
-T "application/json":POST数据所使用的Content-type头信息
测试结果解释说明:
#服务器软件及其版本号 Server Software: nginx #服务器主机名 Server Hostname: xxx-xxxxxxxxxxx.ad-survey.com #服务器端口号 Server Port: 80 #请求URI,也就是请求的页面文档地址 Document Path: /store/match #页面文档大小,实际上是第一个成功返回的文档的字节大小。如果在测试期间文档长度发生变化,则认为响应是错误的。 Document Length: 5961 bytes #并发数 Concurrency Level: 200 #整个测试持续的时间 Time taken for tests: 97.578 seconds #完成的请求数量 Complete requests: 20000 #失败的请求数量 #如果该数字大于零,将打印另一行,显示由于连接、读取、内容长度不正确或异常而失败的请求的数量。 #需要注意的是,如果后续请求的响应大小与第一个请求的响应大小(Document Length值)不同,则此请求会被视为失败。因此,如果测试请求的响应大小不固定的话,
#Length失败请求数应该忽略!! Failed requests: 16525 (Connect: 0, Receive: 0, Length: 16525, Exceptions: 0) #从服务器接收的字节总数。这个数字实际上是通过网络发送的字节数。 Total transferred: 84152648 bytes #测试期间发送的字节总数。如果测试没有包含要发送的正文,就不会有这个字段。应该是POST请求的数据大小(所有请求之和) Total body sent: 59680000 #从服务器接收的文档字节总数。这个数字不包括HTTP报头中的字节数 HTML transferred: 80532648 bytes #吞吐率,指的是某个并发用户数下单位时间内处理的请求数【第一性能指标】 计算公式:总请求数 / 处理完成这些请求数所花费的时间,即Request per second = Complete requests / Time taken for tests Requests per second: 204.97 [#/sec] (mean) #用户平均请求等待时间【第二性能指标】 #计算公式:处理完成所有请求数所花费的时间 /(总请求数 / 并发用户数),即Time per request = Time taken for tests /( Complete requests / Concurrency Level) Time per request: 975.775 [ms] (mean) #服务器平均请求处理时间【第三性能指标】 #计算公式:处理完成所有请求数所花费的时间 / 总请求数,即Time taken for tests / Complete requests Time per request: 4.879 [ms] (mean, across all concurrent requests) #传输速率 Transfer rate: 842.21 [Kbytes/sec] received 597.28 kb/s sent 1439.49 kb/s total #消耗时间的分解、统计 #表示在Connect,Processing,Waiting,Total各阶段的耗时的最小值(min),平均值(mean),标准差([+/-sd]),中值(median),最大值(max)。 Connection Times (ms) min mean[+/-sd] median max Connect: 0 0 0.6 0 30 Processing: 17 968 895.8 793 7349 Waiting: 17 968 895.7 793 7349 Total: 18 968 895.7 793 7349 #在特定时间内处理请求数的百分比 Percentage of the requests served within a certain time (ms) 50% 793 #50%请求的响应时间在793ms内 66% 1110 #66%请求的响应时间在1110ms内 75% 1378 #以此类推 80% 1591 90% 2160 95% 2611 98% 3162 99% 3640 100% 7349 (longest request)
参考:
http://httpd.apache.org/docs/current/programs/ab.html
https://www.jb51.net/article/28320.htm