ab压力测试命令详解


1.ab的简介
ab是apachebench命令的缩写。ab是apache自带的压力测试工具。ab非常实用,它不仅可以对apache服务器进行网站访问压力测试,也可以对或其它类型的服务器进行压力测试。比如nginx、tomcat、IIS等。

2.原理
ab的原理:ab命令会创建多个并发访问线程,模拟多个访问者同时对某一URL地址进行访问。它的测试目标是基于URL的,因此,它既可以用来测试apache的负载压力,也可以测试nginx、lighthttp、tomcat、IIS等其它Web服务器的压力。ab命令对发出负载的计算机要求很低,它既不会占用很高CPU,也不会占用很多内存。但却会给目标服务器造成巨大的负载,其原理类似CC攻击。自己测试使用也需要注意,否则一次上太多的负载。可能造成目标服务器资源耗完,严重时甚至导致死机。

3.安装
yum -y install httpd-tools

4.检验
ab -V

[root@gjtest ~]# ab -help
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
    -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.
    -h              Display usage information (this message)
    -Z ciphersuite  Specify SSL/TLS cipher suite (See openssl ciphers)
    -f protocol     Specify SSL/TLS protocol
                    (SSL3, TLS1, TLS1.1, TLS1.2 or ALL)

详情说明:

-n在测试会话中所执行的请求个数。默认时,仅执行一个请求。请求的总数量
-c一次产生的请求个数。默认是一次一个。请求的用户量
-t测试所进行的最大秒数。其内部隐含值是-n 50000,它可以使对服务器的测试限制在一个固定的总时间以内。默认时,没有时间限制。
-V显示版本号并退出。
-p 发送post请求参数存放的文件 文件格式为xx=2&x'x=1,配合-T使用(使用此选项的时候一定要加上-T参数)
-T 文本类型 默认为’text-plain‘ post请求时一般为'application/x-www-form-urlencoded' 或json 'application/json'

6.ab性能指标
在进行性能测试过程中有几个指标比较重要:

1、吞吐率(Requests per second)

服务器并发处理能力的量化描述,单位是reqs/s,指的是在某个并发用户数下单位时间内处理的请求数。某个并发用户数下单位时间内能处理的最大请求数,称之为最大吞吐率。

记住:吞吐率是基于并发用户数的。这句话代表了两个含义:

a、吞吐率和并发用户数相关
b、不同的并发用户数下,吞吐率一般是不同的

计算公式:总请求数/处理完成这些请求数所花费的时间,即
Request per second=Complete requests/Time taken for tests
必须要说明的是,这个数值表示当前机器的整体性能,值越大越好。

2、并发连接数(The number of concurrent connections)

并发连接数指的是某个时刻服务器所接受的请求数目,简单的讲,就是一个会话。

3、并发用户数(Concurrency Level)

要注意区分这个概念和并发连接数之间的区别,一个用户可能同时会产生多个会话,也即连接数。在HTTP/1.1下,IE7支持两个并发连接,IE8支持6个并发连接,FireFox3支持4个并发连接,所以相应的,我们的并发用户数就得除以这个基数。

4、用户平均请求等待时间(Time per request)

计算公式:处理完成所有请求数所花费的时间/(总请求数/并发用户数),即:
Time per request=Time taken for tests/(Complete requests/Concurrency Level)

5、服务器平均请求等待时间(Time per request:across all concurrent requests)

计算公式:处理完成所有请求数所花费的时间/总请求数,即:
Time taken for/testsComplete requests

可以看到,它是吞吐率的倒数。同时,它也等于用户平均请求等待时间/并发用户数,即
Time per request/Concurrency Level

7、ab实际使用
ab的命令参数比较多,我们经常使用的是-c和-n参数。

ab -c 10 -n 100 https://www.baidu.com/

-c10表示并发用户数为10
-n100表示请求总数为100

https://www.baidu.com/表示请求的目标URL

这表示模拟10个用户总共请求100次

如果请求带参数。并要指定post或者get请求

1.模拟get请求
直接在url后面带参数即可
ab -c 10 -n 10 https://www.baidu.com/?id=2

2.模拟post请求

在当前目录下创建一个文件post.txt

编辑文件post.txt写入
id=1&name=test

相当于post传递cid,status参数
ab -n 10 -c 10 -p 'post.txt' -T 'application/x-www-form-urlencoded' 'https://www.baidu.com/'

测试POST接口,需要JSON格式请求体
ab -n 5000 -c 300 -p post.txt -T 'application/json' url
其中,-p 表示需要携带的请求体,一般是.txt格式文件,文件内容如下:
{"toBank":"123456"}; -T 表示请求体格式,一般为’application/json’

如果需要带head头参数 在命令中使用-H如果有多个就写多个
-H "client:android" -H "time:1534816208" -H "version:1.0.0"

8.常见错误
1.当并发量达到1000多的时候报错如下

Completed 3000 requests
apr_socket_recv: Connection reset by peer (104)
Total of 3393 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个并发测试了。

CTRL+D快速收藏,欢迎常来喔

本文来自投稿,不代表本站立场,如若转载,请注明出处: https://www.xixd.cn/post-364.html
原文地址: 《ab压力测试命令详解》 发布于2024年3月6日

温馨提示: 没找到想要的资源?资源链接失效?下载后资源打不开?点我进群帮你解决!

本文标签:

相关推荐
发表评论
您需要登录后才可以回复 【 登录 】
0 评论
还没有评论,快来抢沙发吧!