1 ab命令原理
Apache的ab命令模拟多线程并发请求,测试服务器负载压力,也可以测试nginx、lighthttp、IIS等其它Web服务器的压力。
ab命令对发出负载的计算机要求很低,既不会占用很多CPU,也不会占用太多的内存,但却会给目标服务器造成巨大的负载,因此是某些DDOS攻击之必备良药,老少皆宜。自己使用也须谨慎。否则一次上太多的负载,造成目标服务器直接因内存耗光死机,而不得不硬重启,得不偿失。
在带宽不足的情况下,最好是本机进行测试,建议使用内网的另一台或者多台服务器通过内网进行测试,这样得出的数据,准确度会高很多。远程对web服务器进行压力测试,往往效果不理想(因为网络延时过大或带宽不足)
2 运行
A 100个并发,总请求数 500
ab -c 100 -n 500 http://127.0.0.1/phpinfo.php
This is ApacheBench, Version 2.3 <$Revision: 1604373 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking 127.0.0.1 (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Finished 500 requests
Server Software: Apache/2.4.10
Server Hostname: 127.0.0.1
Server Port: 80
Document Path: /phpinfo.php
Document Length: 66005 bytes HTTP响应数据的正文长度
Concurrency Level: 100
Time taken for tests: 1.759 seconds
Complete requests: 500
Failed requests: 78
(Connect: 0, Receive: 0, Length: 78, Exceptions: 0)
Total transferred: 33096996 bytes
HTML transferred: 33002496 bytes
Requests per second: 284.30 [#/sec] (mean)
Time per request: 351.739 [ms] (mean)
Time per request: 3.517 [ms] (mean, across all concurrent requests)
Transfer rate: 18377.98 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.8 0 12
Processing: 3 316 447.9 112 1423
Waiting: 1 311 449.2 100 1423
Total: 3 316 448.0 112 1435
Percentage of the requests served within a certain time (ms)
50% 112
66% 260
75% 276
80% 345
90% 1236
95% 1297
98% 1339
99% 1358
100% 1435 (longest request)
B 在60秒内发请求,一次100个请求。
ab -c 100 -t 60 http://127.0.0.1/phpinfo.php
This is ApacheBench, Version 2.3 <$Revision: 1604373 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking 127.0.0.1 (be patient)
Completed 5000 requests
Completed 10000 requests
Finished 10525 requests
Server Software: Apache/2.4.10
Server Hostname: 127.0.0.1
Server Port: 80
Document Path: /phpinfo.php
Document Length: 66005 bytes
Concurrency Level: 100
Time taken for tests: 60.000 seconds
Complete requests: 10525
Failed requests: 1115
(Connect: 0, Receive: 0, Length: 1115, Exceptions: 0)
Total transferred: 696690603 bytes
HTML transferred: 694701378 bytes
Requests per second: 175.42 [#/sec] (mean)
Time per request: 570.076 [ms] (mean)
Time per request: 5.701 [ms] (mean, across all concurrent requests)
Transfer rate: 11339.28 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.9 0 34
Processing: 0 567 1689.7 273 18692
Waiting: 0 561 1689.3 270 18692
Total: 0 567 1689.7 274 18693
Percentage of the requests served within a certain time (ms)
50% 273
66% 295
75% 338
80% 401
90% 657
95% 1305
98% 4499
99% 7287
100% 18693 (longest request)
C 在url中带参数,p.txt 是和ab.exe在一个目录,p.txt 中可以写参数,如 p=wdp&fq=78
ab -t 60 -c 100 -T "text/plain" -p p.txt http://127.0.0.1/phpinfo.php
3 帮助
ab -h
Usage: ab [options] [http://]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
-l Accept variable document length (use this for dynamic pages)
-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.
-m method Method name
-h Display usage information (this message)