ab(Apache Bench)是Apache附带的压力测试软件,容易使用,功能彻底可以知足咱们的要求,安装apache时会自带该压力测试软件web
ab自带的参数说明:apache
[root@xiaozhu images]# 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)
ab的参数比较多,经常使用的就几个:windows
下面使用ab进行一次压力测试:ruby
[root@xiaozhu images]# ab -c5 -n100 -k xxx.com/
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking cms.dev.paidui.com (be patient).....done
Server Software: Tengine
Server Hostname: cms.dev.paidui.com
Server Port: 80
Document Path: /
Document Length: 30692 bytes
Concurrency Level: 5
Time taken for tests: 20.663 seconds
Complete requests: 100
Failed requests: 10
(Connect: 0, Receive: 0, Length: 10, Exceptions: 0)
Write errors: 0
Non-2xx responses: 10
Keep-Alive requests: 10
Total transferred: 2881762 bytes
HTML transferred: 2822279 bytes
Requests per second: 4.84 [#/sec] (mean)
Time per request: 1033.169 [ms] (mean)
Time per request: 206.634 [ms] (mean, across all concurrent requests)
Transfer rate: 136.19 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 5 2.1 6 11
Processing: 9 984 1007.0 739 7508
Waiting: 9 330 230.7 257 1278
Total: 9 989 1007.2 744 7513
Percentage of the requests served within a certain time (ms)
50% 744
66% 919
75% 1130
80% 1222
90% 1772
95% 2422
98% 4720
99% 7513
100% 7513 (longest request)
测试结果一目了然,吞吐率为4.84 reqs/s(吞吐率即单位时间内服务器处理的请求数)服务器
测试结果中关注如下内容:cookie
持久链接(Keep-Alive)又称长链接,指TCP链接中连续发送多份数据而不断开的链接,与之对应的称为短链接,就是链接后发送一次数据便断开的链接。长链接对于密集型图片或者网页等小数据请求处理有明显的加速做用。架构
在请求大量小文件时,长链接可以有效地减小从新创建链接的开销。在启动ab时加上-k参数可使用Keep-Alive模式进行测试并发
好比:app
先测试不使用长链接请求一个图片:socket
[root@xiaozhu images]# ab -c10 -n1000 cms.dev.paidui.com/images/3gphost.jpg
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking cms.dev.paidui.com (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Completed 600 requests
Completed 700 requests
Completed 800 requests
Completed 900 requests
Completed 1000 requests
Finished 1000 requests
Server Software: Tengine
Server Hostname: cms.dev.paidui.com
Server Port: 80
Document Path: /images/3gphost.jpg
Document Length: 169 bytes
Concurrency Level: 10
Time taken for tests: 5.166 seconds
Complete requests: 1000
Failed requests: 0
Write errors: 0
Non-2xx responses: 1000
Total transferred: 659912 bytes
HTML transferred: 169000 bytes
Requests per second: 193.59 [#/sec] (mean)
Time per request: 51.657 [ms] (mean)
Time per request: 5.166 [ms] (mean, across all concurrent requests)
Transfer rate: 124.76 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 4 5 1.2 5 17
Processing: 14 46 26.9 39 219
Waiting: 14 46 26.9 39 219
Total: 19 51 26.9 44 224
Percentage of the requests served within a certain time (ms)
50% 44
66% 49
75% 52
80% 55
90% 64
95% 113
98% 174
99% 193
100% 224 (longest request)
不使用长链接的时候吞吐率为193.59
下面使用长链接:
[root@xiaozhu images]# ab -c10 -n1000 -k cms.dev.paidui.com/images/3gphost.jpg
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking cms.dev.paidui.com (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Completed 600 requests
Completed 700 requests
Completed 800 requests
Completed 900 requests
Completed 1000 requests
Finished 1000 requests
Server Software: Tengine
Server Hostname: cms.dev.paidui.com
Server Port: 80
Document Path: /images/3gphost.jpg
Document Length: 169 bytes
Concurrency Level: 10
Time taken for tests: 3.086 seconds
Complete requests: 1000
Failed requests: 0
Write errors: 0
Non-2xx responses: 1000
Keep-Alive requests: 992
Total transferred: 648975 bytes
HTML transferred: 169000 bytes
Requests per second: 324.08 [#/sec] (mean)
Time per request: 30.856 [ms] (mean)
Time per request: 3.086 [ms] (mean, across all concurrent requests)
Transfer rate: 205.39 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.8 0 7
Processing: 12 31 21.7 27 232
Waiting: 12 31 21.7 27 232
Total: 12 31 22.2 27 238
Percentage of the requests served within a certain time (ms)
50% 27
66% 31
75% 34
80% 35
90% 40
95% 46
98% 53
99% 220
100% 238 (longest request)
使用长链接的吞吐率为324,除此以外,Keep-Alive request为992,即便用长链接的有992个请求,也就是说有992个请求重用同一个TCP链接。
《PHP核心技术与最佳实践》的第11章 高性能网站架构方案