AB压测工具使用

AB压测工具的使用

官网:
http://mirror.bit.edu.cn/apache/httpd/
http://httpd.apache.org/docs/2.4/programs/ab.html
http://httpd.apache.org/docs/2.2/

什么是AB

它是Apache服务器的一个命令,ab命令模拟多线程并发请求,测试服务器负载压力,也可以测试nginx、lighthttp、IIS等其它Web服务器的压力。
ab命令对发出负载的计算机要求很低,既不会占用很多CPU,也不会占用太多的内存,但却会给目标服务器造成巨大的负载,因此是某些DDOS攻击之必备良药,老少皆宜。自己使用也须谨慎。否则压测负载过多,造成目标服务器直接因内存耗光死机,而不得不硬重启,得不偿失。
在带宽不足的情况下,最好是本机进行测试,建议使用内网的另一台或者多台服务器通过内网进行测试,这样得出的数据,准确度会高很多。远程对web服务器进行压力测试,往往效果不理想(因为网络延时过大或带宽不足)

安装

首先得进入官网下载如下的安装文件。

  1. tar -zxvf httpd-2.2.31 .tar.gz
  2. cd http-2.2.31
  3. ./configure --prefix=/payment/tools/httpd --with-apr=/payment/tools/apr-1.5.2/bin/apr-1-config --with-apr-util=/payment/tools/apr-util-1.5.4/bin/apu-1-config --with-pcre=/payment/tools/pcre-7.9/bin/pcre-config --with-included-apr
  4. make
  5. make install

PS: 前提需要先安装了apr-1.5.2.tar.gzapr-util-1.5.4.tar.gzpcre-7.9.tar.gz

安装apr
./configure --prefix=/payment/tools/apr-1.5.2
make
make install

安装apr-util
./configure --prefix=/payment/tools/apr-util-1.5.4 --with-apr=/payment/tools/apr-1.5.2/bin/apr-1-config
make
make install

同理pcre也是这种安装方法。

安装问题

如果到了make后出现如下错误,

1
2
3
4
5
make[2]: *** [htpasswd] Error 1
make[2]: Leaving directory `/usr/local/directadmin/custombuild/httpd-2.0.63/support'
make[1]: *** [all-recursive] Error 1
make[1]: Leaving directory `/usr/local/directadmin/custombuild/httpd-2.0.63/support'
make: *** [all-recursive] Error 1

解决办法:
在configure后面添加一个–with-included-apr,完美解决上述问题。

