引言: 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 |
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. 参考资料