https://blog.csdn.net/blueheart20/article/details/52170790
引言: ab測試是業界比較流行和簡單易用的一種壓力測試工具包,本文將簡要介紹其核心使用的方法。
1. ab
ab是Apache HTTP server benchmarking tool的縮寫,可以用以測試HTTP請求的服務器性能。其是通過命令行的不同參數從而提供簡單易用的性能測試工具命令組合。
2. 環境信息
CentOS 7, ab的版本是2.3, 目前最新的版本是2.4
3. 使用指南
3.1 查看幫助文件內容
[root@test~]# 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 at a time
-t timelimit Seconds to max. to spend on benchmarking
This implies -n 50000
-s timeout Seconds to max. wait for each response
Default is 30 seconds
-b windowsize Size of TCP send/receive buffer, in bytes
-B address Address to bind to when making outgoing connections
-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 to use for POST/PUT data, 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.
-q Do not show progress when doing more than 150 requests
-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, TLS1.1, TLS1.2 or ALL)
3.2 初步測試
參數說明: -n 請求數量, -c 並發數量
ab option url-address
Case 1: ab -n1000 -c10 http://www.baidu.com/index.html
結果信息如下:
[root@flybird ~]# ab -n1000 -c10 http://www.baidu.com/index.html
This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking www.baidu.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
apr_socket_recv: Connection reset by peer (104)
分析百度不允許直接這么測試,直接將請求重置。
Case 2:
command: ab -n1000 -c10 http://www.csdn.net/index.html
測試結果:
[root@test~]# ab -n1000 -c10 http://www.csdn.net/index.html
This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking www.csdn.net (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
//服務器信息: openresty
Server Software: openresty
Server Hostname: www.csdn.net
Server Port: 80
Document Path: /index.html
Document Length: 100686 bytes
Concurrency Level: 10
Time taken for tests: 24.567 seconds
Complete requests: 1000
Failed requests: 0
Write errors: 0
Total transferred: 100982000 bytes
HTML transferred: 100686000 bytes
//服務器的吞吐量,每秒請求處理量
Requests per second: 40.71 [#/sec] (mean)
//用戶平均等待時間
Time per request: 245.665 [ms] (mean)
//服務器端單個請求的處理時間
Time per request: 24.567 [ms] (mean, across all concurrent requests)
Transfer rate: 4014.21 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 38 71 175.3 44 3065
Processing: 114 173 30.4 170 446
Waiting: 38 46 9.6 44 302
Total: 152 244 180.7 213 3294
Percentage of the requests served within a certain time (ms)
50% 213
66% 238
75% 247
80% 249
90% 256
95% 288
98% 1201
99% 1248
100% 3294 (longest request)
測試結果: 請求吞吐量為:40.71/s, 單個請求處理時間為:24.567
Case 2: ab -n1000 -c10 http://www.sina.com.cn/index.html
[root@test~]# ab -n1000 -c10 http://www.sina.com.cn/index.html
This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking www.sina.com.cn (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
//后台服務器是nginx
Server Software: nginx
Server Hostname: www.sina.com.cn
Server Port: 80
Document Path: /index.html
Document Length: 27201 bytes
Concurrency Level: 10
Time taken for tests: 8.470 seconds
Complete requests: 1000
Failed requests: 0
Write errors: 0
Total transferred: 27538766 bytes
HTML transferred: 27201000 bytes
Requests per second: 118.06 [#/sec] (mean)
Time per request: 84.704 [ms] (mean)
Time per request: 8.470 [ms] (mean, across all concurrent requests)
Transfer rate: 3174.97 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 15 27 70.7 21 1026
Processing: 32 57 24.3 53 321
Waiting: 15 24 16.4 22 253
Total: 47 84 74.9 74 1094
Percentage of the requests served within a certain time (ms)
50% 74
66% 84
75% 90
80% 92
90% 103
95% 120
98% 124
99% 289
100% 1094 (longest request)
分析結果: 請求吞吐量為:118.06/s, 服務器處理單個請求的時間為8.470 ms。 相比csdn.net而言,舒淇效率將近3倍。csdn為40個請求/s, 單個請求為24ms。
那提個問題為什么sina比csdn的性能高3倍呢? 我們來簡要分析一下,首先是傳輸的數據量,csdn是sina的3倍;以下皆以mean時間為准。
站點 connect processing waiting total
csdn 71 173 46 244
sina.com.cn 27ms 57 24 84
整體而言,可以發現sina.com.cn在各個節點上都是由於csdn的性能的。
3.3 加大並發數的壓測(單台機器)
case 1: ab -c100 -n1000 http://www.csdn.net/index.html [100並發數]
HTML transferred: 100580778 bytes
Requests per second: 52.39 [#/sec] (mean)
Time per request: 1908.606 [ms] (mean)
Time per request: 19.086 [ms] (mean, across all concurrent requests)
Transfer rate: 5161.49 [Kbytes/sec] received
相比之前的數據,並發設為100之后,吞吐量從40上升到52, 單個請求響應時間從24ms降低為19ms
case 2: ab -c200 -n1000 http://www.csdn.net/index.html
HTML transferred: 100358484 bytes
Requests per second: 48.32 [#/sec] (mean)
Time per request: 4139.429 [ms] (mean)
Time per request: 20.697 [ms] (mean, across all concurrent requests)
Transfer rate: 4749.23 [Kbytes/sec] received
相比之前的數據,並發數為200, 吞吐量下降了,響應時間變長。
case 3: ab -c500 -n1000 http://www.csdn.net/index.html [100並發數]
HTML transferred: 83827148 bytes
Requests per second: 49.05 [#/sec] (mean)
Time per request: 10193.544 [ms] (mean)
Time per request: 20.387 [ms] (mean, across all concurrent requests)
Transfer rate: 4029.59 [Kbytes/sec] received
結果分析: 整體數據無太大變化,只是對於單個用戶而言,等待時間變得比較久,需要10s.
6. 參考資料
http://httpd.apache.org/docs/current/programs/ab.html