美文网首页
一文告诉你 ab 如何做性能测试,详解测试派

一文告诉你 ab 如何做性能测试,详解测试派

作者: LinminWang | 来源:发表于2020-09-14 14:04 被阅读0次

一文告诉你 ab 如何做性能测试

说到 ab,可能有同学听说过,不知道的同学,也可能马上就去百度了。ab 是 apache 服务器附带一个工具,下载了 apache 服务,那么在它的 bin 文件夹中,就有这个工具命令。

但是,

我的项目不是用 apache 做服务器,我想用这个工具,就一定需要下载 apache 服务吗?这个 ab 工具,可以独立安装。

安装 ab

在你的 Linux 服务器上,执行:

# centos系统sudo yum -y install httpd-tools# ubuntu系统sudo apt-get -y install apache2-utils

待命令安装完成后,在系统中执行ab -V如果一切正常,就能看到 ab 相关信息,说明你的电脑已经安装好了 ab 工具包了。

CentOS 中安装:

728 x 6891892 x 1790

Ubuntu 中安装:

728 x 4281885 x 1109

用法说明

在服务器上执行:

$ ab --helpab: wrong number of argumentsUsage: ab[options][http[s]://]hostname[:port]/pathOptions are:    -n requests    Number of requests to perform    -c concurrency  Number of multiple requests to make at atime-t timelimit    Seconds to max. to spend on benchmarking                    This implies -n50000-s timeout      Seconds to max.waitforeach response                    Default is30seconds    -b windowsize  Size of TCP send/receive buffer, in bytes    -B address      Address tobindto when making outgoing connections    -p postfile    File containing data to POST. Remember also toset-T    -u putfile      File containing data to PUT. Remember also toset-T    -T content-type Content-type header to useforPOST/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 andexit-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 than150requests    -l              Accept variable document length(use thisfordynamic pages)-g filename    Output collected data to gnuplot format file.    -e filename    Output CSV file with percentages served    -r              Don''texiton 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

Usage:用法ab 参数 被测地址

参数:

-c concurrency: 一次产生的请求数,可以理解为并发数

-n requests:执行的总请求数,默认 1 次

-t timelimit:测试进行的最长时长,可以理解为持续施压时长

-s timeout: 最大超时等待时长

-p postfile: POST 请求文件,但是记得 配置 -T 参数

-u putfile: PUT 请求, 但是记得配置 -T 参数

-i: HEAD 请求

-m method: 方法名称

-T content-type: 指定 POST\PUT 请求的头部信息 Content-Type,默认为'text/plain',可以根据自己需要更改为如:application/x-www-form-urlencoded、application/json、multipart/form-data

-C attribute: 对请求附加一个 Cookie:行。其典型形式是 name=value 的一个参数对,此参数可以重复

-H attribute: 对请求附加额外的头信息。此参数的典型形式是一个有效的头信息行,其中包含了以冒号分隔的字段和值的对(如,"Accept-Encoding:zip/zop;8bit")

-A attribute: 对服务器提供 BASIC 认证信任。用户名和密码由一个:隔开,并以 base64 编码形式发送。无论服务器是否需要(即,是否发送了 401 认证需求代码),此字符串都会被发送

-g filename: 把所有测试结果写入一个'gnuplot'或者 TSV(以 Tab 分隔的)文件。此文件可以方便地导入到 Gnuplot,IDL,Mathematica,Igor 甚至 Excel 中。其中的第一行为标题

-e filename: 将执行的结果写到 CSV 文件,并提供百分比

-r : 出现错误时,不退出

案例操作

案例:并发请求,指定时长

ab -c100-t120https://www.baidu.com/

用100个并发用户,向 http://www.baidu.com/ 这个网址,持续请求120秒

案例:指定请求次数

ab -c100-n1000https://www.baidu.com/

用100个用户并发,总共请求1000次

案例:GET 请求带参数,用户登录

ab -c100-t10-p pdata.txt -T"application/x-www-form-urlencoded"http://172.17.0.1:8380/futureloan/mvc/api/member/register# data.txt文件内容为mobilephone=13500090018&pwd=123456780

728 x 3531903 x 922

案例:POST 请求,带参数,参数类型为 JSON,用户登录

ab -c1-n1-p user.json -T"application/json"-v4http://172.17.0.1:8808/erp/loginIn# user.json是请求的参数文件{"name":"029ip","pwd":"1234567890"}

728 x 3531903 x 922

结果参数说明

# ab -c 15 -t 10 -p data.txt -T "application/x-www-form-urlencoded"  http://172.17.0.1:8380/futureloan/mvc/api/member/registerThis is ApacheBench, Version 2.3 <$Revision:1843412$>Copyright1996Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/Licensed to The Apache Software Foundation, http://www.apache.org/Benchmarking 172.17.0.1(be patient)Finished1220requests# 总共请求数量# 服务器信息Server Software:Server Hostname:        172.17.0.1#服务器主机名Server Port:            8380#服务器端口Document Path:          /futureloan/mvc/api/member/register#请求的URL中的根绝对路径Document Length:60bytes#HTTP响应的正文长度Concurrency Level:      15#并发数Time takenfortests:  10.009 seconds#整个测试耗时Complete requests:      1220# 总共完成的请求数量Failed requests:        1219# 失败数(Connect: 0, Receive: 0, Length: 1219, Exceptions: 0)Total transferred:233008bytes#测试过程中产生的网络传输总量Total body sent:277425HTML transferred:87828bytes#测试过程中产生的HTML传输量Requests per second:    121.89[#/sec] (mean) # 平均每秒请求数Time per request:      123.065[ms](mean)# 每个请求平均时间,所有并发用户一起Time per request:      8.204[ms](mean, across all concurrent requests)#单个用户请求时间Transfer rate:          22.73[Kbytes/sec]received#吞吐率 每秒接收多少kb27.07 kb/s sent# 吞吐率, 每秒发送多少kb49.80 kb/s total#吞吐率 每秒多少kbConnection Times(ms)min  mean[+/-sd]median  max#(最小值、平均值、方差、中位数、最大值)Connect:000.204#socket链路建立消耗,代表网络状况好Processing:1121229.3123282#写入缓冲区消耗+链路消耗+服务器消耗Waiting:1121229.2123282#写入缓冲区消耗+链路消耗+服务器消耗+读取数据消耗Total:1121229.3123282#单个事务总时间#网络上消耗的时间的分解,表示这些请求在单位时间内从服务器获取的数据长度Percentage of the requests served within a certaintime(ms)50%1266%2675%21580%22190%29395%50398%87999%1131100%3282(longest request)

注意事项

-c 设置最大并发数,一般不超过 1024,因为系统默认设置最大打开文件数为 1024,Linux 中,可以通过 ulimit -a 查看

-n 指定最大请求数,但是不能操作 50000

总结

总体来说,ab工具小巧灵活,学习上手快,可以提供基本的性能测试指标。但是,不能做复杂的脚本开发,没有图形化结果,不能监控。所以,ab工具做个临时的紧急的简单测试是可以的。

想了解更多有趣,有料的测试相关技能技巧,欢迎关注柠檬班微信公众号,或在腾讯课堂中搜索柠檬班机构,观看测试相关视频。

作者:Allen

链接:http://testingpai.com/article/1595507309308

来源:测试社区

协议:CC BY-SA 4.0 https://creativecommons.org/licenses/by-sa/4.0/

相关文章

网友评论

      本文标题:一文告诉你 ab 如何做性能测试,详解测试派

      本文链接:https://www.haomeiwen.com/subject/nokzektx.html