压测工具-ab

如何获取UA和TPS

比如一个系统系统用户数是500万 但是最高峰日活用户10万  但是10万里面,并不是每个用户都会对系统实时发起请求,有的用户可能再浏览商品,有的用户可能在填写表单,或者用户的用户挂起,做别的事情去了,或者是短暂访问后退出了系统。这一部分用户是不会对系统的负载产生影响,所以取10%作为并发数

安装

 yum -y install httpd-tools

安装后输入ab可以看到以下提示

使用

post请求

例如,一个测试并发用户数为 10、请求数量为 100 的的 post 请求输入如下

ab -n 100  -r -c 10 -p 'post.txt' -T 'application/x-www-form-urlencoded' 'http://test.api.com/test/register'

post.txt 为存放 post 参数的文档,存储格式如下:

usernanme=test&password=test&sex=1

参数含义

-n:总请求次数(最小默认为 1); 个人理解 是为了保持测试的稳定性 一般 要设置比并发数多一倍
-c:并发次数(最小默认为 1 且不能大于总请求次数,例如:10 个请求,10 个并发,实际就是 1 人请求 1 次);
-p:post 参数文档路径(-p 和 -T 参数要配合使用);
-T:header 头内容类型(此处切记是大写英文字母 T)
-r: 出现异常不结束

JSON

ab -n 1000 -c 100 -T "application/json" -H "Content-Type: application/json"  -p requestbody.txt http://IP:PORT/openapi/user/getUserByIdOrName

get请求

ab -c 10 -n 100 http://www.test.api.com/test/login?userName=test&password=test
//版本信息
This is ApacheBench, Version 2.3 <$Revision: 1826891 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking localhost (be patient).....done


Server Software:      
//请求域名端口  
Server Hostname:        localhost
Server Port:            8999
//请求路径
Document Path:          /testController/test2?a=1
Document Length:        0 bytes
//测试期间使用的并发客户端数
Concurrency Level:      10
//这是从创建第一个套接字连接到接收到最后一个响应的时间
Time taken for tests:   0.025 seconds
//成功请求数
Complete requests:      100
//被视为失败的请求数。如果该数字大于零,则将打印另一行,显示由于连接、读取、内容长度不正确或异常而失败的请求数。
Failed requests:        0
//从服务器接收的字节总数。这个数字实际上是通过线路发送的字节数。
Total transferred:      9200 bytes
//从服务器接收的文档字节总数。此数字不包括在HTTP头中接收的字节数
HTML transferred:       0 bytes
//吞吐率 这是每秒的请求数。此值是请求数除以所用总时间的结果 值越大越好
Requests per second:    3946.33 [#/sec] (mean)
//每10个并发处理完平均耗时
Time per request:       2.534 [ms] (mean)
//每次请求平均耗时
Time per request:       0.253 [ms] (mean, across all concurrent requests)
//按totalread/1024/timetaken公式计算的传输速率
Transfer rate:          354.55 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.1      0       1
Processing:     1    2   0.3      2       3
Waiting:        1    2   0.3      2       3
Total:          2    2   0.3      2       3
//请求耗时的 分布情况 如百分之80请求都不超过2ms
Percentage of the requests served within a certain time (ms)
  50%      2
  66%      2
  75%      2
  80%      2
  90%      3
  95%      3
  98%      3
  99%      3
 100%      3 (longest request)

官方文档:http://httpd.apache.org/docs/2.4/programs/ab.html

Too many open files

查看最大文件数

ulimit -n
re file size          (blocks, -c) 0
data seg size           (kbytes, -d) unlimited
file size               (blocks, -f) unlimited
max locked memory       (kbytes, -l) unlimited
max memory size         (kbytes, -m) unlimited
open files                      (-n) 256
pipe size            (512 bytes, -p) 1
stack size              (kbytes, -s) 8192
cpu time               (seconds, -t) unlimited
max user processes              (-u) 1418
virtual memory          (kbytes, -v) unlimited

默认 256

This is ApacheBench, Version 2.3 <$Revision: 1826891 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking localhost (be patient)
socket: Too many open files (24)

解决方式

sudo ulimit -n 35768(要改的数)

1.如果提示 -bash: ulimit: open files: cannot modify limit: Operation not permitted:表示权限不足 通过 sudo ulimit -n 35768 修改

3.如果调得过高的话,会出现报错: ulimit: open files: cannot modify limit: Invalid argument。

输入:sysctl -w kern.maxfiles=35786

输入:sysctl -w kern.maxfilesperproc=35786

输入:ulimit -n 35780

原因:linux是通过文件来对设备进行管理,ulimit -n是设置同时打开文件的最大数值,ab中每一个连接打开一个设备文件,所以设置这个值就可以解决了。

total of 870 requests completed

可能是出现当有请求出现自动退出    加上-r参数 出现异常不退出 :

ab -r -c 10000  -n 10000 localhost:8999/testController/test2 

Linux解决

当并发量达到1000多的时候报错如下:

This is ApacheBench, Version 2.3 <$Revision: 1826891 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking localhost (be patient)
apr_socket_recv: Connection reset by peer (54)
Total of 876 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个并发测试了。

另附其他系统内核参数说明:

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总数量,可根据连接数和系统资源需要进行设置。 
————————————————
版权声明:本文为CSDN博主「cocosgirl」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/cocos2dGirl/java/article/details/88842875

Mac解决 

https://segmentfault.com/a/1190000007956688 

https://blog.csdn.net/u010860518/article/details/53309024

cd  /usr/local/webserver/httpd-2.4.10/bin/  ./ab -r -n  记录一下自己的安装路径

原文地址:https://www.cnblogs.com/LQBlog/p/12575028.html