0

I saw that locust supports defining different user classes and shape classes and that it is possible to choose which one to run through the web-ui when locust is started with --class-picker

However, when I select a user class and a shape class, locust doesn't use the shape and uses only what I type in the "Number of Users" and "Spawn rate" fields on the interface.

How can I make locust use the selected shape file? This is how my user classes look: (the other user classes are very similar to this one)

from locust import HttpUser, TaskSet, task
from locust import LoadTestShape
import csv
import random

class Tasks1(TaskSet):
    DATA = {}

    def on_start(self):
        if not self.DATA:
            self._get_pics()

    def _get_pics(self):
        with open('targets.csv') as csv_file:
            csv_reader = csv.reader(csv_file, delimiter=',', quotechar="'")
            for row in csv_reader:
                self.DATA['images/' + row[0]] = '{"x":' + row[1] + ',"y":' + \
                    row[2] + ',"width":' + row[3] + ',"height":' + row[4] + '}'

    @task
    def service(self):
        key = random.choice(list(self.DATA.keys()))
        payload = {self.DATA[key]}
        with open(key, 'rb') as image:
            self.client.post('/process', headers={'Authorization': 'Bearer ' + token, 'Accept-Encoding': 'gzip'}, files={'image': image}, data=payload)

    @task
    def stop(self):
        self.interrupt()


class FirstUser(HttpUser):
    tasks = [Tasks1]

And these are the shape classes: ShapeClass1.py:

from locust import LoadTestShape

class ramp_up_20min_40users_hold_5min_ramp_down_5min_1user(LoadTestShape):

    stages = [
        {"duration": 1200, "users": 40, "spawn_rate": 0.034},
        {"duration": 300, "users": 40, "spawn_rate": 1},
        {"duration": 300, "users": 1, "spawn_rate": 0.0325}
    ]

    def tick(self):
        run_time = self.get_run_time()

        for stage in self.stages:
            if run_time < stage["duration"]:
                try:
                    tick_data = (
                        stage["users"], stage["spawn_rate"], stage["user_classes"])
                except:
                    tick_data = (stage["users"], stage["spawn_rate"])
                return tick_data

        return None

ShapeClass2.py:

from locust import LoadTestShape

class ramp_up_1min_40users_hold_20min(LoadTestShape):

    stages = [
        {"duration": 60, "users": 40, "spawn_rate": 0.67},
        {"duration": 1200, "users": 40, "spawn_rate": 1},
    ]

    def tick(self):
        run_time = self.get_run_time()

        for stage in self.stages:
            if run_time < stage["duration"]:
                try:
                    tick_data = (
                        stage["users"], stage["spawn_rate"], stage["user_classes"])
                except:
                    tick_data = (stage["users"], stage["spawn_rate"])
                return tick_data

        return None

I can see the choices in the UI. However, the Number of users and spawn rate field are not disabled and locust executes what I type in there and not what is defined in the load shape files.

Am I missing something here?

nixpix
  • 53
  • 1
  • 5

0 Answers0