Locust: The data between the different locusts

Created on 10 Mar 2016  ·  11Comments  ·  Source: locustio/locust

If I have some test users, now I want to apply one user to a locust, and this user won't be used by the others, how can I do?

All 11 comments

You could include an iterable of user data which you pop from on_start. Something like:

class APIUser(HttpLocust):
    # ...
    task_set = WebsiteUser
    users = [{'login_name': "[email protected]", 'password': "123"}, {'login_name': "[email protected]", 'password': "456"}]

class WebsiteUser(TaskSet):
    def on_start(self):
        if len(self.locust.users) > 0:
            user = self.locust.users.pop()
            self.locust.login_name = user['login_name']
            self.locust.password = user['password']

It really works, thank you very much!

Another question is after I stopped the test from website button which named “stop”, I started the test again, but the data is empty. So how can I add the data back to the data set after locust is stopped?

How can I control the datas between the different slaves, if one slave pop one data, meanwhile the data can't be used by the others.

If it is general, static data that all the instances share, it can be included as a variable within the HttpLocust class. If it is data that is specific to a locust (such as login credentials), it might be best to keep separate, or instead of popping the data, reference it. Something like:

user = random.choice(self.locust.users)

@zvxr The initial solution you posted is not working (at least not for me). When one "worker" does pop from self.locust.users, another worker still sees the entire list of self.locust.users. The pop operation was executed just for the first worker

Same thing, initial solution not working

This seems to be the right (although a little bit ugly) way to do it, (fixed the example above):

class APIUser(HttpLocust):
    # ...
    task_set = WebsiteUser
    users = [{'login_name': "[email protected]", 'password': "123"}, {'login_name': "[email protected]", 'password': "456"}]

class WebsiteUser(TaskSet):
    def on_start(self):
        if len(self.locust.users) > 0:
            user = self.locust.__class__.users.pop()
            self.login_name = user['login_name']
            self.password = user['password']

Although the strategy above won't work if you need to share data between several locusts (e.g. if you run locust like this: locust --host http://example.com ApiUser SomeOtherLocustClass)

I would recommend having a separate var or static class for it, e.g.:

class SharedData(object):
    users = [{'login_name': "[email protected]", 'password': "123"}, {'login_name': "[email protected]", 'password': "456"}]

class APIUser(HttpLocust):
    # ...
    task_set = WebsiteUser

class SomeOtherLocustClass(HttpLocust):
    # ...
    task_set = SomeOtherUser


class WebsiteUser(TaskSet):
    def on_start(self):
        if len(self.locust.users) > 0:
            user = SharedData.users.pop()
            self.login_name = user['login_name']
            self.password = user['password']

Another question is after I stopped the test from website button which named “stop”, I started the test again, but the data is empty. So how can I add the data back to the data set after locust is stopped?

You can handle test stop via Locust.teardown method

For distributed locust cluster something similar to this could be used: https://medium.com/locust-io-experiments/locust-experiments-feeding-the-locusts-cf09e0f65897

Was this page helpful?
0 / 5 - 0 ratings