使用ApacheBench做网站压力测试

来源:互联网 发布:linux 输入中文 编辑:程序博客网 时间:2024/05/17 01:09

使用ApacheBench进行网站压力测试,是较为简单方便的方法。该工具软件可在Apache的bin目录下找到,文件名为ab,用法介绍如下:

Usage: ./ab [options] [http://]hostname[:port]/path
Options are:
-n requests     全部请求数
-c concurrency  并发数
-t timelimit    最传等待回应时间
-p postfile     POST数据文件
-T content-type POST Content-type
-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    加入cookie, eg. 'Apache=1234. (repeatable)
-H attribute    加入http头, eg. 'Accept-Encoding: gzip'
                Inserted after all normal header lines. (repeatable)
-A attribute    http验证,分隔传递用户名及密码
-P attribute    Add Basic Proxy Authentication, the attributes
                are a colon separated username and password.
-X proxy:port   代理服务器
-V              查看ab版本
-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  保存成CSV文件
-h              Display usage information (this message)

 

常用的方法是:

ab -n 全部请求数 -c 并发数 测试url

以下是一个测试的结果:

Server Software:        Apache/2.0.55
Server Hostname:        localhost
Server Port:            80

Document Path:          /1.php
Document Length:        82522 bytes  #请求文档大小

Concurrency Level:      50           #并发数 
Time taken for tests:   92.76140 seconds #全部请求完成耗时
Complete requests:      10000          #全部请求数
Failed requests:        1974           #大家最关心的指标之一,失败的请求数,可用于表示服务器稳定性
  (Connect: 0, Length: 1974, Exceptions: 0)
Write errors:           0
Total transferred:      827019400 bytes #整个场景中的网络传输量
HTML transferred:       825219400 bytes #整个场景中的HTML内容传输量
Requests per second:    108.61 [#/sec] (mean) #大家最关心的指标之二,相当于 LR 中的 每秒事务数 ,后面括号中的 mean 表示这是一个平均值
Time per request:       460.381 [ms] (mean) #大家最关心的指标之三,相当于 LR 中的 平均事务响应时间 ,后面括号中的 mean 表示这是一个平均值(所有并发)
Time per request:       9.208 [ms] (mean, across all concurrent requests)  #每一请求时间(并发平均) 
Transfer rate:          8771.39 [Kbytes/sec] received #传输速率

Connection Times (ms) #连接时间
             min  mean[+/-sd] median   max
Connect(#连接):        0    0   2.1      0      46
Processing(#处理):    31  458  94.7    438    1078
Waiting(#等待):       15  437  87.5    422     938
Total:         31  458  94.7    438    1078

 

ab还提供了POST方式的测试,要post出去的参数信息需要从一个外部文件读入:

在本地生成post.txt文件
内容为param1=abc&param2=def

同时需要设置contentType,使用参数 -T ,并设置参数值为 application/x-www-form-urlencoded
完整的ab post测试命令如下:
ab -n 1 -p post.txt -T ‘application/x-www-form-urlencoded’ http://192.168.0.2/test.jsp

 

压力测试的基本观念

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