压力测试之ab测试

一、介绍
ab是apache自带的压力测试工具,非常实用,他不仅能对apache服务器进行压力测试,还可以对 nginx、tomcat、IIS等服务器进行测试

二、原理
ab命令会创建多个并发访问县城,模拟多个访问者同时对某一URL地址进行访问,他的测试目标是基于URL的。他既可以测试负载压力,也可以测试web服务器的压力
ab命令对发出的负责计算机要求很低,他不会占用很高CPU也不会占用很高内存,但会给服务器造成很高的负载。其原理类似DDos攻击,所以使用时需要注意,可能造成目标服务器资源耗尽,严重时导致死机。

三、ab的安装

  • ab的安装非常简单,如果是源码安装apache的话,那就更简单了。apache安装完毕后ab命令存放在apache安装目录的bin目录下。如下: /usr/local/apache2/bin 
  • 如果apache 是通过yum的RPM包方式安装的话,ab命令默认存放在/usr/bin目录下。如下:
  • 可以通过 which ab 来查看是否安装了ab,以及ab的位置
  • ab -V:检测是否安装ab以及版本信息


四、参数说明

bash-3.2# ab -he
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
    -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)
    -Z ciphersuite  Specify SSL/TLS cipher suite (See openssl ciphers)
    -f protocol     Specify SSL/TLS protocol
                    (SSL2, SSL3, TLS1 or ALL)


-n: 请求数
-c: 一次产生的请求个数,默认为1
-t: 测试所进行的最大秒数,默认没有时间限制
-p: 包含了需要POST的数据的文件 
-P: 对一个中转代理提供BASIC认证信任。用户名和密码由一个:隔开,并以base64编码形式发送。无论服务器是否需要(即, 是否发送了401认证需求代码),此字符串都会被发送
-T: POST数据所使用的Content-type头信息
-v: 设置显示信息的详细程度-4或更大值会显示头信息,3或更大值可以显示响应代码(404,200等),2或更大值可以显示警告和其他信息
-V: 显示版本号并退出。
-w: w以HTML表的格式输出结果。默认时,它是白色背景的两列宽度的一张表。
-i: 执行HEAD请求,而不是GET。
-x: 设置<table>属性的字符串。
-X: 对请求使用代理服务器
-y: 设置<tr>属性的字符串。
-z: 设置<td>属性的字符串。
-C: 对请求附加一个Cookie:行。其典型形式是name=value的一个参数对,此参数可以重复。
-H: 对请求附加额外的头信息。此参数的典型形式是一个有效的头信息行,其中包含了以冒号分隔的字段和值的对(如,"Accept-Encoding:zip/zop;8bit")。
-A: 对服务器提供BASIC认证信任。用户名和密码由一个:隔开,并以base64编码形式发送。无论服务器是否需要(即,是否发送了401认证需求代码),此字符串都会被发送。
-h: 显示使用方法。
-d: 不显示"percentage served within XX [ms] table"的消息(为以前的版本提供支持)。
-e: 产生一个以逗号分隔的(CSV)文件,其中包含了处理每个相应百分比的请求所需要(从1%到100%)的相应百分比的(以微妙为单位)时间。由于这种格式已经“二进制化”,所以比'gnuplot'格式更有用。
-g: 把所有测试结果写入一个'gnuplot'或者TSV(以Tab分隔的)文件。此文件可以方便地导入到Gnuplot,IDL,Mathematica,Igor甚至Excel中。其中的第一行为标题。
-i: 执行HEAD请求,而不是GET
-k: 启用HTTP KeepAlive功能,即在一个HTTP会话中执行多个请求。默认时,不启用KeepAlive功能
-q: 如果处理的请求数大于150,ab每处理大约10%或者100个请求时,会在stderr输出一个进度计数。此-q标记可以抑制这些信息。

