http性能测试点滴

做者:樊春霖 腾讯移动互联网高级工程师
商业转载请联系腾讯WeTest得到受权,非商业转载请注明出处。
原文连接:https://wetest.qq.com/lab/view/410.htmlhtml

WeTest 导读

在服务上线以前,性能测试必不可少。本文主要介绍性能测试的流程,须要关注的指标,性能测试工具apache bench的使用,以及常见的坑。docker

_apache

什么是性能测试

性能测试是经过自动化的测试工具模拟多种正常、峰值以及异常负载条件来对系统的各项性能指标进行测试。负载测试和压力测试都属于性能测试,二者能够结合进行。经过负载测试,肯定在各类工做负载下系统的性能,目标是测试当负载逐渐增长时,系统各项性能指标的变化状况。压力测试是经过肯定一个系统的瓶颈或者不能接受的性能点,来得到系统能提供的最大服务级别的测试。api

性能测试的目标是什么

性能测试最终的目的,是找到系统的瓶颈,通常来讲,是找到服务单机最大TPS(每秒完成的事务数)。服务器

须要注意的是,服务的TPS须要结合请求平均耗时来综合考虑。例如:服务TPS压到1000,平均请求耗时500ms,可是假如咱们定的服务请求耗时不能超过200ms,那么这个1000的TPS是无效的。cookie

不少场景下,服务都会设置超时时间,若平均耗时超过此超时时间,则可认为服务处于不可用状态。网络

何时须要性能测试

1.功能测试完成以后,上线以前。session

正常状况下,上线以前,都应该进行性能测试,尤为是请求量较大的接口,重点业务的核心接口,以及直接影响用户操做流程的接口。并发

2.各类大促,运营活动开始以前。app

大促,运营活动,都会致使流量激增,所以上线以前作好压力测试,评估系统性能是否知足预估流量,提早作好准备。

举个反面例子:聚美优品,年年大促年年挂。

再来个正面的例子:每一年双十一以前,阿里都会有全链路压测,各个业务本身也会有独立的压测,阿里在这块作得仍是很是不错的。

怎么作性能测试

常见的http性能测试工具

  1. httpload

图片描述

  1. wrk

图片描述

  1. 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)

性能测试报告

图片描述

测试报告应该包含如下内容。固然,根据场景不一样,能够适当增减指标,例若有的业务要求关注cpu,内存,IO等指标,此时就应该加上相关指标。
图片描述

常见的坑

1.AB发送的是http1.0请求。

2.-t能够指定时间,-n指定发送请求总数,同时使用时压测会在-t秒或者发送了-n个请求以后中止。可是-t必定要在-n以前(ab的bug,-n在-t以前最多只会跑5s)。

3.为了使测试结果更可靠,单次压测时间应在2分钟以上。

理论上,压测时间越长,结果偏差越小。同时,能够在瓶颈附近进行长时间压测,例如一个小时或者一天,能够用来测试系统稳定性。许多系统的bug都是在持续压力下才会暴露出来。

4.当心压测客户端成为瓶颈。

例如上传,下载接口的压测,此时压测客户端的网络上行,下行速度都会有瓶颈,千万当心服务器还没到达瓶颈时,客户端先到了瓶颈。此时,能够利用多客户端同时压测。

5.ab能够将参数写入文件中,用此种方式能够测试上传文件的接口。

须要配合-p -t 使用。

$ ab -n 10000 -c 8 -p post_image_1k.txt -T "multipart/form-data; boundary=1234567890" http://xxxxxxx

文件内容以下:

图片描述

图片描述
6.ab不支持动态构建请求参数,wrk可配合lua脚本支持动态构建请求参数,仍是比较牛的。

package.path = '/root/wrk/?.lua;'

local md5 = require "md5"
local body   = [[BI_login|userid{145030}|openid{4-22761563}|source{}|affiliate{}|creative{}|family{}|genus{0}|ip{180.111.151.116}|from_uid{0}|login_date{2016-11-04}|login_time{10:40:13}|extra{}|srcid{1}|snid{1002}|clientid{1253}|gameid{2100012}
BI_logout|userid{184103}|openid{4-22784181}|family{}|genus{0}|ip{218.2.96.82}|logout_date{2016-11-04}|logout_time{10:40:42}|extra{}|srcid{1}|snid{1002}|clientid{1253}|gameid{2100012}
BI_role_logout|roleid{184103}|userid{184103}|openid{4-22784181}|ip{218.2.96.82}|level{100}|money{468}|power{1}|exp{252}|lijin{0}|online_time{0}|mapid{0}|posx{0}|posy{0}|rolelogout_date{2016-11-04}|rolelogout_time{10:40:42}|extra{0}|srcid{0}|snid{1002}|clientid{1253}|gameid{2100012}
BI_logout|userid{71084}|openid{4-20974629}|family{}|genus{0}|ip{117.136.8.76}|logout_date{2016-11-04}|logout_time{10:40:43}|extra{}|srcid{1}|snid{1002}|clientid{1253}|gameid{2100012}]] 

--local body = "hello"
wrk.headers["Content-Type"] = "text/xml"

local i=0
request = function()
   i = i+1
   local path = "/v1/pub?gameid=510038&timestamp=%s&key=510038&type=basic&sign=%s"
   local time = os.time()*1000
   local v = "510038" .. time .. "basic98889999"
   local sign = md5.sumhexa(v)
   path = string.format(path, time, sign)
   --print(path)
   return wrk.format("POST", path, nil, body)
end

图片描述

_

腾讯WeTest推出的“压测大师”,一分钟完成用例配置,无需维护测试环境,支持http协议、API接口、网站等主流压测场景。

点击:https://wetest.qq.com/gaps 便可体验。

若是使用当中有任何疑问,欢迎联系腾讯WeTest企业QQ:2852350015。

相关文章
相关标签/搜索