ab是Apache超文本传输协议(HTTP)的性能测试工具。其设计意图是描绘当前所安装的Apache的执行性能,主要是显示你安装的Apache每秒可以处理多少个请求 。

概要

ab [ -A auth-username ] [ -c concurrency ] [ -C cookie-name=value ] [ -d ] [ -e csv-file ] [ -g gnuplot-file ] [ -h ] [ -H custom-header ] [ -i ] [ -k ] [ -n requests ] [ -p POST-file ] [ -P proxy-auth-username ] [ -q ] [ -s ] [ -S ] [ -t timelimit ] [ -T content-type ] [ -v verbosity] [ -V ] [ -w ] [ -x <table>-attributes ] [ -X proxy[] ] [ -y <tr>-attributes ] [ -z <td>-attributes ] [http://]hostname[:port]/path


一般把apache压力测试称为AB测试. ab工具的位置在apache2的bin目录里.ab的使用是这样的:  
$Content$nbsp;./ab
./ab: wrong number of arguments
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)


选项

-A auth-username:password
对服务器提供BASIC认证信任。用户名和密码由一个:隔开,并以base64编码形式发送。无论服务器是否需要(即, 是否发送了401认证需求代码),此字符串都会被发送。


-c concurrency
一次产生的请求个数。默认是一次一个。



-C cookie-name=value
对请求附加一个Cookie:行。 其典型形式是name=value的一个参数对。此参数可以重复。


-d
不显示"percentage served within XX [ms] table"的消息(为以前的版本提供支持)。


-e csv-file
产生一个以逗号分隔的(CSV)文件,其中包含了处理每个相应百分比的请求所需要(从1%到100%)的相应百分比的(以微妙为单位)时间。由于这种格式已经“二进制化”,所以比'gnuplot'格式更有用。


-g gnuplot-file
把所有测试结果写入一个'gnuplot'或者TSV (以Tab分隔的)文件。此文件可以方便地导入到Gnuplot, IDL, Mathematica, Igor甚至Excel中。其中的第一行为标题。


-h
显示使用方法。


-H custom-header
对请求附加额外的头信息。此参数的典型形式是一个有效的头信息行,其中包含了以冒号分隔的字段和值的对 (如, "Accept-Encoding: zip/zop;8bit").


-i
执行HEAD请求,而不是GET。


-k
启用HTTP KeepAlive功能,即, 在一个HTTP会话中执行多个请求。默认时,不启用KeepAlive功能.


-n requests
在测试会话中所执行的请求个数。默认时,仅执行一个请求,但通常其结果不具有代表意义。



-p POST-file
包含了需要POST的数据的文件.


-P proxy-auth-username:password
对一个中转代理提供BASIC认证信任。用户名和密码由一个:隔开,并以base64编码形式发送。无论服务器是否需要(即, 是否发送了401认证需求代码),此字符串都会被发送。


-q
如果处理的请求数大于150, ab每处理大约10%或者100个请求时,会在stderr输出一个进度计数。此-q标记可以抑制这些信息。


-s
用于编译中(ab -h会显示相关信息)使用了SSL的受保护的https,而不是http协议的时候。此功能是实验性的,也是很简陋的。最好不要用。


-S
不显示中值和标准背离值,而且在均值和中值为标准背离值的1到2倍时,也不显示警告或出错信息。默认时,会显示最小值/均值/最大值等数值。(为以前的版本提供支持).


-t timelimit
测试所进行的最大秒数。其内部隐含值是-n 50000。它可以使对服务器的测试限制在一个固定的总时间以内。默认时,没有时间限制。


-T content-type
POST数据所使用的Content-type头信息。


-v verbosity
设置显示信息的详细程度 - 4或更大值会显示头信息, 3或更大值可以显示响应代码(404, 200等), 2或更大值可以显示警告和其他信息。


-V
显示版本号并退出。


-w
以HTML表的格式输出结果。默认时,它是白色背景的两列宽度的一张表。


-x <table>-attributes
设置<table>属性的字符串。 此属性被填入<table 这里 >.


-X proxy[:port]
对请求使用代理服务器。


-y <tr>-attributes
设置<tr>属性的字符串.


-z <td>-attributes
设置<td>属性的字符串.

缺陷
程序中有各种静态声明的固定长度的缓冲区。另外,对命令行参数、服务器的响应头和其他外部输入的解析也很简单,这可能会有不良后果。

它没有完整地实现HTTP/1.x; 仅接受某些'预想'的响应格式。 strstr(3)的频繁使用可能会带来性能问题,即, 你可能是在测试ab而不是服务器的性能


参数很多,一般我们用 -c 和 -n 参数就可以了. 例如:

./ab -c 100 -n 10000 http://127.0.0.1/index.php

这个表示同时处理100个请求并运行10000次index.php文件.



# ./ab -c 100 -n 10000 http://127.0.0.1/index.php
This is ApacheBench, Version 2.0.41-dev http://www.zeustech.net/
Copyright (c) 1998-2002 The Apache Software Foundation, http://www.apache.org/

Benchmarking 127.0.0.1 (be patient)
Completed 1000 requests
Completed 2000 requests
Completed 3000 requests
Completed 4000 requests
Completed 5000 requests
Completed 6000 requests
Completed 7000 requests
Completed 8000 requests
Completed 9000 requests
Finished 10000 requests

Server Software:        Apache/2.0.53
Server Hostname:        127.0.0.1
Server Port:            80

Document Path:          /index.php
Document Length:        17998 bytes

Concurrency Level:      100
Time taken for tests:   660.930623 seconds
Complete requests:      10000
Failed requests:        0
Write errors:           0
Total transferred:      181850000 bytes
HTML transferred:       179980000 bytes
Requests per second:    15.13 [#/sec] (mean)
Time per request:       6609.306 [ms] (mean)
Time per request:       66.093 [ms] (mean, across all concurrent requests)
Transfer rate:          268.69 [Kbytes/sec] received

Connection Times (ms)
             min  mean[+/-sd] median   max
Connect:        0   15 117.6      0    1730
Processing:   436 6564 1142.2   6517   15395
Waiting:      340 4164 1512.2   3906   14973
Total:        436 6579 1143.4   6526   15395

Percentage of the requests served within a certain time (ms)
50%   6526
66%   6972
75%   7262
80%   7377
90%   7755
95%   8295
98%   8683
99%   9815
100%  15395 (longest request)

以上结果指出,在同时间 100个并发联机请求的情况下,完成 10000 次的联机请求,共花了 660.930623 秒 (Time taken for tests),因此这个程序每秒平均可以处理 (Requests per second) 15.13 个联机请求。