Web性能测试工具之ab入门篇

1. ab简介

ab全称Apache Bench,是apache附带的一个小工具,它可以同时模拟多个并发请求,测试apache等Web服务器的最大负载压力。
本文通过一个简单的示例,介绍了使用ab进行web页面性能测试、查看结果方法及运行参数详解。
运行环境为Windows 10系统。

2. ab下载

2.1 Windows系统

对于windows系统,可访问页面https://www.apachelounge.com/download/下载相应版本的zip包,我们下载的版本是:httpd-2.4.29-Win64-VC15.zip
也可以下载我提取出来的ab.exe,解压即可使用:https://files.cnblogs.com/files/lovesoo/ab.zip

2.2 linux系统

使用如下命令安装Apache
yum install httpd

3. ab运行

使用cmd/shell进入到Apache24in目录下,运行ab即可
 

4. 使用ab进行性能测试

 
最常用两种运行命令如下:
ab -n 10 -c 10 http://www.cnblogs.com
# -n 总共10次请求,-c 模拟10个并发用户,即10个并发请求博客园首页,共请求10次
 
ab -t 10 -c 10 http://www.cnblogs.com
# -t 是测试执行时间,-c 模拟10个并发用户,即10个并发请求博客园首页,持续10秒

5. 测试结果

6. 结果解析

Server Software:
Server Hostname:        www.cnblogs.com
Server Port:            80

Document Path:          /
Document Length:        147 bytes    #HTTP响应数据的正文长度

Concurrency Level:      10    #并发数
Time taken for tests:   10.183 seconds    #测试执行时间
Complete requests:      169    #完成请求数
Failed requests:        0    #失败请求数
Non-2xx responses:      169
Total transferred:      65403 bytes    #网络总传输量
HTML transferred:       24843 bytes    #HTML内容传输量
Requests per second:    16.60 [#/sec] (mean)    #每秒请求数
Time per request:       602.545 [ms] (mean)    #用户平均请求等待时间,计算公式:测试执行总时间/(总请求数/并发用户数)
Time per request:       60.254 [ms] (mean, across all concurrent requests)    #服务器平均请求等待时间,计算公式:测试执行总时间/总请求数
Transfer rate:          6.27 [Kbytes/sec] received    #平均传输速率

Connection Times (ms)    #响应时间小、中、大值
              min  mean[+/-sd] median   max
Connect:        2   40 325.5      3    3006
Processing:    41  382 928.1     63    3072
Waiting:       41  376 922.1     58    3063
Total:         43  422 971.5     68    3075

Percentage of the requests served within a certain time (ms)    #一定时间内请求完成的百分比,如50%用户完成的响应时间在68ms内, 66%用户完成的响应时间在73ms内,100%用户在3075ms内完成
  50%     68
  66%     73
  75%     81
  80%     85
  90%   3060
  95%   3066
  98%   3069
  99%   3075
 100%   3075 (longest request)

7. 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 即requests,用于指定压力测试/总请求数。
-c 即concurrency,用于指定压力测试的并发数。
-t 即timelimit,测试执行最大秒数,它可以让测试限制在一个固定的总时间以内,默认值为50000。
-s 即timeout,请求最大等待时长,默认30s
-b 即windowsize,TCP发送/接收的缓冲大小(单位:字节)。
-p 即postfile,发送POST请求时需要上传的文件,文件格式如"p1=1&p2=2"。使用方法是 -p 123.txt 。 (配合-T)
-u 即putfile,发送PUT请求时需要上传的文件。(配合-T)
-T 即content-type,用于设置Content-Type请求头信息,如 -T "application/x-www-form-urlencoded”,默认值为text/plain。(配合-p)
-v 即verbosity,设置显示信息的详细程度 – 4或更大值会显示头信息, 3或更大值可以显示响应代码(404, 200等), 2或更大值可以显示警告和其他信息。
-w 以HTML表格形式打印结果。
-i 使用HEAD请求代替GET请求。
-x 插入字符串作为table标签的属性。
-y 插入字符串作为tr标签的属性。
-z 插入字符串作为td标签的属性。
-C 添加cookie信息,例如:"Apache=1234"。此参数可以重复,用逗号分割。提示:可以借助session实现原理传递 JSESSIONID参数, 实现保持会话的功能,如-C "c1=1234,c2=2,c3=3, JSESSIONID=FF056CD16DA9D71CB131C1D56F0319F8"-H 添加任意的请求头,例如:"Accept-Encoding: gzip",请求头将会添加在现有的多个请求头之后(可以重复该参数选项以添加多个)。
-A 添加一个基本的网络认证信息,用户名和密码之间用英文冒号隔开。
-P 添加一个基本的代理认证信息,用户名和密码之间用英文冒号隔开。如-P proxy-auth-username:password
-X 指定使用的代理服务器和端口号,例如:"126.10.10.3:88"-V 显示版本号并退出。
-k 使用HTTP的KeepAlive特性。
-d 不显示百分比。
-S 不显示预估和警告信息。
-q 超过150个请求后不显示进度
-l 接受可变文档长度(用于动态页面)
-g filename 输出结果信息到gnuplot格式的文件中。
-e filename 输出结果信息到CSV格式的文件中。
-r 指定接收到错误信息时不退出程序。
-m method 方法名
-h 帮助
原文地址:https://www.cnblogs.com/lovesoo/p/7762796.html