Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to assign different behaviors to different users in locust

Tags:

locust

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

like image 969
janetsmith Avatar asked Nov 26 '25 10:11

janetsmith


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.

like image 151
Cyberwiz Avatar answered Nov 29 '25 01:11

Cyberwiz


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)
like image 29
sykez Avatar answered Nov 29 '25 00:11

sykez



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!