使用Apache ab工具测试性能

Posted by Kaka Blog on December 14, 2018

ab简介

ab是Apache提供的一款小巧的压力测试工具,用来测试服务能力很方便。

安装

下载Apache,地址:http://httpd.apache.org/download.cgi#apache24,点击Files for Microsoft Windows链接,点击ApacheHaus链接,选择版本下载。

直接解压到具体目录,找到bin目录,将路径加到系统环境变量Path里即可使用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 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
    -l              Accept variable document length (use this for dynamic pages)
    -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.
    -m method       Method name
    -h              Display usage information (this message)

常用参数说明:

-n    请求的总数,默认是1次。
-c    并发数,同一时间请求数。
-p    如果后面接的是文件,表示POST请求,文件中包含请求数据,根据数据格式,设置-T参数。
-T    设置请求头中的Content-type(针对POST/PUT),比如:application/x-www-form-urlencoded。默认是text/plain。
-w    将测试结果打印到HTML表格中。

结果解释:

Time taken for tests:   0.914 seconds    所有这些请求处理完成所花费的时间 
Complete requests:      800             完成请求数 
Failed requests:        0                失败请求数 
Write errors:           0                
Non-2xx responses:      800 
Total transferred:      393600 bytes     网络总传输量 
HTML transferred:       252000 bytes     HTML内容传输量 
Requests per second:    875.22 [#/sec] (mean) 吞吐量-每秒请求数 
Time per request:       914.052 [ms] (mean)  服务器收到请求,响应页面要花费的时间 
Time per request:       1.143 [ms] (mean, across all concurrent requests) 并发的每个请求平均消耗时间 
Transfer rate:          420.52 [Kbytes/sec] received 平均每秒网络上的流量,可以帮助排除是否存在网络流量过大导致响应时间延长的问题

网络上消耗的时间的分解: 
Connection Times (ms) 
            min  mean[+/-sd] median   max 
Connect:        0    1   0.5      1       3 
Processing:   245  534 125.2    570     682 
Waiting:       11  386 189.1    409     669 
Total:        246  535 125.0    571     684

整个场景中所有请求的响应情况。在场景中每个请求都有一个响应时间 
其中 50% 的用户响应时间小于 571 毫秒 
80 % 的用户响应时间小于 652 毫秒 
最大的响应时间小于 684 毫秒 

参考

ab - Apache HTTP server benchmarking tool - Apache HTTP Server