首页 > 解决方案 > 蝗虫忽略扩展子类并实例化基类

问题描述

我想编写基本 http 用户类和基本负载测试形状,然后在子类中扩展它们,但 locust 不理解扩展类和 instatiate bas 类,这些是基类 helpers.py:

from locust.contrib.fasthttp import FastHttpUser
import  string
from locust import LoadTestShape, constant_pacing
from dotenv import load_dotenv
import os
load_dotenv()
# init parameters
host_address = "127.0.0.1"
class BaseHttpUser(FastHttpUser):
    host = host_address
    wait_time = constant_pacing(5)
    chars = string.ascii_uppercase + string.ascii_lowercase + string.digits
    start_time = 0

class BaseRps(LoadTestShape):
    time_limit = 600
    user_spawn = {1: (1500, 10)}

    def tick(self):
        step = len(self.user_spawn.keys())
        run_time = self.get_run_time()
        print(step, )
        for idx in range(1, step+1):
            print(run_time , idx , self.time_limit)
            if run_time < idx * self.time_limit / step:
                print("here", self.user_spawn.get(idx))
                return self.user_spawn.get(idx)
        return None

这是我运行 minio.py 的文件

from locust import task
from helpers import BaseHttpUser, BaseRps
import os

host_address = "127.0.0.1"
test_name = "minio"
log_file_path = 'log.log'
base_url = os.getenv("MINIO_URL")

class HttpUser(BaseHttpUser):
    host = host_address
    base_url = base_url

    @task
    def download(self):
        self.client.get(f'{self.base_url}/magnix-server-media/ads-images/ff.png', name='download')


    
class Rps(BaseRps):
    user_spawn = {1: (10000, 100)} 

标签: pythonooplocust

解决方案


基本用户类需要一个abstract = True不被实例化的属性。https://docs.locust.io/en/stable/api.html#locust.User.abstract

我不认为你可以对负载形状类做同样的事情,但你可以使用类属性(你可以在导入之后在你的 locustfile 中操作)

就像删除 Rps 类,而只是做

BaseRps.user_spawn = {1: (10000, 100)} 

推荐阅读