前言:
转眼个把月过去了,好久没更博客了哈,最近实在太忙。前段时间休了陪产假,家里新来了个大胖小子,哈哈哈!
好了言归正传吧,这段时间公司项目新增了一个直播的模块,今天自己拿着公司的接口做了一下性能测试,居然真的发现了问题!
压测直播接口:
这次主要压测的有两个接口,一个进入直播详情的接口,再一个是进入直播的接口:
首先,把结构理清楚,文件拉出来:
代码结构:
代码:
直接上代码,应该都能看懂:
#coding:utf-8
import Hash
import time,requests
from locust import HttpLocust,TaskSet,task
import login
#定义用户行为
class User(TaskSet):
#下面是请求头header
header = {
'User-Agent': 'LanTingDoctor/1.3.1 (iPad; iOS 10.1.1; Scale/2.00)',
'Accept-Encoding': 'gzip, deflate',
'Accept-Language': 'zh-Hans-CN;q=1',
'Content-Type': 'application/json',
'requestApp': '3',
'requestclient': '2',
'versionForApp': '2.4.0',
'Authorization': 'Basic YXBpTGFudGluZ0BtZWRsYW5kZXIuY29tOkFwaVRobWxkTWxkQDIwMTM=',
'Connection': 'keep-alive'
}
#调用登录,获取token
s = requests.session()
L = login.LG(s)
#进入直播课的入参
t = {
"token": L.login(),
"nonce": Hash.get_digit(),
"timestamp":str(int(time.time())),
'live_code': 'L2018091268052'
}
#入参加密
t['sign'] = Hash.get_sign(t)
#进入直播课详情的入参
de = {
'token': L.login(),
'nonce': Hash.get_digit(),
'timestamp': str(int(time.time())),
'live_code': 'L2018091268052'
}
#入参加密
de['sign'] = Hash.get_sign(de)
#task()括号中代表执行压测时的比重
@task(1)
def checkin(self):
u'进入直播课堂'
r = self.client.post('/v1/live/checkIn',headers = self.header,json=self.t)
result = r.json()
#assert r.json()['code'] == 200
@task(1)
def detail(self):
u'进入直播课详情'
r2 = self.client.post('/v1/live/detail',headers = self.header,json=self.de)
result2 = r2.json()
#assert r2.json()['code'] == 200
class Websiteuser(HttpLocust):
task_set = User
#host = 'http://api-live.xxxxxx'
max_wait = 6000
min_wait = 1000
执行压测:
再然后进入命令行,进入压测脚本的目录:
直接回车,然后进入本地localhost设置虚拟用户200个,每秒启动40,开始测试。。。
响应时间逐渐变大,然后服务器的cpu就满了。。。。
再然后就出现了请求失败的情况,服务器报错500
未完待续。。
经过开发半天的调优,终于稳定了,如下图:
到此结束,此次的实战只是初试,还有很多不懂的,比如参数化,比如断言之类等等等等,资料也不好找!哎,继续努力吧!!
end。。。。
后续:
以下内容为2018年11月22编辑:
前段时间帮同事安装locust发现已经升级到了0.9版本,索性升级;
1.进入cmd命令
2.pip uninstall locustio 来卸载旧的locust
3.pip install locustio 来安装最新版本locustio
搞定!
由于我司最近直播模块进行了部分调整,座椅又对直播接口进行了简单的压测,此次测试只对部分代码做了些许修改,对断言的方式优化了一番。具体内容如下:
#coding:utf-8
from common import Hash
import time,requests
from locust import HttpLocust,TaskSet,task
from common import login
#定义用户行为
class User(TaskSet):
#下面是请求头header
header = {
'User-Agent': 'okhttp/3.8.1',
'Accept-Encoding': 'gzip, deflate',
'Accept-Language': 'zh-Hans-CN;q=1',
'Content-Type': 'application/json',
'requestApp': '3',
'requestclient': '2',
'versionForApp': '2.5.1',
'Authorization': 'Basic YXBpTGFudGluZ0BtZWRsYW5kZXIuY29tOkFwaVRobWxkTWxkQDIwMTM=',
'Connection': 'keep-alive'
}
#调用登录,获取token
s = requests.session()
L = login.LG(s)
#进入直播课的入参
t = {
"token": L.login(),
"nonce": Hash.get_digit(),
"timestamp":str(int(time.time())),
'live_code': 'L2018112298710'
}
#入参加密
t['sign'] = Hash.get_sign(t)
#进入直播课详情的入参
de = {
'token': L.login(),
'nonce': Hash.get_digit(),
'timestamp': str(int(time.time())),
'live_code': 'L2018112298710'
}
#入参加密
de['sign'] = Hash.get_sign(de)
#获取嘉宾主持人入参
member = {
'token': L.login(),
'nonce': Hash.get_digit(),
'timestamp': str(time.time()),
'live_code': 'L2018112298710'
}
#入参加密
member['sign'] = Hash.get_sign(member)
#获取图片
image = {
'token': L.login(),
'nonce': Hash.get_digit(),
'timestamp': str(time.time()),
'live_code': 'L2018112298710'
}
#加密
image = Hash.get_sign(image)
#task()括号中代表执行压测时的比重
@task(1)
def checkin(self):
u'进入直播课堂'
with self.client.post('/v1/live/checkIn',headers = self.header,json=self.t,catch_response=True) as response:
#请求参数中通过catch_response=True来捕获响应数据,然后对响应数据进行校验
#使用success()/failure()两个方法来标识请求结果的状态
if response.status_code == 200:
response.success()
else:
response.failure()
@task(1)
def detail(self):
u'进入直播课详情'
with self.client.post('/v1/live/detail',headers = self.header,json=self.de,catch_response=True) as response:
if response.status_code == 200:
response.success()
else:
response.failure()
@task(1)
def member_teach(self):
u'获取嘉宾主持人'
with self.client.post('/v1/member/teach',headers = self.header,json= self.member,catch_response=True) as response:
if response.status_code == 200:
response.success()
else:
response.failure()
@task(1)
def get_iamge(self):
with self.client.post('/v1/matter/imageList',headers = self.header,json = self.image,catch_response=True) as response:
if response.status_code == 200:
response.success()
else:
response.failure()
class Websiteuser(HttpLocust):
task_set = User
#host = 'http://api-live.sunnycare.cc'
max_wait = 6000
min_wait = 3000
if __name__=='__main__':
#导入os模块,os.system方法可以直接在pycharm中该文件中直接运行该py文件
import os
os.system('locust -f locustfile4.py --host=http://xxxxxx')