How to assign different behaviors to different users in locust

Viewed 1298

I want to simulate two users running different tasks at the same time. How to do that with locust performance testing framework?

2 Answers

Just define multiple User classes:

class User1(HttpUser):
    @task
    def task1(self):
    ...

class User2(HttpUser):
    @task
    def task2(self):
    ...

You’ll also need to start Locust with an even number of users if you want the distribution between user types to be 50-50.

Your Locust Tasks are random based on their weight. So every user will be performing tasks in different sequence.

If you want users to do different tasks, you can decide which functions to call based on user roles or other logic for example.

class Main(TaskSet):
    def setup(self):
        # Login
        # Get user role and decide which function to call
        if user.role == 'admin':
            self.admin_actions()
        else:
            self.other_user_actions()

        def admin_actions():
            # Do some admin stuff

        def other_user_actions():
            # Do some other stuff
    @task
    def random_task(self):
        # Some random task that everyone does

class MyLocust(HttpLocust):
    task_set = Main
    wait_time = between(5, 10)
Related