浮生闲记
2025-04-23
点 赞
0
热 度
4
评 论
0

解决locust新版本性能测试报错

文章摘要

摘要

解决locust新版本性能测试报错

问题描述

代码如下:

from locust import TaskSet,HttpLocust

# 定义任务
def login(l):
    l.client.post("/login", data={"username": "admin", "password": "123456"})


def index(l):
    l.client.get("/index")


def profile(l):
    l.client.get("/profile")


def logout(l):
    l.client.post("/logout")


class UserBehavior(TaskSet):
    tasks = {index: 3, profile: 1}

    def on_start(self):
        login(self)

    def on_stop(self):
        logout(self)

class WebsiteUser(HttpLocust):
    task_set = UserBehavior
    min_wait = 2000
    max_wait = 3000
    host = "http://www.baidu.com"
    weight = 10

问题1

报错信息如下:

ImportError: The HttpLocust class has been renamed to HttpUser in version 1.0. For more info see: https://docs.locust.io/en/latest/changelog.html#changelog-1-0

解决方法

将从locust中导入的HttpLocust改为HttpUser,即from locust import HttpLocust改为from locust import HttpUser,将脚本中引用的所有HttpLocust都改为HttpUser.

问题2:

报错信息如下

No tasks defined on WebsiteUser. Use the @task decorator or set the 'tasks' attribute of the User (or mark it as abstract = True if you only intend to subclass it)

解决方法

task_set = UserBehavior改成tasks = [UserBehavior]即可,UserBehavior 指的是你继承TaskSet的那个子类

问题原因

locust进入1.x版本后,语法发生相应变化

问题解决

代码如下:

from locust import TaskSet, HttpUser


# 定义任务
def login(l):
    l.client.post("/login", data={"username": "admin", "password": "123456"})


def index(l):
    l.client.get("/index")


def profile(l):
    l.client.get("/profile")


def logout(l):
    l.client.post("/logout")


class UserBehavior(TaskSet):
    tasks = {index: 3, profile: 1}

    def on_start(self):
        login(self)

    def on_stop(self):
        logout(self)


class WebsiteUser(HttpUser):
    tasks = [UserBehavior]
    min_wait = 2000
    max_wait = 3000
    host = "http://www.baidu.com"
    weight = 10

用键盘敲击出的不只是字符,更是一段段生活的剪影、一个个心底的梦想。希望我的文字能像一束光,在您阅读的瞬间,照亮某个角落,带来一丝温暖与共鸣。

浮生闲记

intj 建筑师

站长

具有版权性

请您在转载、复制时注明本文 作者、链接及内容来源信息。 若涉及转载第三方内容,还需一同注明。

具有时效性

目录

欢迎来到浮生闲记的站点,为您导航全站动态

26 文章数
3 分类数
1 评论数
5标签数
最近评论
二十七度风

二十七度风


已Mark

访问统计