Search code examples
pythonooplocust

locust ignoring extended sub classes and instantiates base classes


i want to write base http user class and base load test shape and then extend them in sub classes but locust doesn't under stands extended classes and instatiate bas classes these are base 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

and this is the file that I run 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)} 

Solution

  • Base User classes need an attribute abstract = True to not be instantiated. https://docs.locust.io/en/stable/api.html#locust.User.abstract

    I dont think you can do the same with load shape classes, but you can use class attributes (which you can manipulate in your locustfile after importing it)

    Like removing Rps class and instead just doing

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