Apache Bench (ab) 使用说明


ApacheBench 是 Apache 软件基金会的一个开放源代码的网页服务器软件,可以在大多数电脑操作系统中运行。它是一个指令列程式,专门用来测试网站服务器的运行效能。

使用方法

1
ab [options] [http://]hostname[:port]/path

Options

参数命令|参数|说明 ---|:--:|---: -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 requests 要执行的请求数
-c concurrency 同一时间发出的请求个数(并发数)
-t timelimit 在基准测试上花费最多的秒数,这意味着-n 50000
-s timeout 等待每个响应的最长秒数
默认:30秒
-b windowsize TCP发送/接收缓冲区的大小(字节)
-B address 进行传出连接时要绑定到的地址
-p postfile 用 POST 发送文件,请设置-T
-u putfile 用 PUT 发送文件,请设置-T
-T content-type Content-type header to use for POST/PUT data
默认:'text/plain'
-v verbosity 要打印多少故障排除信息
-w 在 HTML 表格中打印出结果
-i 使用 head 替代 get
-x attributes 要作为表属性插入的字符串
-y attributes 要作为tr属性插入的字符串
-z attributes 要作为td或th属性插入的字符串
-C attribute 添加cookie,例如“apache=1234”。(可重复)
-H attribute 添加任意标题行,例如在所有普通标题行后插入“accept encoding:gzip”。(可重复)
-A attribute 添加基本的WWW认证,属性是以冒号分隔的用户名和密码。
-P attribute 添加基本代理身份验证,属性是以冒号分隔的用户名和密码。
-X proxy:port 要使用的代理服务器和端口号
-V 打印版本号并退出
-k 使用 HTTP 保持连接功能
-d 不要显示服务台的百分比。
-S 不要显示置信估计值和警告。
-q 执行超过150个请求时不显示进度
-l 接受可变文档长度(用于动态页面)
-g filename 将收集的数据输出到gnuplot格式文件。
-e filename 输出提供百分比的csv文件
-r 在套接字接收错误时不退出
-m method 方法名
-h 显示使用信息(this message)

例子解析

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
Benchmarking 10.0.0.146
Completed 1000 requests
...
Finished 10000 requests

(已经完成的请求)

Server Software: # 服务器软件
Server Hostname: # 服务器 IP 地址
Server Port: # 服务器端口

Document Path: # 文件路径
Document Length: # 文件大小

Concurrency Level: # 并发等级
Time taken for tests: # 测试用时(秒)
Complete requests: # 完成的请求数
Failed requests: # 失败的请求数
Total transferred: # 整个场景中的网络传输量
HTML transferred: # 整个场景中的HTML内容传输量
Requests per second: # 吞吐率,相当于 LR 中的每秒事务数
Time per request: # 用户平均请求等待时间,相当于 LR 中的平均事务响应时间
Time per request: # 每个连接请求实际运行时间的平均值
Transfer rate: # 平均每秒网络上的流量,可以帮助排除是否存在网络流量过大导致响应时间延长的问题


Win 系统
Win 系统需要到 Apache 官网下载软件包。

Mac 系统
Mac 系统自带 Apache

Linux 系统

1
2
3
user@ubuntu:~$ ab
程序 "ab" 尚未安装。您可以使用以下命令安装:
user@ubuntu:~$ sudo apt-get install apache2-utils

关于登录的问题
有时候进行压力测试需要用户登录,怎么办?
请参考以下步骤:

先用账户和密码登录后,用开发者工具找到标识这个会话的Cookie值(Session ID)记下来
如果只用到一个Cookie,那么只需键入命令:
ab -n 100 -C key=value http://test.com/

如果需要多个Cookie,就直接设Header:
ab -n 100 -H “Cookie: Key1=Value1; Key2=Value2” http://test.com/

NodeJS 分布式和多进程的测试与记录 Markdown 基本语法
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×