首页 > 其他分享 >ab压测的选项、示例和主要关注的指标意义以及ab压测问题Connection reset by peer (104),Connection timed out (110)的解决

ab压测的选项、示例和主要关注的指标意义以及ab压测问题Connection reset by peer (104),Connection timed out (110)的解决

时间:2024-10-13 11:51:50浏览次数:3  
标签:ab 请求 压测 Connection Time net 1000

一、ab压测的选项、示例和主要关注的指标意义

1. ab压测的一些选项

-n requests     全部请求数
-c concurrency  并发数
-t timelimit    最传等待回应时间
-p postfile     POST数 据文件
-T content-type POST Content-type
-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    加入cookie, eg. 'Apache=1234. (repeatable)
-H attribute    加入http头, eg. 'Accept-Encoding: gzip'
                Inserted after all normal header lines. (repeatable)
-A attribute    http验证,分隔传递用户名及密码
-P attribute    Add Basic Proxy Authentication, the attributes
                are a colon separated username and password.
-X proxy:port   代理服务器
-V              查看ab版本
-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)to run in the test

2. 压测命令示例

#压测示例,直接GET压测URL
root@test:~# ab -n 1000 -c 10 http://127.0.0.1/api/test-db
#压测示例,POST压测URL并带post数据
ab -n 20000 -c 1000  -p post.txt  -T application/json http://127.0.0.1.
# cat post.txt 
{"id":1000}
#压测POST并带header中的token数据
ab -n 20000 -c 1000  -p post.txt  -T application/json -H 'x-token: SDY21='  http://127.0.0.1.
# cat post.txt 
{"id":1000}
#响应数据及意义
#并发数
Concurrency Level:      5000
#全部请求完成耗时
Time taken for tests:   24.192 second
Complete requests:      10000
Failed requests:        0
Write errors:           0
#总传输大小 
Total transferred:      5370172 bytes
Total body sent:        2630000
#HTML传输量
HTML transferred:       3880000 bytes
#每秒请求数
Requests per second:    413.37 [#/sec] (mean)
#每次并发请求时间(所有并发),mean 表示这是一个平均值
Time per request:       12095.762 [ms] (mean)
#每一请求时间(并发平均) 
Time per request:       2.419 [ms] (mean, across all concurrent requests)
#传输速率,平均每秒网络上的流量
Transfer rate:          216.78 [Kbytes/sec] received
						106.17 kb/s sent
                        322.95 kb/s total

    其它指标都比较好理解,关于最后的两个Time per request指标,手册上的解释:

The first value is calculated with the formula concurrency *timetaken * 1000 / done
while the second value is calculated with the formula timetaken * 1000 / done

    网上的解释也是各种各样,不好理解。比如解释为:第一个Time per request代表每个链接上单个请求的平均响应时间,第二个Time per request所有链接合计后单个请求的平均响应时间。

    有的甚至是错的。我觉得直接从数值上面就可以发现其意义,比如Time taken for tests为全部请求完成耗时。而第一个Time per request值为2.419即是全部请求完成耗时除以Complete requests得到的值。即服务器在压测时每个请求的用时(实际因为多核并发,这个值比实际上的每个请求用时要小)。而第二个Time per request看数值则是第一个Time per request与Concurrency Level的乘积,可以认为是每次并发(Concurrency Level个请求完成)所用的时间。Tpr1=Tpr2*n

二、ab压测问题Connection reset by peer (104),Connection timed out (110)的解决

    ab是常用的压测工具,安装apache后就自带ab压测工具。不安装apache也可以直接使用ab,只需要安装apache的工具包httpd-tools即可。使用ab –V命令即可查看ab是否安装成功。压测示例见下方,使用-p带上要post的数据存放的文件路径,-H带上常用的token数据即可开压。如下:

# yum -y install httpd-tools
# ab -V
This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
# cat post.txt 
{"user_id":1}
# 压测示例
ab -n 20000 -c 1000  -p post.txt  -T application/json  -H 'x-token: SDY21='  http://127.0.0.1.

#1压测响应时可能遇到错误1:
Benchmarking 10.28.20.13 (be patient)
Completed 2000 requests
apr_socket_recv: Connection reset by peer (104)
Total of 1085 requests completed
#2压测响应时可能遇到错误1:
Benchmarking 10.18.230.23 (be patient)
Completed 2000 requests
apr_socket_recv: Connection timed out (110)
Total of 1085 requests completed

    此两项报错均未到程序层面。apr_socket_recv是操作系统内核的一个参数,在高并发的情况下,内核会认为系统受到了SYN flood攻击,会发送cookies(possible SYN flooding on port 80. Sending cookies),以减慢影响请求的速度,所以在应用服务武器上设置下这个参数为0禁用系统保护就可以进行大并发测试。

# vim /etc/sysctl.conf 
net.ipv4.tcp_syncookies = 0
# sysctl -p
然后就可以超过1000个并发测试了。

    网上有说是资源描述符的问题,用ulimit -n 65536解决,也有说是在/etc/sysctl.conf加入参数:

#vim /etc/sysctl.conf
net.nf_conntrack_max = 655360
net.netfilter.nf_conntrack_tcp_timeout_established = 1200
sysctl -p /etc/sysctl.conf

    不过我今天在遇到这个问题的时候和上面这些选项都没有关系,看了配置也都正常。最后是程序里面的mysql连接问题导致无法处理请求从而连接超时。net.ipv4.tcp相关的几个重要的系统配置如下:

net.ipv4.tcp_syncookies = 0 
#此参数是为了防止洪水攻击,但对于大并发系统,要禁用此设置
net.ipv4.tcp_max_syn_backlog
#参数决定了SYN_RECV状态队列的数量,一般默认值为512或者1024,即超过这个数量,系统将不再接受新的TCP连接请求,一定程度上可以防止系统资源耗尽。可根据情况增加该值以接受更多的连接请求。
net.ipv4.tcp_tw_recycle
#参数决定是否加速TIME_WAIT的sockets的回收,默认为0。
net.ipv4.tcp_tw_reuse
#参数决定是否可将TIME_WAIT状态的sockets用于新的TCP连接,默认为0。
net.ipv4.tcp_max_tw_buckets
#参数决定TIME_WAIT状态的sockets总数量,可根据连接数和系统资源需要进行设置。

 

标签:ab,请求,压测,Connection,Time,net,1000
From: https://blog.csdn.net/weixin_47792780/article/details/142757613

相关文章