Apache性能测试工具ab

来源:互联网 发布:网络服务器是指 编辑:程序博客网 时间:2024/04/29 19:03

如果要进行网站的压力测试,Apache本身自带ab工具可以很方便的进行简单的测试。

ab是apachebench的缩写,通过ab工具,可以实现创建多个并发访问线程,模拟多个访问者同时对某一URL地址进行访问。它的测试目标是基于URL的,因此,它既可以用来测试apache的负载压力。ab命令对发出负载的计算机要求很低,它既不会占用很高CPU,也不会占用很多内存。但却会给目标服务器造成巨大的负载,其原理类似CC攻击。自己测试使用也需要注意,否则一次上太多的负载。可能造成目标服务器资源耗完,严重时甚至导致死机。

安装Apache之后,一般情况下ab命令就可以使用了,Windows下在Apache安装目录下C:\apache\bin\,可以找到ab.exe文件,linux下可直接运行ab命令,一般存放在/usr/local/apache2/bin目录下,如果你的Apache不是源码安装或者找不到ab命令,也可以直接安装apache的工具包httpd-tools,使用以下命令:yum -y install httpd-tools。安装完毕后执行ab -V可正确查看ab工具版本,就可以使用了。

关于ab命令的参数,可以使用ab -h来查看:

[root@iZ281hdlfydZ ~]# 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

    -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

    -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 10 -n 500 http://121.**.171.5/weiphp/

其中,-c 10表示并发用户数为10,-n 500表示请求总数为500,http://121.**.171.5/weiphp/表示请求的目标URL,也就是向百度的主页同时进行500个请求并运行10次。运行此命令之后会得到如下结果:

[root@iZ281hdlfydZ ~]# ab -c 10 -n 500 http://121.**.171.5/weiphp/

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 121.**.171.5 (be patient)



Completed 100 requests

Completed 200 requests

Completed 300 requests

Completed 400 requests

Completed 500 requests

Finished 500 requests





Server Software:        nginx

Server Hostname:        121.**.171.5

Server Port:            80



Document Path:          /weiphp/

Document Length:        0 bytes



Concurrency Level:      10

Time taken for tests:   6.751 seconds

Complete requests:      500

Failed requests:        0

Write errors:           0

Non-2xx responses:      500

Total transferred:      211500 bytes

HTML transferred:       0 bytes

Requests per second:    74.06 [#/sec] (mean)

Time per request:       135.022 [ms] (mean)

Time per request:       13.502 [ms] (mean, across all concurrent requests)

Transfer rate:          30.59 [Kbytes/sec] received



Connection Times (ms)

              min  mean[+/-sd] median   max

Connect:        0    1   1.3      0      17

Processing:    28  133  24.1    134     234

Waiting:       27  133  24.1    134     234

Total:         28  134  24.1    136     234



Percentage of the requests served within a certain time (ms)

  50%    136

  66%    144

  75%    148

  80%    151

  90%    159

  95%    172

  98%    187

  99%    204

 100%    234 (longest request)


Server Software表示被测试的Web服务器软件名称。

Server Hostname表示请求的URL主机名。

Server Port表示被测试的Web服务器软件的监听端口。

Document Path表示请求的URL中的根绝对路径,通过该文件的后缀名,我们一般可以了解该请求的类型。

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

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

Time taken for tests表示所有这些请求被处理完成所花费的总时间。

Complete requests表示总请求数量,这是我们设置的参数之一。

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

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表示这些请求在单位时间内从服务器获取的数据长度,计算公式:Total trnasferred/ Time taken for tests,这个统计很好的说明服务器的处理能力达到极限时,其出口宽带的需求量。

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


下面附上ab性能指标比较重要的几项说明:

1、吞吐率(Requests per second)

服务器并发处理能力的量化描述,单位是reqs/s,指的是在某个并发用户数下单位时间内处理的请求数。某个并发用户数下单位时间内能处理的最大请求数,称之为最大吞吐率。记住:吞吐率是基于并发用户数的。这句话代表了两个含义:

a、吞吐率和并发用户数相关

b、不同的并发用户数下,吞吐率一般是不同的

计算公式:总请求数/处理完成这些请求数所花费的时间,即Request per second=Complete requests/Time taken for tests,必须要说明的是,这个数值表示当前机器的整体性能,值越大越好。

2、并发连接数(The number of concurrent connections)

并发连接数指的是某个时刻服务器所接受的请求数目,简单的讲,就是一个会话。

3、并发用户数(Concurrency Level)

要注意区分这个概念和并发连接数之间的区别,一个用户可能同时会产生多个会话,也即连接数。在HTTP/1.1下,IE7支持两个并发连接,IE8支持6个并发连接,FireFox3支持4个并发连接,所以相应的,我们的并发用户数就得除以这个基数。

4、用户平均请求等待时间(Time per request)

计算公式:处理完成所有请求数所花费的时间/(总请求数/并发用户数),即:Time per request=Time taken for tests/(Complete requests/Concurrency Level)

5、服务器平均请求等待时间(Time per request:across all concurrent requests)

计算公式:处理完成所有请求数所花费的时间/总请求数,即:Time taken for/testsComplete requests,可以看到,它是吞吐率的倒数。同时,它也等于用户平均请求等待时间/并发用户数,即Time per request/Concurrency Level





0 0
原创粉丝点击