Locust: Is there a way to setup/teardown before running the load tests

Created on 24 Feb 2017  ·  12Comments  ·  Source: locustio/locust

This is usually stuff that needs to happen only once across before starting any clients/tasks
and once after all load test is complete.

I can use the quit event for the tear down i think.

Is there a start event that I can plugin into for setup ?

Most helpful comment

You should be able to add the code to the locustfile before you define the locust or anything else for your startup event.

Just make sure your code is setup to execute on import.

All 12 comments

you want to use on_start (note that is run per virtual user)
see: http://docs.locust.io/en/latest/writing-a-locustfile.html#the-on-start-function
and the examples at: http://docs.locust.io/en/latest/quickstart.html

I am looking for some way to invoke a setup() code before starting any virtual user.
Think of this as a way to setup/teardown the testbed/servers/etc. that all virtual user/client will be talking to.

Right now, the only for me to do it, is to wrap locustio code with my own python wrapper, that does the setup/teardown, etc and then invoke locustio.main()

I don't think on_start() works that way does it ?

I don't think on_start() works that way does it ?

not really... it runs per user, so wouldn't really be useful for a global setup step.

You should be able to add the code to the locustfile before you define the locust or anything else for your startup event.

Just make sure your code is setup to execute on import.

I need to do a singular setup to prime a database with information prior to the TaskSet execution. I need to access self.client to post resources... How would I access that variable prior to locust setup?

Bumping this. I am in the same situation as @MushuEE

I need to create resources before the load tests start and remove them after all load tests finish.
@cgoldberg

Right, but the setup/teardown functions under a locust are executed before/after an individual locust is hatched, correct? I was looking for a way to setup shared resources among all locusts and teardown those shared resources at the very end.

@payton you can define the setup() method from HttpLocust

class WebsiteUser(HttpLocust):
    def setup(self):
        client = clients.HttpSession(base_url=self.host)
        client.post("/resources/", json=RESOURCE_1, headers=headers_with_auth)

    task_set = UserBehavior
    min_wait = 500
    max_wait = 900

I have not touched this for a little while now so I hope that helps

    def __init__(self):
        super(Locust, self).__init__()
        self._lock.acquire()
        if hasattr(self, "setup") and self._setup_has_run is False:
            self._set_setup_flag()
            self.setup()

and similarly

        if hasattr(self, "teardown") and self._teardown_is_set is False:
            self._set_teardown_flag()
            events.quitting += self.teardown

@MushuEE where does the events object come from in the teardown function setup?

@sarvi / @payton, they could find how to solve their need?

It worked this way for me. Reading a file before starting:

class User(HttpUser):
host = 'http://localhost:5000'
tasks = [UserBehavior]
wait_time = between(1, 2)
sock = None

@events.test_start.add_listener
def on_test_start(**kw):
    global USER_CREDENTIALS
    print("================================================================")
    if (USER_CREDENTIALS == None):
        with open('credentials.csv', mode='r') as file:
            reader = csv.reader(file)                
            USER_CREDENTIALS = [tuple(r) for r in reader]

@events.test_stop.add_listener
def on_test_start(**kw):
    print("test is stopping")
Was this page helpful?
0 / 5 - 0 ratings