Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is there a way to setup/teardown before running the load tests #553

Closed
sarvi opened this issue Feb 24, 2017 · 13 comments
Closed

Is there a way to setup/teardown before running the load tests #553

sarvi opened this issue Feb 24, 2017 · 13 comments

Comments

@sarvi
Copy link

sarvi commented Feb 24, 2017

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 ?

@cgoldberg
Copy link
Member

cgoldberg commented Feb 24, 2017

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

@sarvi
Copy link
Author

sarvi commented Feb 24, 2017

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 ?

@cgoldberg
Copy link
Member

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.

@aldenpeterson-wf
Copy link
Contributor

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.

@MushuEE
Copy link

MushuEE commented Dec 28, 2018

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?

@payton
Copy link

payton commented Jun 12, 2019

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

@cgoldberg
Copy link
Member

see: https://docs.locust.io/en/stable/writing-a-locustfile.html#setups-teardowns-on-start-and-on-stop

@payton
Copy link

payton commented Jun 12, 2019

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.

@MushuEE
Copy link

MushuEE commented Jun 13, 2019

@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

@payton
Copy link

payton commented Jun 17, 2019

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

@GUSTAVOVARGASAVILA
Copy link

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

@GUSTAVOVARGASAVILA
Copy link

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")

@TWoeste
Copy link

TWoeste commented Jun 29, 2021

You can try to use the @event.init.add_listener
Here is an example from the docs

from locust.runners import MasterRunner

@events.init.add_listener
def on_locust_init(environment, **kwargs):
    if isinstance(environment.runner, MasterRunner):
        print("I'm on master node")
    else:
        print("I'm on a worker or standalone node")```

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants