ab参数详解 – 压力测试

命令参数:
    -n requests     Number of requests to perform
    //在测试会话中所执行的请求个数。默认时,仅执行一个请求
    -c concurrency Number of multiple requests to make
    //一次产生的请求个数。默认是一次一个。 
    -t timelimit    Seconds to max. wait for responses
    //测试所进行的最大秒数。其内部隐含值是-n 50000。它可以使对服务器的测试限制在一个固定的总时间以内。默认时,没有时间限制。
    -p postfile     File containing data to POST
    //包含了需要POST的数据的文件. 
    -T content-type Content-type header for POSTing
    //POST数据所使用的Content-type头信息。
    -v verbosity    How much troubleshooting info to print
    //设置显示信息的详细程度 - 4或更大值会显示头信息, 3或更大值可以显示响应代码(404, 200等), 2或更大值可以显示警告和其他信息。 -V 显示版本号并退出。
    -w              Print out results in HTML tables
    //以HTML表的格式输出结果。默认时,它是白色背景的两列宽度的一张表。
    -i              Use HEAD instead of GET
   // 执行HEAD请求,而不是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)
    //-C cookie-name=value 对请求附加一个Cookie:行。 其典型形式是name=value的一个参数对。此参数可以重复。
    -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.
    //-P proxy-auth-username:password 对一个中转代理提供BASIC认证信任。用户名和密码由一个:隔开,并以base64编码形式发送。无论服务器是否需要(即, 是否发送了401认证需求代码),此字符串都会被发送。
    -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
    -h              Display usage information (this message)
    //-attributes 设置 属性的字符串. 缺陷程序中有各种静态声明的固定长度的缓冲区。另外,对命令行参数、服务器的响应头和其他外部输入的解析也很简单,这可能会有不良后果。它没有完整地实现HTTP/1.x; 仅接受某些'预想'的响应格式。 strstr(3)的频繁使用可能会带来性能问题,即, 你可能是在测试ab而不是服务器的性能。

 

 

参数很多,一般我们用 -c 和 -n 参数就可以了. 例如:

./ab -c 1000 -n 1000 http://127.0.0.1/index.php

这个表示同时处理1000个请求并运行1000次index.php文件.
#/usr/local/xiaobai/apache2054/bin/ab -c 1000 -n 1000 http://127.0.0.1/index.html.zh-cn.gb2312 
This is ApacheBench, Version 2.0.41-dev <$Revision: 1.121.2.12 $> apache-2.0
Copyright (c) 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Copyright (c) 1998-2002 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
Completed 600 requests
Completed 700 requests
Completed 800 requests
Completed 900 requests
Finished 1000 requests


Server Software:        Apache/2.0.54
//平台apache 版本2.0.54
Server Hostname:        127.0.0.1
//服务器主机名
Server Port:            80
//服务器端口

Document Path:          /index.html.zh-cn.gb2312
//测试的页面文档
Document Length:        1018 bytes
//文档大小

Concurrency Level:      1000
//并发数
Time taken for tests:   8.188731 seconds
//整个测试持续的时间
Complete requests:      1000
//完成的请求数量
Failed requests:        0
//失败的请求数量
Write errors:           0

Total transferred:      1361581 bytes
//整个场景中的网络传输量
HTML transferred:       1055666 bytes
//整个场景中的HTML内容传输量
Requests per second:    122.12 [#/sec] (mean)
//大家最关心的指标之一,相当于 LR 中的 每秒事务数 ,后面括号中的 mean 表示这是一个平均值
Time per request:       8188.731 [ms] (mean)
//大家最关心的指标之二,相当于 LR 中的 平均事务响应时间 ,后面括号中的 mean 表示这是一个平均值
Time per request:       8.189 [ms] (mean, across all concurrent requests)
//每个请求实际运行时间的平均值
Transfer rate:          162.30 [Kbytes/sec] received
//平均每秒网络上的流量,可以帮助排除是否存在网络流量过大导致响应时间延长的问题

Connection Times (ms)
              min mean[+/-sd] median   max
Connect:        4 646 1078.7     89    3291
Processing:   165 992 493.1    938    4712
Waiting:      118 934 480.6    882    4554
Total:        813 1638 1338.9   1093    7785
//网络上消耗的时间的分解,各项数据的具体算法还不是很清楚

Percentage of the requests served within a certain time (ms)
50%   1093
66%   1247
75%   1373
80%   1493
90%   4061
95%   4398
98%   5608
99%   7368
100%   7785 (longest request)
//整个场景中所有请求的响应情况。在场景中每个请求都有一个响应时间,其中50%的用户响应时间小于1093 毫秒,60% 的用户响应时间小于1247 毫秒,最大的响应时间小于7785 毫秒

      由于对于并发请求,cpu实际上并不是同时处理的,而是按照每个请求获得的时间片逐个轮转处理的,所以基本上第一个Time per request时间约等于第二个Time per request时间乘以并发请求数

 

 

 

Apache附带的ab,它非常容易使用,ab可以直接在Web服务器本地发起测试请求。这至关重要,因为我们希望测试的服务器的处理时间,而不包含数据的网络传输时间以及用户PC本地的计算时间。

需要清楚的是,ab进行一切测试的本质都是基于HTTP,所以可以说它是对于Web服务器软件的黑盒性能测试,它获得的一切数据和计算结果,都可以通过HTTP来解释。

另有一些压力测试软件,包括LoadRnner、Jmeter等,则是不同程度上包含了服务器处理之外的时间,比如LoadRunner运行在用户PC上,可以录制浏览器行为,这种测试的结果玩玩侧重于站点用户的角度,有另外一些层面的参考意义。

接下来,将使用ab来进行一次压力测试,在本书中我们使用Apache 2.3中附带的ab,其版本信息如下所示:

[root@localhost ~]#/usr/local/apache/bin/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/

请看下面的命令行信息:

[root@localhost ~]# /usr/local/apache/bin/ab -n1000 -c10 http://localhost/index.html
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/
 
Benchmarking localhost (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:        Apache/2.2.19
Server Hostname:        localhost
Server Port:            80
 
Document Path:          /index.html
Document Length:        45 bytes
 
Concurrency Level:      10
Time taken for tests:   0.454 seconds
Complete requests:      1000
Failed requests:        0
Write errors:           0
Total transferred:      322644 bytes
HTML transferred:       45090 bytes
Requests per second:    2204.64 [#/sec] (mean)
Time per request:       4.536 [ms] (mean)
Time per request:       0.454 [ms] (mean, across all concurrent requests)
Transfer rate:          694.64 [Kbytes/sec] received
 
Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    1   0.5      1       5
Processing:     1    3   5.6      2      62
Waiting:        0    3   5.6      2      62
Total:          1    4   5.6      3      63
 
Percentage of the requests served within a certain time (ms)
  50%      3
  66%      4
  75%      4
  80%      4
  90%      6
  95%     12
  98%     26
  99%     33
 100%     63 (longest request)

请注意我们在启动ab时,传入3个命令行参数,它们正是代表了前面提到的前提条件:
-n1000 表示总请求数位1000
-c 表示并发用户数为10
http://localhost/index.html 表示这些请求的目标URL。
测试结果一目了然,我们看到吞吐率显示为2204.64reqs/s。同时,在测试结果中还有一些其他内容也值得我们关注,主要包括:
Server Software
表示被测试的Web服务器软件名称,这里是Apache/2.2.19,它来自于http响应数据的头信息,所以如果是我们自己编写的Web服务器软或者修改开源Web服务器软件的源代码,便可以随意改写这里的名称。
vi /usr/local/apache/conf/httpd.conf #隐藏具体版本信息
ServerSignature Off
ServerTokens Prod

Server Hostname
表示请求的URL中的主机部分名称,它来自于http请求数据的头信息,这里我们请求的URL是http://localhost/index.html,所以主机名为localhost,说明我们的请求是从Web服务器端发起的。

Server Port
表示被测试的Web服务器软件的监听端口,为了方便测试,我们后面会对多个不同的Web服务器软件使用不同的监听端口。

Document Path
表示请求的URL中根绝对路径,它同样来自于http请求数据的头信息,通过它的后缀名,我们一般可以理解该请求的类型。

Document Length
表示http响应数据的正文长度。

Concurrency Level
表示并发用户数,这是我们设置的参数。

Time taken for tests
表示所有这些请求被处理完成花费的总时间。顺便提一下,某些Apache版本如2.2.4附带的ab,对于这一统计项存在一些计算上的bug,当总请求数较少时,其统计的总时间会无法小于0.1s。

Complete requests
表示总请求数,这是我们设置的相应参数。

Failed requests
表示失败的请求数,这里的失败是指请求的连接服务器、发送数据、接收数据等环节发生异常,以及无响应后超时的情况。对于超时时间的设置可以用ab的-t参数。
而如果接受到的http响应数据的头信息中含有2xx以外的状态码,则会在测试结果显示另一个名为“Non-2xx responses”的统计项,用于统计这部分请求数,这些请求并不算是失败的请求。

Total transferred
表示所有请求的响应数据长度总和,包括每个http响应数据的头信息和正文数据的长度。注意这里不包括http请求数据的长度,所以Total
transferred代表了从Web服务器流向用户PC的应用层数据总长度。通过使用ab的-v参数即可查看详细的http头信息。

HTML transferred
表示所有请求的响应数据中正文数据的总和,也就是减去了Total transferred中http响应数据中头信息的长度。

Requests per second
这便是我们重点关注的吞吐率,它等于:
Complete requests / Time taken for tests

Time per request
这便是前面提到的用户平均请求等待时间,它等于:
Time taken for tests / (Complete requests /Concurrency Level)

Time per request?(across all concurrent requests)
这便是前面提到的服务器平均请求处理时间,它等于:
Time taken for tests / Complete requests

这正是吞吐率的倒数。同时,它也等于:
Time per request / Concurrency Level

Transfer rate
表示这些请求在单位时间内从服务器获取的数据长度,它等于:
Total transferred / Time taken for tests
这个统计项可以很好的说明服务器在处理能力达到限制时,其出口带宽的需求量。
利用前面介绍的有关带宽的知识,不难计算出结果。

Percentage of the requests served within a certain time(ms)
这部分数据用于描述每个请求处理时间的分布情况,比如在以上测试结果中,80%请求的处理时间都不超过1ms,而99%的请求都不超过2ms。注意这里的处理时间,是指前面的Time per request,即对于单个用户而言,平均每个请求处理的时间。

继续压力测试
下面,我们再来进行一次压力测试,此时并发用户数为100,其他条件不变,测试结果如下所示:

[root@localhost ~]# /usr/local/apache/bin/ab -n1000 -c100 http://localhost/index.html
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/
 
Benchmarking localhost (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:        Apache/2.2.19
Server Hostname:        localhost
Server Port:            80
 
Document Path:          /index.html
Document Length:        45 bytes
 
Concurrency Level:      100
Time taken for tests:   0.369 seconds
Complete requests:      1000
Failed requests:        0
Write errors:           0
Total transferred:      283648 bytes
HTML transferred:       46080 bytes
Requests per second:    2711.45 [#/sec] (mean)
Time per request:       36.881 [ms] (mean)
Time per request:       0.369 [ms] (mean, across all concurrent requests)
Transfer rate:          751.07 [Kbytes/sec] received
 
Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0   12   3.4     12      19
Processing:    11   23  18.9     17     122
Waiting:        6   19  18.3     13     114
Total:         20   35  17.1     30     128
 
Percentage of the requests served within a certain time (ms)
  50%     30
  66%     32
  75%     34
  80%     35
  90%     53
  95%     75
  98%     99
  99%    114
 100%    128 (longest request)

和前一次的测试结果相比,可以看出,当并发用户数据从原来的10变为100后,吞吐率从原来的2204.64增长到了2711.45,服务器平均请求处理
时间从原来的0.454ms降到了0.369ms,而用户评价请求等待时间从原来的4.536ms增加到了36.881ms.
可见,随着并发用户数的变化,吞吐率、用户平均请求等待时间、服务器配件请求处理时间都发生了相应的变化。

posted @ 2015-05-26 14:43  zhhtao  阅读(3790)  评论(0编辑  收藏  举报