性能测试-locust 基础模板

发布时间 2023-10-24 09:03:10作者: 徐俊112
from locust import HttpUser
from locust import TaskSet
from locust import task

class Demo(TaskSet):
    """继承   定义任务类"""
    def on_start(self):
        print("开始执行")

    @task
    def bai_du(self):

        url = 'https://new.czwlyk.com/login'
        data = {
            "acco": "xujh",
            "steg": "111111",
            "code": "string"
        }
        headers = {"Content-Type": "application/json"}
        with self.client.request(method='POST', url=url, json=data, headers=headers, name='login登录') as response:
            if response.status_code == 200:
                if response.json().get('code') == 200:
                    print('登录成功')
                else:
                    response.failure("登录失败")
            else:
                response.failure("登录失败")


class WebSitUser(HttpUser):

    """继承   定义用户类,访问用户"""
    tasks = [Demo]      # 指定任务类
    host = "http://127.0.0.1:8080/"  # 指定主机地址
    min_wait = 2000
    max_wait = 3000
    # 指定思考时间,3s到2s之内随机选取一个时间充当思考时间

在终端运行:
cmd终端直接执行:locust -f demo.py