蝗虫忽略扩展子 类 并实例化基础 类

locust ignoring extended sub classes and instantiates base classes

我想编写基础 http 用户 class 和基础负载测试形状,然后在子 classes 中扩展它们,但蝗虫不支持扩展 classes 和实例化基础 classes 这些是基础 classes 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)} 

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

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

喜欢删除 Rps class 而只是做

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