Web性能压力测试工具ApacheBench详解

  网站性能压力测试是性能调优过程中必不可少的一环。只有让服务器处于高压状态下才能真正体现出各种设置所暴露的问题。Apache中有个自带的名为ab的工具,可以对Apache或其他类型的服务器进行网站访问压力测试。

ApacheBench原理:

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

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

ApacheBench参数说明

[root@thankinglove bin]# ab -h
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
    
    //测试所进行的最大秒数。其内部隐含值是-n 50000。它可以使对服务器的测试限制在一个固定的
    //总时间以内。默认时,没有时间限制。
    -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
    
    //包含了需要POST的数据的文件,文件格式如“p1=1&p2=2”.使用方法是 -p 111.txt 。(配合-T)
    -p postfile     File containing data to POST. Remember also to set -T
    
    -u putfile      File containing data to PUT. Remember also to set -T
    
    //POST数据所使用的Content-type头信息,如 -T “application/x-www-form-urlencoded” 。 (配合-p)
    -T content-type Content-type header to use for POST/PUT data, eg.
                    'application/x-www-form-urlencoded'
                    Default is 'text/plain'
                    
    //设置显示信息的详细程度 – 4或更大值会显示头信息, 3或更大值可以显示响应
    //代码(404, 200等), 2或更大值可以显示警告和其他信息。 -V 显示版本号并退出。
    -v verbosity    How much troubleshooting info to print
    
    //以HTML表的格式输出结果。默认时,它是白色背景的两列宽度的一张表。
    -w              Print out results in HTML tables
    
    // 执行HEAD请求,而不是GET。
    -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 cookie-name=value 对请求附加一个Cookie:行。 其典型形式是name=value的一个参数对。
    //此参数可以重复,用逗号分割。
    //提示:可以借助session实现原理传递 JSESSIONID参数, 实现保持会话的功能,如
    //-C ” c1=1234,c2=2,c3=3, JSESSIONID=FF056CD16DA9D71CB131C1D56F0319F8″ 。
    -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 proxy-auth-username:password 对一个中转代理提供BASIC认证信任。
    //用户名和密码由一个:隔开,并以base64编码形式发送。无论服务器是否需要
    //(即, 是否发送了401认证需求代码),此字符串都会被发送。
    -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
                    (SSL2, SSL3, TLS1, TLS1.1, TLS1.2 or ALL)

参数很多,一般只有-c和-n即可,如:

ab -c 100 -n 10 
//输出如下
This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 47.88.6.126 (be patient).....done


Server Software:        nginx/1.8.0
Server Hostname:        192.168.8.10
Server Port:            80

Document Path:          /index.php
Document Length:        72548 bytes

Concurrency Level:      10
Time taken for tests:   0.221 seconds
Complete requests:      100
Failed requests:        5
   (Connect: 0, Receive: 0, Length: 5, Exceptions: 0)
Write errors:           0
Total transferred:      7270995 bytes
HTML transferred:       7254795 bytes
Requests per second:    452.46 [#/sec] (mean)
Time per request:       22.101 [ms] (mean)
Time per request:       2.210 [ms] (mean, across all concurrent requests)
Transfer rate:          32127.58 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    1   1.6      0       7
Processing:     6   20   3.9     22      24
Waiting:        0   20   4.7     22      24
Total:          9   21   2.8     22      24

Percentage of the requests served within a certain time (ms)
  50%     22
  66%     22
  75%     22
  80%     22
  90%     23
  95%     23
  98%     24
  99%     24
 100%     24 (longest request)


http://www.ha97.com/4617.html

http://www.jb51.net/article/59469.htm