码迷,mamicode.com
首页 > 编程语言 > 详细

python locust 性能测试:locust 关联---提取返回数据并使用

时间:2018-08-30 01:58:19      阅读:399      评论:0      收藏:0      [点我收藏+]

标签:log   开始   self   会员   headers   nio   for   使用   use   

from locust import HttpLocust, TaskSet, task
import json
from common import readConfig


class UserBehavior(TaskSet): # 定义用户行为

def on_start(self): # 当模拟用户开始执行TaskSet类的时候,on_start方法会被调用
        pass

def get_headers(self):
   """会员登录"""
headers = {
"Content-Type": "application/json",
"channel": "SHOP"
}
body = {
"unionid": readConfig.unionid # 这样使用会报错,没有 common ...
}
res = self.client.post(‘/customers/login‘, headers=headers, data=json.dumps(body)).text
assert ‘成功‘ in res # 断言,判断接口返回是否成功
res = json.loads(res)
uid = res[‘data‘][‘uid‘]
ukey = res[‘data‘][‘ukey‘]
return [uid, ukey] # 会员登录返回的uid和ukey

@task(1) # @task() 装饰该方法为一个任务;1表示一个Locust实例被挑选执行的权重,数值越大,执行频率越高;
def members_can_withdraw_account_information(self): # 一个行为
"""使会员获取会员状态"""
res = self.get_headers()
headers = {
"channel": "SHOP",
"uid": str(res[0]),
"ukey": res[1]
}
response = self.client.put("/customers/customer_state",
headers=headers).text # client.get()用于指请求的路径,可加headers,params,body参数;
assert ‘成功‘ in response

@task(1)
def get_member_account_withdrawal_details(self):
"""获取会员账户提现明细"""
res = self.get_headers()
headers = {
"channel": "SHOP",
"uid": str(res[0]),
"ukey": res[1]
}
params = {
"page": "10",
"size": "10"
}
response = self.client.get("/customers/account/withdraw/log", headers=headers, params=params).text
assert ‘成功‘ in response


class WebsiteUser(HttpLocust): # 设置性能测试;
host = "http://dev.sign.lixiaofeng.com"
task_set = UserBehavior # 指向一个定义了的用户行为类;
min_wait = 3000 # 用户执行任务之间等待时间的下界,单位:毫秒;
max_wait = 6000 # 用户执行任务之间等待时间的上界,单位:毫秒;

python locust 性能测试:locust 关联---提取返回数据并使用

标签:log   开始   self   会员   headers   nio   for   使用   use   

原文地址:https://www.cnblogs.com/changqing8023/p/9557545.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!