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服务器进行压力测试,往往效果不理想(因为网络延时过大或带宽不足)
安装
首先得进入官网下载如下的安装文件。
tar -zxvf httpd-2.2.31 .tar.gz
cd http-2.2.31
./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
make
make install
PS: 前提需要先安装了apr-1.5.2.tar.gz
、apr-util-1.5.4.tar.gz
、pcre-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
5make[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 | Usage: ./ab [options] [http://]hostname[:port]/path |
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 压测过程中的常用命令
./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个用户同时去访问百度)
./ab -t [等待请求响应最大时间,单位秒] -c [并发数] URL
1
./ab -t 1 -c 10 http://www.baidu.com (-t 1模拟发一秒钟请求,-c 10 一次发10个请求数,去访问http://www.baidu.com,相当于一次10个人同时去访问百度持续一秒钟)
如果需要带参数进行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 | #模拟10个用户并发对http://171.0.0.54:18000/gray/unPayOrder_c.html进行访问 |
参考文献
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]安装》