ab帮助命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
Usage: ./ab [options] [http://]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)

ab测试返回描述

返回关键字 描述
Server Software The value, if any, returned in the server HTTP header of the first successful response. This includes all characters in the header from beginning to the point a character with decimal value of 32 (most notably: a space or CR/LF) is detected.
Server Hostname The DNS or IP address given on the command line
Server Port The port to which ab is connecting. If no port is given on the command line, this will default to 80 for http and 443 for https.
SSL/TLS Protocol The protocol parameters negotiated between the client and server. This will only be printed if SSL is used.
Document Path The request URI parsed from the command line string.
Document Length This is size in bytes of the first succesfully returning document. If the document length changes during testing, the response is considered an error.
Concurrency Level The number of concurrent clients used during the test
Time taken for tests This is the time taken from the moment the first socket connection is created to the moment the last response is recieved
Complete requests The number of successful responses recieved
Failed requests The number of requests that were considered a failure. If the number is greater than zero, another line will be printed showing the numer of requests that failed due to connecting, reading, incorrect content length, or exceptions.
Write errors The number of errors that failed during write (broken pipe).
Non-2xx responses The number of responses that were not in the 200 series of response codes. If all responses were 200, this field is not printed.
Keep-Alive requests The number of connections that resulted in Keep-Alive requests
Total body sent If configured to send data as part of the test, this is the total number of bytes sent during the tests. This field is omitted if the test did not include a body to send.
Total transferred The total number of bytes received from the server. This number is essentially the number of bytes sent over the wire.
HTML transferred The total number of document bytes received from the server. This number excludes bytes received in HTTP headers
Requests per second This is the number of requests per second. This value is the result of dividing the number of requests by the total time taken
Time per request The average time spent per request. The first value is calculated with the formula concurrency timetaken 1000 / done(这里指的是并发数) while the second value is calculated with the formula timetaken * 1000 / done
备注:这里测试结果会有两个值第一个Time per request代表的是所有请求响应所花费的时间,第二个值是并发的请求的平均时间,可以看到其结果是timeaken响应花费的时间除以并发数。
Transfer rate The rate of transfer as calculated by the formula totalread / 1024 / timetaken

ab 压测过程中的常用命令

  1. ./ab -n [请求数] -c [concurrency并发数] URL

    1
    ./ab -n 10 -c 10 http://www.baidu.com (-n 10模拟10个请求,-c 10 模拟10个并发数,去访问http://www.baidu.com,相当于模拟10个用户同时去访问百度)
  2. ./ab -t [等待请求响应最大时间,单位秒] -c [并发数] URL

    1
    ./ab -t 1 -c 10 http://www.baidu.com (-t 1模拟发一秒钟请求,-c 10 一次发10个请求数,去访问http://www.baidu.com,相当于一次10个人同时去访问百度持续一秒钟)
  3. 如果需要带参数进行POST访问,则需要配合-T 和 -p 选项
    ./ab -n [请求数] -c [concurrency并发数] -T "content-type" -p postFile.txt URL

    1
    2
    3
    ./ab -n 100 -c 100 -T "application/json" -p /payment/param/getCashier.txt
    http://171.0.0.54:18000/payment-api/pay/cashier
    模拟100个请求同时使用POST请求发起对http://171.0.0.54:18000/payment-api/pay/cashier的访问

AB压测实战

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#模拟10个用户并发对http://171.0.0.54:18000/gray/unPayOrder_c.html进行访问
payment@test1:~/tools/httpd/bin> ./ab -n 10 -c 10 http://171.0.0.54:18000/gray/unPayOrder_c.html

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 171.0.0.54 (be patient).....done
Server Software: openresty/1.11.2.5
Server Hostname: 171.0.0.54
Server Port: 18000
Document Path: /gray/unPayOrder_c.html
Document Length: 7608 bytes HTTP响应正文数据长度
Concurrency Level: 10 并发级别
Time taken for tests: 0.001 seconds 处理完这些请求所花费的时间
Complete requests: 10 完成请求数
Failed requests: 0 失败请求数
Write errors: 0
#网络总传输量
Total transferred: 77360 bytes
HTML transferred: 76080 bytes
#吞吐量-每秒请求数QPS
Requests per second: 7698.23 [#/sec] (mean)
#服务器收到请求,响应页面要花费的时间
Time per request: 1.299 [ms] (mean)
#并发的每个请求所耗费的平均时间
Time per request: 0.130 [ms] (mean, across all concurrent requests) --请求平均耗时
Transfer rate: 58157.72 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.0 0 0
Processing: 0 1 0.2 1 1
Waiting: 0 1 0.3 1 1
Total: 0 1 0.2 1 1
#整个场景中所有请求的响应情况,90%的用户请求响应小于1ms,95%的用户请求响应小于1.2ms,100%的用户请求响应小于3ms
Percentage of the requests served within a certain time (ms)
50% 1
66% 1
75% 1
80% 1
90% 1.2
95% 1.2
98% 2
99% 2
100% 3 (longest request)

参考文献

http://www.cnblogs.com/gumuzi/p/5617232.html 《Apache ab性能测试结果分析》
http://blog.csdn.net/hexuan1/article/details/45191549 《运行ab测试时socket: Too many open files (24)的解决办法》
http://eip.teamshub.com/t/3158668 《性能压测工具[AB]安装》