0

Basically I need to spawn 30 users and have 50 different tasks for them I need them to run in parallel. So I am trying to generate 50 tasks like following:

class UserSimulation(HttpUser):
    host = os.environ['BASE_URL']

    # time in seconds the simulated user will wait before proceeding to the next task
    wait_time = between(1, 2)

    for item_id in range(1, 51):

        @task(1)
        def view_items_with_different_item_ids(self, item_id=item_id):
            self.client.get(
                url=f"/my-url/item24_00{item_id}",
                verify=False,
                auth=(os.environ['USERNAME'], os.environ['PASSWORD'])

For obvious reasons this approach doesn't let me create 50 tasks dynamically, as only the last one gets saved. Any ideas of a work-around to it?

D_Asti
  • 59
  • 7

1 Answers1

2

To do it the way you're trying, try creating different functions programmatically. I don't know how to use decorators with those, but if nothing else you can add the functions to the Locust task list when you create them. Just create a task list tasks = [] then tasks.append(view_items_with_different_item_ids_1) when the function is created.

However, I'm not positive if this is necessary, based on your description of what you want. If you only need 30 users to go make that call 50 times, you only need the one task and you can loop through the call in there.

class UserSimulation(HttpUser):
    host = os.environ['BASE_URL']

    # time in seconds the simulated user will wait before proceeding to the next task
    wait_time = between(1, 2)

    @task(1)
    def view_items_with_different_item_ids(self):
        for item_id in range(1, 51):
            self.client.get(
                url=f"/my-url/item24_00{item_id}",
                verify=False,
                auth=(os.environ['USERNAME'], os.environ['PASSWORD'])

If you need a random number instead of a sequential one but need to make sure each one is called once:

    import random

    item_ids = list(range(1,51))
    random.shuffle(item_ids)
    @task(1)
    def view_items_with_different_item_ids(self):
        for item_id in item_ids:
            self.client.get(
                url=f"/my-url/item24_00{item_id}",
                verify=False,
                auth=(os.environ['USERNAME'], os.environ['PASSWORD'])

If you just want to pull a random number all the time and don't care about repeats:

    import random

    item_ids = list(range(1,51))
    @task(1)
    def view_items_with_different_item_ids(self):
        random_id = random.choice(item_ids)
        self.client.get(
            url=f"/my-url/item24_00{random_id}",
            verify=False,
            auth=(os.environ['USERNAME'], os.environ['PASSWORD'])
Solowalker
  • 2,431
  • 8
  • 13
  • if I do it as you have suggested, and loop through within the task- then the simulated user will sequentially visit every single url. But, what I need is to run these tasks in parallel. I basically, need to spawn 30 users and have 50 tasks for each of them. These tasks should be picked randomly by users. – D_Asti Oct 09 '20 at 06:25
  • Tasks aren’t run in parallel by users. Even if you had 50 tasks they wouldn’t run all 50 at once, just one a time. But I’ll update my answer to include an example for randomness in addition to sequential. – Solowalker Oct 09 '20 at 13:48