使用 ApacheBench 进行网站的压力测试 - tda7088的日志 - 网易博客

来源:百度文库 编辑:神马文学网 时间:2024/04/28 07:54:24

使用 ApacheBench 进行网站的压力测试

默认分类 2009-11-06 18:44:24 阅读1 评论0 字号:大中

使用 ApacheBench 进行网站的压力测试

ApacheBench 工具程序是 Apache 网站服务器软件的一个附带的工具软件,专门用来执行网站服务器的运行效能,特别是针对 Apache 网站服务器 的效能分析。这支程序原本是用来检测 Apache 网站服务器(Web Server) 所能够提供的效能,特别是可以看出 Apache 网站服务器能提供每秒能送出多少网页,当然的,也可以用在任何其它的网站服务器,例如说:IISlighttpd

你可以到 Apache HTTP Server 网站下载最新版,如果你要在 Windows 的环境执行 ApacheBench 可以直接下载 Win32 Binary 的版本就好,由于在线所提供的版本是 MSI 的封装档,安装好之后也等同于在你的计算机内安装了一套 Apache HTTP Server,如果你不需要多执行一套 Apache HTTP Server 的话,你可以在安装好之后进入 C:\Program Files\Apache Group\Apache2\bin 目录,找到 ab.exe 执行档,复制出来后再移除 Apache 安装即可,因为 ab.exe 是可以独立执行的,不需要任何关连的 dll 檔。

底下是 ab.exe 的使用参数摘要说明,若要看详细说明可以看这里(或中文翻译):

C:\>ab -h
Usage: ab [options] [http://]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
    -p postfile     File containing data to POST
    -T content-type Content-type header for POSTing
    -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
    -h              Display usage information (this message)

而我经常使用的参数摘要如下:

1. 同时 10 个联机,连续点击 10000 次 ( 每个 Request 执行完毕后都会自动断线,然后再重新联机 )

ab -n 10000 -c 10 http://www.example.com/index.aspx

2. 同时 10 个联机,连续点击 10000 次,并且使用 Keep-Alive 方式联机(当 Web Server 有支持 Keep-Alive 功能时 ApacheBench 会在同一个联机下连续点击该网页)

ab -n 10000 -c 10 -k http://www.example.com/index.aspx

3. 将测试的效能原始资料汇出成 CSV 文件

ab -e output.csv -n 10000 -c 10 http://www.example.com/index.aspx

   汇出的 output.csv 内容如下:

Percentage served,Time in ms
0,6.200000e+001
1,6.200000e+001
2,6.200000e+001
3,6.200000e+001
4,6.200000e+001
5,6.200000e+001
6,6.200000e+001
7,6.200000e+001
8,6.200000e+001
9,6.200000e+001
10,6.200000e+001
11,6.200000e+001
12,6.200000e+001
13,6.200000e+001
14,6.200000e+001
......

   上表所代表的每一列代表送出的百分比,第二个字段是当下的 "Time per request" (每个要求所花费的时间),单位是豪秒(millisecond)。

如何有效的检视结果

压力测试的核心在于如何分析结果,底下我用一个测试的结果说明每个字段所代表的意义。如果你只要看重点的话,可以看 Failed requests、Requests per second、与 Time per request 这三个参数也就差不多够了。其中的 Failed requests 的数量太高的话,很有可能代表你的 Web Application 的稳定度不够,而导致使用大量要求时无法响应需求 。而 Request per second 代表你每表可送出的响应数有多少,代表你 Web Application 的承载量有多少(在不考虑频宽限制的情况下)。

C:\>ab -d -e a.csv -v 1 -n 1000 -c 3 http://www.example.com/index.aspx
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) 2006 The Apache Software Foundation, http://www.apache.org/

Benchmarking www.m-taoyuan.tw (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:        Microsoft-IIS/6.0
Server Hostname:        www.m-taoyuan.tw
Server Port:            80

Document Path:          /index.aspx
Document Length:        25986 bytes

Concurrency Level:      3
Time taken for tests:   25.734375 seconds
Complete requests:      1000
Failed requests:        0
Write errors:           0
Total transferred:      26372000 bytes
HTML transferred:       25986000 bytes
Requests per second:    38.86 [#/sec] (mean)
Time per request:       77.203 [ms] (mean)
Time per request:       25.734 [ms] (mean, across all concurrent requests)
Transfer rate:          1000.72 [Kbytes/sec] received

Connection Times (ms)
              min mean[+/-sd] median   max
Connect:        0    1   4.4      0      15
Processing:    62   75   9.1     78     109
Waiting:       46   64   8.0     62     109
Total:         62   76   9.3     78     109

如上表显示的结果来说明,我一个字段一个字段的讲解如下:

    * Server Software:    Web主机的操作系统与版本(若Web主机设定关闭此信息则无)
    * Server Hostname: Web主机的IP地址(Hostname)
    * Server Port:           Web主机的连接端口(Port)
    * Document Path:     测试网址的路径部分
    * Document Length: 测试网页响应的网页大小
    * Concurrency Level: 同时进行压力测试的人数
    * Time taken for tests: 本次压力测试所花费的总秒数
    * Complete requests: 完成的要求数(Requests)
    * Failed requests:      失败的要求数(Requests)
    * Write errors:           写入失败的数量
    * Total transferred:   本次压力测试的总数据传输量(包括 HTTP Header 的数据也计算在内)
    * HTML transferred: 本次压力测试的总数据传输量(仅计算回传的 HTML 的数据)
    * Requests per second: 平均每秒可响应多少要求
    * Time per request: 平均每个要求所花费的时间(单位: 豪秒)
    * Time per request: 平均每个要求所花费的时间,跨所有同时联机数的平均值(单位: 豪秒)
    * Transfer rate:         从 ab 到 Web Server 之间的网络传输速度

最后的 Connection Times (ms) 指的是压力测试时的联机处理时间:

横轴字段的部分:

    * min:       最小值
    * mean:    平均值(正、负标准差)
    * median: 平均值(中间值)
    * max:      最大值

纵轴字段的部分:

    * Connect:     从 ab 发出 TCP 要求到 Web 主机所花费的建立时间。
    * Processing: 从 TCP 联机建立后,直到 HTTP 响应(Response)的数据全部都收到所花的时间。
    * Waiting:       从发送 HTTP 要求完后,到 HTTP 响应(Response)第一个 Byte 所等待的时间。
    * Total:           等于 Connect + Processing 的时间(因为 Waiting 包含在 Processing 时间内了)

压力测试的基本观念

    * 排除频宽的限制
          o 做压力测试通常不会考虑「频宽的限制」,所以一般来说不会将测试的主机摆在远程机房、然后测试程序摆在公司内部的主机,而是会将压力测试的 Client 跟 Web 主机摆在同一个网段下进行压力测试。
          o 因为「频宽」只要花钱就会有了,但是主机的承载量却是有限的,从远程进行压力测试主要的限制是在「频宽」而非「效能」,所以从远程单点进行压力测试毫无任何意义可言,这样是测不出主机的效能极限的。
          o 如果你有能力与资源进行大规模(多点)压力测试的话,透过远程进行压力测试才有意义。
    * 压力要循序渐进
          o 你不要一下字就执行同时联机数 100 人,而是要循序渐进的慢慢加同时联机数上去,才不会让 Web Application 一下字承受过大的负载而导致效能的数据不正确(例如说 Failed requests 过高),但这只是建议,你也可以一下子操死你的主机,反正你在测主机的极限嘛!