locust-demo/base-login.py

58 lines
1.9 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# -*- coding: utf-8 -*-
from locust import HttpUser,task,TaskSet
import jpath
import json
from ensure import ensure
# 定义ScriptTasks类继承TaskSet类
class ScriptTasks(TaskSet):
#初始化每个locust用户开始做的第一件事
def on_start(self):
#放置 用户名和密码
res = self.client.post('/api/user/login', {
"username":"13700998877",
"password":'c123456'
})
#断言
ensure(res.status_code).equals(200)
#值
code = jpath.get('code', json.loads(res.text))
ensure(code).equals(0)
msg = jpath.get('msg', json.loads(res.text))
ensure(msg).equals("登录成功")
#@task()装饰的方法为一个事务方法的参数用于指定该行为的执行权重参数越大每次被虚拟用户执行的概率越高默认为1
@task(1)
def about(self):
#self.client 属性使用python的request库的方法调用和使用方法和request一样
task_rel = self.client.get('/api/user/user_info')
#断言
ensure(task_rel.status_code).equals(200)
#值
code = jpath.get('code', json.loads(task_rel.text))
ensure(code).equals(0)
msg = jpath.get('msg', json.loads(task_rel.text))
ensure(msg).equals("操作成功")
# @task(2)
# def demo(self):
# payload = {}
# headers = {}
# self.client.post('/demo', data = payload,headers = headers)
#TaskSet类该类定义用户任务信息(模拟用户信息)
class WebsitUser(HttpUser):
#指向一个定义的用户行为
task_set = task(ScriptTasks)
#被测系统的host
host = 'http://sky.nnzhp.cn'
#每个用户执行两个任务间隔时间最小值单位是毫秒默认是1000ms
min_wait = 100
# 每个用户执行两个任务间隔时间最大值,单位是(毫秒)
max_wait = 5000