WeTest 导读
在服务上线之前,性能测试必不可少。本文主要介绍性能测试的流程,需要关注的指标,性能测试工具apache bench的使用,以及常见的坑。
-
什么是性能测试
性能测试是通过自动化的测试工具模拟多种正常、峰值以及异常负载条件来对系统的各项性能指标进行测试。负载测试和压力测试都属于性能测试,两者可以结合进行。通过负载测试,确定在各种工作负载下系统的性能,目标是测试当负载逐渐增加时,系统各项性能指标的变化情况。压力测试是通过确定一个系统的瓶颈或者不能接受的性能点,来获得系统能提供的最大服务级别的测试。
-
性能测试的目标是什么
性能测试最终的目的,是找到系统的瓶颈,一般来说,是找到服务单机最大TPS(每秒完成的事务数)。
需要注意的是,服务的TPS需要结合请求平均耗时来综合考虑。例如:服务TPS压到1000,平均请求耗时500ms,但是假如我们定的服务请求耗时不能超过200ms,那么这个1000的TPS是无效的。
很多场景下,服务都会设置超时时间,若平均耗时超过此超时时间,则可认为服务处于不可用状态。
-
什么时候需要性能测试
1.功能测试完成之后,上线之前。
正常情况下,上线之前,都应该进行性能测试,尤其是请求量较大的接口,重点业务的核心接口,以及直接影响用户操作流程的接口。
2.各种大促,运营活动开始之前。
大促,运营活动,都会导致流量激增,因此上线之前做好压力测试,评估系统性能是否满足预估流量,提前做好准备。
举个反面例子:聚美优品,年年大促年年挂。
再来个正面的例子:每年双十一之前,阿里都会有全链路压测,各个业务自己也会有独立的压测,阿里在这块做得还是非常不错的。
-
怎么做性能测试
常见的http性能测试工具
1. httpload

2. wrk

3. apache bench



最终我们选择apache bench
看上去wrk才是最完美的,但是我们却选择了ab。我们验证过各种工具请求数据是否准确,压测的时候,通过后台日志记录,最终得出结论,ab的请求数误差在千分之二左右,而其他两个工具在千分之五左右。
不过不得不说,wrk的确是一款非常优秀的压测工具,采用异步IO模型,能压到非常高的TPS。曾经用空逻辑接口压到过7w的TPS,而相同接口,ab只能压到2w多。
-
apache bench的使用
前面已经给了一个简单的例子了,下面详细介绍下ab的使用。
如何安装?如果docker容器已经安装的apache,那么恭喜,ab是apache自带的一个组件,不用重新安装了。当然,也可以自己单独安装apache bench。

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 //测试所进行的最大秒数,默认没有时间限制。
-r Don't exit on socket receive errors. // 抛出异常继续执行测试任务
-p postfile File containing data to POST //包含了需要POST的数据的文件,文件格式如“p1=1&p2=2”.使用方法是 -p 111.txt
-T content-type Content-type header for POSTing
//POST数据所使用的Content-type头信息,如 -T “application/x-www-form-urlencoded” 。 (配合-p)
-v verbosity How much troubleshooting info to print
//设置显示信息的详细程度 – 4或更大值会显示头信息, 3或更大值可以显示响应代码(404, 200等), 2或更大值可以显示警告和其他信息。 -V 显示版本号并退出。
-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″ 。
-w Print out results in HTML tables //以HTML表的格式输出结果。默认时,它是白色背景的两列宽度的一张表。
-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
-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.
-g filename Output collected data to gnuplot format file.
-e filename Output CSV file with percentages served
-h Display usage information (this message)
-
性能测试报告
<
