前言
ab全称Apache HTTP server benchmarking tool的缩写,是Apache自带的网站benchmark测试工具。使用非常方便,简约而不简单。
官方网站:https://httpd.apache.org/docs/2.4/programs/ab.html
TIPS
可达到ab相同效果的工具:
安装
macOS
系统自带,直接使用即可。
CentOS 7
运行如下命令即可安装
1
| yum -y install httpd-tools
|
Windows
TIPS
由于笔者没有Windows环境,所以此步骤无法亲测。
亲们可百度 “Windows 安装 ab”,参照结果安装即可。资料非常丰富。
使用说明
ab是一个命令行工具,使用说明如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
| ➜ ab -help
Usage: ab [options] [http[s]://]hostname[:port]/path
Options are:
-n requests 指定请求多少次
-c concurrency 指定并发数(指定同时发送几个请求)
-t timelimit 测试持续的最长时间,默认无限制,此参数隐含-n 50000
-s timeout 每个请求的超时时间,默认30秒
-b windowsize 指定TCP收发缓存大小,单位字节
-B address 指定在发起连接时绑定的IP地址
-p postfile 指定想要POST的文件,需和-T参数配合使用
-u putfile 指定想要PUT的文件,需和-T参数配合使用
-T content-type 指定POST/PUT文件时的Content-type。默认text/plain
-v verbosity 详细模式,打印更多日志
-w 将结果输出到html表格中
-i 使用HEAD方式代替GET发起请求
-x attributes 插入字符串作为table标签的属性
-y attributes 插入字符串作为tr标签的属性
-z attributes 插入字符串作为td或th标签的属性
-C attribute 添加Cookie,例如Apache=1234;可重复该参数选项以添加多个Cookie
-H attribute 添加任意的请求头,例如Accept-Encoding: gzip;可重复该参数选项以添加多个
-A attribute 添加Basic WWW认证信息,用户名和密码之间用:分隔
-P attribute 添加Basic Proxy认证信息,用户名和密码之间用:分隔
-X proxy:port 指定代理地址
-V 打印ab的版本信息
-k 使用HTTP的KeepAlive特性
-d 不显示百分比
-S 不显示预估和警告信息
-q 默认情况下,如果处理的请求数大于150,ab每处理大约10%或者100个请求时,打印一下进度信息。使用该参数后则不打印进度。
-g filename 输出结果信息到gnuplot格式的文件中
-e filename 输出结果信息到CSV格式的文件中
-r 指定接收到错误信息时不退出程序
-h 显示使用说明
-Z ciphersuite 指定SSL/TLS密码套件
-f protocol 指定SSL/TLS协议(SSL3, TLS1, TLS1.1, TLS1.2 or ALL)
|
示例
1
| ab -c 100 -n 10000 http://www.baidu.com/
|
表示并发100,请求baidu 10000次。
TIPS
http://www.baidu.com/ 不能写成 http://www.baidu.com 。
这是因为 http://www.baidu.com 不符合ab要求的 [http[s]://]hostname[:port]/path 格式要求。这是个小坑,需要注意一下。
报表解读
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
| ➜ ab -c 10 -n 1000 http://www.baidu.com/
This is ApacheBench, Version 2.3 <$Revision: 1843412 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking www.baidu.com (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Completed 600 requests
Completed 700 requests
Completed 800 requests
Completed 900 requests
Completed 1000 requests
Finished 1000 requests
# 展示测试地址所使用的服务器软件及版本 Server Software: BWS/1.1 # 测试地址的主机名 Server Hostname: www.baidu.com # 测试地址的端口 Server Port: 80 # 测试地址的路径 Document Path: / # 测试地址的文档大小 Document Length: 255598 bytes # 并发数 Concurrency Level: 10 # 测试花费了多久 Time taken for tests: 81.881 seconds # 测试总共请求了多少次 Complete requests: 1000 # 失败的请求数 Failed requests: 979
(Connect: 0, Receive: 0, Length: 979, Exceptions: 0)
# 传输的总数据量 Total transferred: 256761473 bytes
# HTML文档的总数据量 HTML transferred: 255605512 bytes
# 平均每秒的请求数,也叫RPS,该值越大表示服务器吞吐量越大,性能表现越好 Requests per second: 12.21 [#/sec] (mean)
# 请求平均耗时,越小说明响应越快 Time per request: 818.815 [ms] (mean)
# 服务器平均处理时间,其实是服务器吞吐量的倒数 Time per request: 81.881 [ms] (mean, across all concurrent requests)
# 每秒获取的数据长度,单位单位:KB/s Transfer rate: 3062.28 [Kbytes/sec] received
# 连接时间统计信息 Connection Times (ms)
# 最小 平均 中值 最大
min mean[+/-sd] median max
# 连接时间 Connect: 17 154 118.0 135 1416 # 处理时间 Processing: 329 661 211.2 602 1824 # 等待时间 Waiting: 22 160 88.4 143 1089 # 总计时间 Total: 391 816 263.4 731 2521
# 请求耗时的统计信息。例如请求经过排序后,50百分位的请求花费了731毫秒;99百分位的请求花费了1942毫秒等 Percentage of the requests served within a certain time (ms)
50% 731
66% 804
75% 873
80% 943
90% 1154
95% 1332
98% 1726
99% 1942
100% 2521 (longest request)
|