2017年08月06日 21:46:52
JMeter比较适合做性能测试(模拟用户各种请求设置并发数,发起请求),而AB程序更适合做基准或者压力测试,来测试服务能扛住的最大压力。
AB安装方式很简单,在Centos下,使用
[root@centos02 conf]# yum intall -y httpd-tools
输入ab -help,可以看到可用的参数
[root@centos02 conf]# ab -help
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)
比较常用的是 -k 使用 keepalive默认是禁止的,-c 并发数,-n 总请求次数
简单示例
[root@centos02 conf]# ab -k -c 1 -n 2 http://www.baidu.com/index.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 www.baidu.com (be patient).....done
Server Software: BWS/1.1
Server Hostname: www.baidu.com
Server Port: 80
Document Path: /index.html
Document Length: 111309 bytes
Concurrency Level: 1
Time taken for tests: 2.573 seconds
Complete requests: 2
Failed requests: 1
(Connect: 0, Receive: 0, Length: 1, Exceptions: 0)
Write errors: 0
Keep-Alive requests: 0
Total transferred: 225033 bytes
HTML transferred: 223139 bytes
Requests per second: 0.78 [#/sec] (mean)
Time per request: 1286.352 [ms] (mean)
Time per request: 1286.352 [ms] (mean, across all concurrent requests)
Transfer rate: 85.42 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 24 28 5.1 32 32
Processing: 1210 1251 57.6 1292 1292
Waiting: 283 292 12.9 301 301
Total: 1242 1279 52.6 1316 1316
Percentage of the requests served within a certain time (ms)
50% 1316
66% 1316
75% 1316
80% 1316
90% 1316
95% 1316
98% 1316
99% 1316
100% 1316 (longest request)
测试前,需要关闭一个系统参数,避免系统认为是攻击行为
[root@centos02 etc]# vi sysctl.conf
net.ipv4.tcp_syncookies = 0
另外请求带上 -r 可以保证在个别请求错误时,不退出。
不做这2个配置,可能会出现 apr_poll: The timeout specified has expired (70007)或者apr_socket_recv: Connection reset by peer (104)的错误
完整例子如
[root@centos02 bin]# ab -k -r -c 1000 -n 100000 http://www.shop.com/brand/content/1.jhtml
1000个并发下,请求100000次