一、ab的原理
ab是apachebench命令的缩写。
ab的原理:ab命令会创建多个并发访问线程,模拟多个访问者同时对某一URL地址进行访问。它的测试目标是基于URL的,因此,它既可以用来测试apache的负载压力,也可以测试nginx、lighthttp、tomcat、IIS等其它Web服务器的压力。
ab命令对发出负载的计算机要求很低,它既不会占用很高CPU,也不会占用很多内存。但却会给目标服务器造成巨大的负载,其原理类似CC攻击。自己测试使用也需要注意,否则一次上太多的负载。可能造成目标服务器资源耗完,严重时甚至导致死机。
二、ab的安装
yum -y install httpd-tools
ab -V
三、ab参数说明
[root@myhost vhost]# ab --help
ab: wrong number of arguments
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
(SSL3, TLS1, TLS1.1, TLS1.2 or ALL)
四、ab实际使用
ab的命令参数比较多,我们经常使用的是-c和-n参数。
[root@myhost vhost]# ab -c 10000 -n 100000 http://www.baidu.com
-c表示并发用户数
-n表示请求总数
五、输出结果
Server Software: nginx
Server Hostname: 127.0.0.1
Server Port: 80
Document Path: /wordpress
Document Length: 178 bytes #请求大小
Concurrency Level: 10000 #并发请求量
Time taken for tests: 7.926 seconds #整个测试持续时间
Complete requests: 100000 #完成的请求数
Failed requests: 0 #失败的请求数
Write errors: 0
Non-2xx responses: 100000
Total transferred: 36800000 bytes #整个场景中网络传输量
HTML transferred: 17800000 bytes #整个场景中的HTML内容传输量
Requests per second: 12615.91 [#/sec] (mean) #吞吐速率,大家最关心的指标之一
Time per request: 792.650 [ms] (mean) #用户平均请求等待时间,大家最关心的指标之二
Time per request: 0.079 [ms] (mean, across all concurrent requests) #服务器平均请求处理的时间,大家最关心的指标之三
Transfer rate: 4533.84 [Kbytes/sec] received #平均每秒网络上的流量,可以帮助排除是否有存在网络流量过大导致响应时间延长的问题
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 510 1056.6 3 7026
Processing: 3 28 89.9 8 1889
Waiting: 0 25 89.4 6 1887
Total: 7 538 1079.8 12 7240
Percentage of the requests served within a certain time (ms)
50% 12
66% 22
75% 1016
80% 1022
90% 1231
95% 3027
98% 3232
99% 3828
100% 7240 (longest request)
定时任务例子
#* * * * * /usr/bin/ab -r -c 10000 -n 100000 http://192.168.223.143:8317/hdpanel/login