ab命令会创建多个并发访问线程,模拟多个访问者同时对某一URL地址进行访问。它的测试目标是基于URL的,因此,它既可以用来测试apache的负载压力,也可以测试nginx、lighthttp、tomcat、IIS等其它Web服务器的压力。

ab命令对发出负载的计算机要求很低,它既不会占用很高CPU,也不会占用很多内存。但却会给目标服务器造成巨大的负载,其原理类似CC攻击。自己测试使用也需要注意,否则一次上太多的负载。可能造成目标服务器资源耗完,严重时甚至导致死机。


查看ab是否安装成功,可以切换到上述目录下,使用ab –V命令进行检测
ab -V

yum -y install httpd-tools #直接安装工具包

ab -V
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/

ab 参数比较多,一般常用的是-c -n
ab -c 10 -n 100 https://jerryblog.cn/index.php

-c10表示并发用户数为10
-n100表示请求总数为10



#测试需要登陆的信息

先用账户和密码登录后,用开发者工具找到标识这个会话的Cookie值(Session ID)记下来
复制Cookie信息
ab -n 100 -c 100 -H "Cookie: JSESSIONID=01BCDA8D30F3011A1C8136ED9B0A3ED6.server;USER.oooooooooooooooo=ab84b0125a24ecb263c6b677b989683ca26da6b4076ae09e1d5ccb8595a92a6d28233e61d860c9d0b745b0dfd8426494"  http://localhost/user/index


ab -c 10 -n 100 https://jerryblog.cn/index.php
This is ApacheBench, Version 2.3 <$Revision: 655654 {#content}gt;
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking jerryblog.cn (be patient).....done
Server Software: nginx/1.10.0 #测试软件版本
Server Hostname: jerryblog.cn #域
Server Port: 443 #端口
SSL/TLS Protocol: TLSv1/SSLv3,ECDHE-RSA-AES128-GCM-SHA256,2048,128

Document Path: /index.php
Document Length: 16528 bytes #页面长度

Concurrency Level: 10 #并发数
Time taken for tests: 7.037 seconds #共使用了多少时间
Complete requests: 100 #请求数
Failed requests: 0 #失败请求数
Write errors: 0
Total transferred: 1671300 bytes#总共传输字节数,包含http的头信息等
HTML transferred: 1652800 bytes#html字节数,实际的页面传递字节数
Requests per second: 14.21 [#/sec] (mean)##每秒多少请求,这个是非常重要的参数数值,服务器的吞吐量
Time per request: 703.723 [ms] (mean)#用户平均请求等待时间
Time per request: 70.372 [ms] (mean, across all concurrent requests)#服务器平均处理时间,也就是服务器吞吐量的倒数
Transfer rate: 231.93 [Kbytes/sec] received #每秒获取的数据长度

Connection Times (ms)

          min  mean[+/-sd] median   max

Connect: 12 54 33.9 45 169
Processing: 59 628 114.3 635 894
Waiting: 52 626 114.7 632 894
Total: 150 683 122.6 679 1063

Percentage of the requests served within a certain time (ms)
50% 679 #50%的请求在679ms内返回
66% 706
75% 730
80% 763
90% 829
95% 893
98% 941
99% 1063
100% 1063 (longest request)

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
-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)