locustio / locust

Write scalable load tests in plain Python 🚗💨
MIT License
24.64k stars 2.96k forks source link

The data between the different locusts #389

Closed likezjuisee closed 8 years ago

likezjuisee commented 8 years ago

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?

zvxr commented 8 years ago

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': "a@gmail.com", 'password': "123"}, {'login_name': "b@gmail.com", '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']
likezjuisee commented 8 years ago

It really works, thank you very much!

likezjuisee commented 8 years ago

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?

likezjuisee commented 8 years ago

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.

zvxr commented 8 years ago

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)

androane commented 7 years ago

@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

DataGreed commented 5 years ago

Same thing, initial solution not working

DataGreed commented 5 years ago

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': "a@gmail.com", 'password': "123"}, {'login_name': "b@gmail.com", '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']
DataGreed commented 5 years ago

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': "a@gmail.com", 'password': "123"}, {'login_name': "b@gmail.com", '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']
DataGreed commented 5 years ago

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

karol-brejna-i commented 5 years ago

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