ab做压力测试

ab是apache 自带的一个压力测试的小工具,可用于接口简单的压力测试。

以下是AB的简要介绍

格式:ab [options] [http://]hostname[:port]/path
参数说明:
-n requests Number of requests to perform
//在测试会话中所执行的请求个数(本次测试总共要访问页面的次数)。默认时,仅执行一个请求。
-c concurrency Number of multiple requests to make
//一次产生的请求个数(并发数)。默认是一次一个。
-t timelimit Seconds to max. wait for responses
//测试所进行的最大秒数。其内部隐含值是-n 50000。它可以使对服务器的测试限制在一个固定的总时间以内。默认时,没有时间限制。
-p postfile File containing data to POST
//包含了需要POST的数据的文件,文本文件里边放个json个是的数据,post.txt
-T content-type Content-type header for POSTing
//POST数据所使用的Content-type头信息,如 -T “application/x-www-form-urlencoded” ,如果是json的话, -T “application/json” (配合-p)
-v verbosity How much troubleshooting info to print
//设置显示信息的详细程度 – 4或更大值会显示头信息, 3或更大值可以显示响应代码(404, 200等), 2或更大值可以显示警告和其他信息。 -V 显示版本号并退出。
//如果为4,可以输出响应信息和响应头
-w Print out results in HTML tables
//以HTML表的格式输出结果。默认时,它是白色背景的两列宽度的一张表。
-i Use HEAD instead of GET
// 执行HEAD请求,而不是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. -C “c1=1234,c2=2,c3=3″ (repeatable)
//-C cookie-name=value 对请求附加一个Cookie:行。 其典型形式是name=value的一个参数对。此参数可以重复,用逗号分割。
提示:可以借助session实现原理传递 JSESSIONID参数, 实现保持会话的功能,如
-C ” c1=1234,c2=2,c3=3, JSESSIONID=FF056CD16DA9D71CB131C1D56F0319F8″ 。
-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.
//-P proxy-auth-username:password 对一个中转代理提供BASIC认证信任。用户名和密码由一个:隔开,并以base64编码形式发送。无论服务器是否需要(即, 是否发送了401认证需求代码),此字符串都会被发送。
-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.
-g filename Output collected data to gnuplot format file.
-e filename Output CSV file with percentages served
-h Display usage information (this message)
//-attributes 设置属性的字符串. 缺陷程序中有各种静态声明的固定长度的缓冲区。另外,对命令行参数、服务器的响应头和其他外部输入的解析也很简单,这可能会有不良后果。它没有完整地实现 HTTP/1.x; 仅接受某些’预想’的响应格式。 strstr(3)的频繁使用可能会带来性能问题,即你可能是在测试ab而不是服务器的性能。

举一个工作中的例子:

ab.exe -c 10 -n 100 -t 60 -C "cookies" -p post.txt -T 'application/json' -v 4 http://xxxx.xxxx.com/lmauto/api/xxxx/order/batchSave.htm

如上所述:

-c是有10个并发用户,

-n 每个发送100个请求,

-t 总共处理60s,

-C 是有些接口需要验证用户的登录身份,所以带上cookie,双引号里边的cookies,就是你需要的cookies,

-p 是post请求需要的参数文件,请保证参数文件中数据的 正确性,

-T 是和-p一起使用,本文post的参数是json格式,所有采用了 'application/json',

-v 是打印服务器的响应和响应头,有不同的级别,这次压力测试刚开始没能看见请求响应的结果,服务器日志也没报异常,以为测试结果是OK的;第二天用了这个参数才知道,原来的测试结果误差很大,根本就没有能请求成功。(注:只要服务器有响应(状态码200),ab就认为是本次请求成功;不会在意返回的是成功和失败,所以一定要确认,请求的返回是不是你所期望的)

后边就是接口的url。

以下是1个并发,执行60s的测试结果:

 

注:还有一个弊端就是如果设计数据库的insert操作,且是唯一的,ab的并发用的是hi同一份数据文件,是无法进行数据的分配的,所以会有报重复,继续关注,看看有没有解决方案。

 附一个关于压力测试工具的链接:http://www.oschina.net/question/12_6110,非常不错。

posted @ 2014-08-11 09:41  liuweiqiang2v  阅读(1630)  评论(2编辑  收藏  举报