您的位置:首页 > 其它

超实用压力测试工具-ab工具

2016-10-28 12:53 393 查看
在学习ab工具之前,我们需了解几个关于压力测试的概念

吞吐率(Requestspersecond)
概念:服务器并发处理能力的量化描述,单位是reqs/s,指的是某个并发用户数下单位时间内处理的请求数。某个并发用户数下单位时间内能处理的最大请求数,称之为最大吞吐率。
计算公式:总请求数/处理完成这些请求数所花费的时间,即
Requestpersecond=Completerequests/Timetakenfortests

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

并发用户数(Thenumberofconcurrentusers,ConcurrencyLevel)
概念:要注意区分这个概念和并发连接数之间的区别,一个用户可能同时会产生多个会话,也即连接数。

用户平均请求等待时间(Timeperrequest)
计算公式:处理完成所有请求数所花费的时间/(总请求数/并发用户数),即
Timeperrequest=Timetakenfortests/(Completerequests/ConcurrencyLevel)

服务器平均请求等待时间(Timeperrequest:acrossallconcurrentrequests)
计算公式:处理完成所有请求数所花费的时间/总请求数,即
Timetakenfor/testsCompleterequests
可以看到,它是吞吐率的倒数。
同时,它也=用户平均请求等待时间/并发用户数,即
Timeperrequest/ConcurrencyLevel

ab工具简介

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

安装:

ycz@ubuntu:~$ab
程序“ab”尚未安装。您可以使用以下命令安装:
sudoapt-getinstallapache2-utils
ycz@ubuntu:~$sudoapt-getinstallapache2-utils


参数列表:

Usage:ab[options][http[s]://]hostname[:port]/path
Optionsare:
-nrequestsNumberofrequeststoperform//请求链接数
-cconcurrencyNumberofmultiplerequeststomakeatatime//表示并发数
-ttimelimitSecondstomax.tospendonbenchmarking
Thisimplies-n50000
-stimeoutSecondstomax.waitforeachresponse
Defaultis30seconds
-bwindowsizeSizeofTCPsend/receivebuffer,inbytes
-BaddressAddresstobindtowhenmakingoutgoingconnections
-ppostfileFilecontainingdatatoPOST.Rememberalsotoset-T
-uputfileFilecontainingdatatoPUT.Rememberalsotoset-T
-Tcontent-typeContent-typeheadertouseforPOST/PUTdata,eg.
'application/x-www-form-urlencoded'
Defaultis'text/plain'
-vverbosityHowmuchtroubleshootinginfotoprint
-wPrintoutresultsinHTMLtables
-iUseHEADinsteadofGET
-xattributesStringtoinsertastableattributes
-yattributesStringtoinsertastrattributes
-zattributesStringtoinsertastdorthattributes
-CattributeAddcookie,eg.'Apache=1234'.(repeatable)
-HattributeAddArbitraryheaderline,eg.'Accept-Encoding:gzip'
Insertedafterallnormalheaderlines.(repeatable)
-AattributeAddBasicWWWAuthentication,theattributes
areacolonseparatedusernameandpassword.
-PattributeAddBasicProxyAuthentication,theattributes
areacolonseparatedusernameandpassword.
-Xproxy:portProxyserverandportnumbertouse
-VPrintversionnumberandexit
-kUseHTTPKeepAlivefeature
-dDonotshowpercentilesservedtable.
-SDonotshowconfidenceestimatorsandwarnings.
-qDonotshowprogresswhendoingmorethan150requests
-lAcceptvariabledocumentlength(usethisfordynamicpages)
-gfilenameOutputcollecteddatatognuplotformatfile.
-efilenameOutputCSVfilewithpercentagesserved
-rDon'texitonsocketreceiveerrors.
-hDisplayusageinformation(thismessage)
-ZciphersuiteSpecifySSL/TLSciphersuite(Seeopensslciphers)
-fprotocolSpecifySSL/TLSprotocol
(SSL3,TLS1,TLS1.1,TLS1.2orALL)


开始测试

输入命令

ycz@ubuntu:~$ab-n100-c10'target='_blank'>http://sewise.21321.com/redistest/RedisSaveToMysqlJson[/code]
其中-n表示请求数,-c表示并发数

请求测试代码:$redisInfo是一个Json数组



这段展示的是web服务器的信息,可以看到服务器采用的是nginx,域名是wan.bigertech.com,端口是80

   


这段是关于请求的文档的相关信息,所在位置“/”,文档的大小为338436bytes(此为http响应的正文长度)  

  


这段展示了压力测试的几个重要指标

  



ConcurrencyLevel:100//并发请求数

Timetakenfortests:50.872seconds//整个测试持续的时间

Completerequests:1000//完成的请求数

Failedrequests:0//失败的请求数

Totaltransferred:13701482bytes//整个场景中的网络传输量

HTMLtransferred:13197000bytes//整个场景中的HTML内容传输量

Requestspersecond:19.66[#/sec](mean)//吞吐率,大家最关心的指标之一,相当于LR中的每秒事务数,后面括号中的mean表示这是一个平均值

Timeperrequest:5087.180[ms](mean)//用户平均请求等待时间,大家最关心的指标之二,相当于LR中的平均事务响应时间,后面括号中的mean表示这是一个平均值

Timeperrequest:50.872[ms](mean,acrossallconcurrentrequests)//服务器平均请求处理时间,大家最关心的指标之三

Transferrate:263.02[Kbytes/sec]received//平均每秒网络上的流量,可以帮助排除是否存在网络流量过大导致响应时间延长的问题



这段表示网络上消耗的时间的分解

  


这段是每个请求处理时间的分布情况,50%的处理时间在273ms内,66%的处理时间在289ms内...,重要的是看90%的处理时间。

  


关于登录的问题

有时候进行压力测试需要用户登录,怎么办?
请参考以下步骤:

先用账户和密码登录后,用开发者工具找到标识这个会话的Cookie值(SessionID)记下来

如果只用到一个Cookie,那么只需键入命令:
ab-n100-Ckey=value'target='_blank'>http://test.com/[/code]
如果需要多个Cookie,就直接设Header:
ab-n100-H“Cookie:Key1=Value1;Key2=Value2”'target='_blank'>http://test.com/[/code]

总结

总的来说ab工具ab小巧简单,上手学习较快,可以提供需要的基本性能指标,但是没有图形化结果,不能监控。因此ab工具可以用作临时紧急任务和简单测试。
同类型的压力测试工具还有:webbench、siege、http_load等

参考网址:http://www.jianshu.com/p/43d04d8baaf7
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: