(六)TaskSequence类

@task修饰的任务是随机执行的,继承TaskSequence类,可以实现任务按指定顺序执行

(六)TaskSequence类_第1张图片

@seq_task

指定执行顺序,如果未指定,默认顺序是1

@task

执行权重,如果未指定,默认执行1次

既没有@task也没有@seq_task修饰的方法,不会被执行

实例代码

# coding=utf-8
''' Created on 2019-11-08
    author: ali

    场景描述
    step1 用户登录(只登录一次)
    step2 用户查看任务列表
    step3 查看任务详情(执行2次)
    step4 退出登录(任务停止时)
'''
import os
from locust import HttpLocust, task, TaskSequence, seq_task

class userTaskSet(TaskSequence):
    # 定义请求头
    webheaders = {
        "Accept-Encoding": "gzip, deflate",
        "User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36",
        "Content-Type": "application/x-www-form-urlencoded;charset=UTF-8",
        "Accept": "application/json, text/javascript, */*; q=0.01",
        "Connection": "Keep-Alive",
    }

    # 用户登录
    def on_start(self):
        # 请求url
        test_url = "/zentao/user-login.json"
        # 请求数据
        test_data = {
            "account":"ali001",
            "password": "q1w2e3r4t5y6",
            "referer":"/zentao/",
            "passwordStrength":1
        }
        with self.client.post(test_url, test_data, headers=self.webheaders, catch_response=True) as response:
            try:
                json_res = response.json()
                # 断言
                if json_res["status"] == 'success':
                    response.success()
                else:
                    response.failure('User login failed!')
                    print(json_res)
            except Exception as e:
                response.failure(e)
                print(e)

    # 用户查看任务列表,用户登录后执行该任务
    @seq_task(1)
    def taskList(self):
        test_url = "/zentao/project-task-1.json"

        with self.client.get(test_url, headers=self.webheaders, catch_response=True) as response:
            try:
                json_res = response.json()
                if json_res["status"] == 'success':
                    response.success()
                else:
                    response.failure('User get task list failed!')
                    print(json_res)
            except Exception as e:
                response.failure(e)
                print(e)

    # 查看任务详情,用户执行查看任务列表后,执行该任务,执行2次
    @seq_task(2)
    @task(2)
    def viewTask(self):
        test_url = "/zentao/task-view-485.json"
        with self.client.get(test_url, headers=self.webheaders, catch_response=True) as response:
            try:
                json_res = response.json()
                if json_res["status"] == 'success':
                    # print(json_res)
                    response.success()
                else:
                    response.failure('User view task failed!')
                    print(json_res)
            except Exception as e:
                response.failure(e)
                print(e)

    # 用户退出登录
    def on_stop(self):
        test_url = "/zentao/user-logout.json"
        with self.client.get(test_url, headers=self.webheaders, catch_response=True) as response:
            try:
                json_res = response.json()
                if json_res["status"] == 'success':
                    response.success()
                else:
                    response.failure('User logout failed!')
                    print(json_res)
            except Exception as e:
                response.failure(e)
                print(e)

class websiteUser(HttpLocust):
    task_set = userTaskSet
    host = 'http://127.0.0.1:8088'
    min_wait = 5000  # 单位为毫秒
    max_wait = 8000  # 单位为毫秒

if __name__ == "__main__":
    os.system("locust -f zentaoTest.py --host=http://127.0.0.1:8088")

执行结果

(六)TaskSequence类_第2张图片

用户登录、退出各执行1次

用户查看任务列表未设置权重,默认执行1次

用户查看任务详情,执行2次

 

你可能感兴趣的:(Locust性能测试入门)