五、测试结果说明

  1. 吞吐率(requests per second):描述服务器并发处理能力,单位是reqs/s,只的是在某个并发用户数下单位时间内能处理的请求数。吞吐率是基于用户并发数的。
  2. 并发连接数(he number of concurrent connections):指的是某个时刻服务器所接受的请求书目
  3. 并发用户数(Concurrency Level) 要注意区分这个概念和并发连接数之间的区别,一个用户可能同时会产生多个会话,也即连接数。在HTTP/1.1下,IE7支持两个并发连接,IE8支持6个并发连接,FireFox3支持4个并发连接,所以相应的,我们的并发用户数就得除以这个基数
  4. 用户平均请求等待时间(Time per request) 处理完成所有请求数所花费的时间/(总请求数/并发用户数)
  5. 服务器平均请求等待时间(Time per request:across all concurrent requests) 处理完成所有请求数所花费的时间/总请求数,即:Time taken for/testsComplete requests,可以看到,它是吞吐率的倒数。同时,它也等于用户平均请求等待时间/并发用户数,即Time per request/Concurrency Level

六、实例说明
     这里拿了京东的一个错误页面做说明:
bash-3.2# ab -n 1000 -c 100 http://www.jd.com/error2.aspx
This is ApacheBench, Version 2.3 <$Revision: 1663405 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking www.jd.com (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Completed 600 requests
Completed 700 requests
Completed 800 requests
Completed 900 requests
Completed 1000 requests
Finished 1000 requests


Server Software:        JDWS
Server Hostname:        www.jd.com
Server Port:            80

Document Path:          /error2.aspx
Document Length:        16503 bytes

Concurrency Level:      100
Time taken for tests:   13.908 seconds
Complete requests:      1000
Failed requests:        0
Total transferred:      16870000 bytes
HTML transferred:       16503000 bytes
Requests per second:    71.90 [#/sec] (mean)
Time per request:       1390.829 [ms] (mean)
Time per request:       13.908 [ms] (mean, across all concurrent requests)
Transfer rate:          1184.52 [Kbytes/sec] received
Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        7  127 282.3     47    1593
Processing:    27  697 1327.0    190   12730
Waiting:       11  167 374.4     57    3810
Total:         75  824 1425.5    287   12785

Percentage of the requests served within a certain time (ms)
  50%    287
  66%    586
  75%    952
  80%   1209
  90%   1652
  95%   2874
  98%   5060
  99%   8673
 100%  12785 (longest request)
     
分析:
  • Server Software:服务器软件,他这里用的JDWS(神马玩意)
  • Server Hostname:主机名www.jd.com
  • Server Port:端口80
  • Document Path:请求文件的路径
  • Document Length:响应文件的正文长度

  • 下面关键了
  •  
  • Concurrency Level:并发用户数,和设置的一样
  • Time taken for tests: 任务处理完所花费的时间
  • Complete requests: 总请求量
  • Failed requests:失败的请求量
  • Total transferred:示所有请求的响应数据长度总和,包括每个HTTP响应数据的头信息和正文数据的长度。注意这里不包括HTTP请求数据的长度,仅仅为web服务器流向用户PC的应用层数据总长度
  • HTML transferred:表示所有请求的响应数据中正文数据的总和,也就是减去了Total transferred中HTTP响应数据中的头信息的长度。
  •  
  • Requests per second : 吞吐率 计算公式:Complete requests/Time taken for tests
  • Time per request:用户平均请求等待时间,计算公式:Time token for tests/Complete requests/Concurrency Level)。
    Time per requet(across all concurrent request):
    服务器平均请求等待时间,计算公式:Time taken for tests/Complete requests,正好是吞吐率的倒数。也可以这么统计:Time per request/Concurrency Level
  • Transfer rate:单位时间从服务器获取数据的长度,这个统计很好的说明服务器的处理能力达到极限时,其出口宽带的需求量
  • Percentage of requests served within a certain time(ms):这部分数据用于描述每个请求处理时间的分布情况,
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章