Search code examples
pythonlocust

Running a request in background every x seconds using Locust


I try to simulate users using my client application with Locust for load testing. Users fires some http requests by themselves and besides that the client also has a background thread that fires an http request every x seconds.

How can I simulate the background thread in Locust?

I tried the following and it kind of works but the "background thread" is not stopping when a test is stopped. Apparently that greenlet doesn't see updates on self._state.

class MyUser(HttpUser):
    host = 'http://localhost'
    wait_time = between(1,5)

    def on_start(self):
        gevent.spawn(self._on_background)

    def _on_background(self):
        while self._state != LOCUST_STATE_STOPPING:
            gevent.sleep(10)
            self.client.get('/status')

    @task(1)
    def main_page(self):
        self.client.get('/main')

    @task(2)
    def hello(self):
        self.client.get('/hello')

Solution

  • Instead of self._state, try checking self.environment.runner.state.

    Also, 10s is quite a long time. Maybe change it so that the loop runs every second and do the request every ten iterations.