5

I need to implement the next logic:
1) Execute on_start method (eg login)
2) Execute the next task (task_2) 5 times
3) After that execute the next task (task_3) 10 times
Return to on_start etc...
So finally I need login:1, task_2: 5, task_3: 10. (5 and 10 times per 1 login)
I try to implement it with the next code:

class MyTaskSet(TaskSequence):
    def on_start(self):
        login()


    @seq_task(1)
    def task_2(self):
        print('Need to be executed 5 times after 1 login')

    @seq_task(2)
    def task_3(self):
        print('Need to be executed 10 times after 1 login')

class LocustUser(HttpLocust):
    host = http://localhost
    task_set = MyTaskSet

Could someone of performance guru help me with this logic?

SergeyMoroz
  • 117
  • 3
  • 11

2 Answers2

2

If I understand your use-case correctly you could create a task out of your login logic. If the login is in a on_start() i think it only gets run once. As of version 1.4.3 of Locust this can be accomplished as such:

class MyTaskSet(SequentialTaskSet):
    @task(1)
    def task_login(self):
        login()

    @task(5)
    def task_2(self):
        print('Need to be executed 5 times after 1 login')

    @task(10)
    def task_3(self):
        print('Need to be executed 10 times after 1 login')

This should run the tasks in order with the assigned weights.

Erik Poromaa
  • 139
  • 2
  • 8
  • Dont think this is correct, SequentialTaskSet will execute task in order, hence the weight on the task decorator don't make sense (since, it will run on a sequence – Federico Baù Apr 05 '23 at 07:58
1

I think the easiest way is just to use ordinary for-loops in a single task. No on_start, no seq_task or anything.

I think the documentation needs to be adjusted, because a lot of people get the (incorrect) impression that you can only do a single request in a task.

Cyberwiz
  • 11,027
  • 3
  • 20
  • 40
  • Should be accepted answer , but also using SequentialTaskSet and combination of multiple request in a task make sense . – Federico Baù Apr 05 '23 at 07:59