网站性能测试工具Apache Benchmark的使用说明

时间:2022-05-19 11:36:03

http://httpd.apache.org/docs/2.0/programs/ab.html

在网站开发中,有时候需要对网站进行性能测试,知道网站在一定的并发访问下的响应时间等情况,这里介绍Apache的一个工具,Apache Benchmark,可以比较方便的对网站做一些性能测试

简介:

Apache Benchmark是Apache的http服务器httpd的附带软件,到http://httpd.apache.org/下载httpd服务器,安装完成后,会在bin目录下找到ab.exe,

运行ab –h,可以看到ab的完整说明如下

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

-b windowsize   Size of TCP send/receive buffer, in bytes

-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 for POSTing, 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.

-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)

对于最基本的使用,假设本地有一个http://localhost/phptest.php显示phpinfo,可以使用-c和-n参数,

运行如下命令测试当并发请求是10个,总共请求是100个的时候的性能数据,

ab -n 1000 -c 100 http://localhost/phptest.php

该测试在我本机结果如下:

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)

Server Software:        Apache/2.2.14

Server Hostname:        localhost

Server Port:            80

Document Path:          /phptest.php

Document Length:        43931 bytes

Concurrency Level:      100

Time taken for tests:   7.563 seconds

Complete requests:      1000

Failed requests:        0

Write errors:           0

Total transferred:      44099000 bytes

HTML transferred:       43931000 bytes

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

Time per request:       756.250 [ms] (mean)

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

Transfer rate:          5694.60 [Kbytes/sec] received

Connection Times (ms)

min  mean[+/-sd] median   max

Connect:        0    4  16.3      0     469

Processing:   203  706 247.4    656    1547

Waiting:      172  381 168.9    328    1125

Total:        203  710 248.1    656    1563

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

50%    656

66%    688

75%    688

80%    719

90%   1172

95%   1375

98%   1422

99%   1453

100%   1563 (longest request)

结果比较简单,重点是红色部分,意思是说在100个并发请求下,完成1000个请求,总共需要7.5秒,1000个请求全部完成,没有失败;其中,平均用时710毫秒,标准差是248毫秒,中位数【就是50%请求时间】是656毫秒,90%请求时间是1172毫秒,就是1.172秒,最慢的请求用了1.563秒;

基本上,可以通过调整这-n和-c两个参数来确定系统可以支撑的并发量。

另外,如果是动态页面,比如需要登陆以后才能看到的页面,可以先在Firefox中打开页面,然后通过firebug查看session相关的Cookie,然后使用-C的参数指定,就可以假定用户登陆了:

ab -c 100 -n 1000 -C JSESSIONID=1CBDB188AB4C901C2C3DC2C5BAEA1F47 http://localhost:9080/myapp

当然,ab还有很多缺陷,比如不能模拟多种不同类型的用户做不同的操作,不能模拟实际的用户思考时间等,缺乏图形化的工具显示结果等等。其他有些工具克服了这些缺陷。