一,安装ab
[root@blog ~]# yum install httpd-tools
说明:刘宏缔的架构森林是一个专注架构的博客,地址:https://www.cnblogs.com/architectforest
对应的源码可以访问这里获取: https://github.com/liuhongdi/
说明:作者:刘宏缔 邮箱: 371125307@qq.com
二,查看版本和帮助
1,查看版本
[root@blog ~]# ab -V 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/
2,查看帮助:
[root@blog ~]# ab -h Usage: ab [options] [http[s]://]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) -I Disable TLS Server Name Indication (SNI) extension -Z ciphersuite Specify SSL/TLS cipher suite (See openssl ciphers) -f protocol Specify SSL/TLS protocol (SSL2, TLS1, TLS1.1, TLS1.2 or ALL) -E certfile Specify optional client certificate chain and private key
三,常用参数说明
-n: 总的请求个数
-c: 一次并发的请求数
-t: 持续的时间,默认没有限制
-k:它会增加请求头Connection: Keep-Alive,相当于开启了HTTP长连接,
这样做一方面可以降低测试服务器动态端口被耗尽的风险,
另一方面也有助于给目标服务器更大的压力,测试出更接近极限的结果
说明:ab使用的是HTTP/1.0,缺省开启的是短链接,用-k参数则可以打开长连接
四,ab运行的例子:
1,运行命令:一次100个并发请法庭,共10000个请求
[root@centos8 conf]# ab -c 100 -n 10000 http://127.0.0.1/a.html 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 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 Completed 10000 requests Finished 10000 requests Server Software: openresty/1.15.8.3 Server Hostname: 127.0.0.1 Server Port: 80 Document Path: /a.html Document Length: 34 bytes Concurrency Level: 100 Time taken for tests: 0.968 seconds Complete requests: 10000 Failed requests: 0 Total transferred: 2710000 bytes HTML transferred: 340000 bytes Requests per second: 10335.21 [#/sec] (mean) Time per request: 9.676 [ms] (mean) Time per request: 0.097 [ms] (mean, across all concurrent requests) Transfer rate: 2735.20 [Kbytes/sec] received Connection Times (ms) min mean[+/-sd] median max Connect: 0 2 2.1 2 15 Processing: 1 7 3.7 6 22 Waiting: 0 6 3.2 5 18 Total: 3 10 3.7 9 23 Percentage of the requests served within a certain time (ms) 50% 9 66% 11 75% 12 80% 13 90% 15 95% 17 98% 19 99% 20 100% 23 (longest request)
五,ab运行结果的输出字段说明:
1,值得注意的字段
请求汇总中的各字段:
Document Length: # 请求的页面大小
Concurrency Level: # 每次的并发请求数
Time taken for tests: # 测试总共耗时
Complete requests: # 完成的请求总数
Failed requests: # 失败的请求数量
Write errors: # 错误的请求数量
Total transferred: # 总共传输数据量
Requests per second 吞吐率(reqs/s):服务器每秒能够处理的请求数量
Time per request 平均请求处理时间
它的两个值是:第一行为每次并发请求的耗时,
第二行为每个请求的耗时
第一行值为第二行的值乘上并发请求数量。
可以尝试将并发数改为20,
这样就会看到第一行是第二行的20倍
Transfer rate 表示吞吐量(BPS):
即:每秒从服务器获取的数据的长度
连接时间汇总部分,则是分别展示了建立连接、请求、等待以及汇总等的各类时间,包括最小、最大、平均以及中值处理时间。
Connect: 建立连接
Processing: 处理请求
Waiting: 等待
Total: 全部时间
min 最小
mean[+/-sd] 平均
median 中值
max 最大
请求延迟汇总部分:给出了不同时间段内处理请求的百分比
例子:
75% 24
75% 的请求,都可以在 24ms 内完成。
2,最常用的字段:
Requests per second 吞吐率
3,请求失败Failed requests时注意查找原因:
例如:
Failed requests: 9991
(Connect: 0, Receive: 0, Length: 9991, Exceptions: 0)
Non-2xx responses: 9991
Connect:无法送出要求、目标主机连接失败、请求的过程中连接被中断
Receive:当客户端connect成功后,并且服务端成功accept,并且没有开始recv,
然后服务端close掉socket,就产生这个错误
平时多见于服务端主动close掉客户端连接,即客户端表现为Connection reset by peer
Length:即读到的报文长度不等于http头的content-length值
Exceptions:多见于网络发生错误
做ab压测时,注意先把waf防火墙关掉,我遇到的这个问题在waf关闭后恢复正常
六,查看centos的版本:
[root@blog ~]# cat /etc/redhat-release CentOS Linux release 8.0.1905 (Core)