7

I'm trying to understand the flow within a locust test. I've set up this very simple task set and user:

from locust import TaskSet, HttpLocust, task


class BlazeDemoTaskSet(TaskSet):

    def setup(self):
        print("hello from taskset setup")

    def teardown(self):
        print("hello from taskset teardown")

    def on_start(self):
        print("hello from taskset on_start")

    def on_stop(self):
        print("hello from taskset on_stop")

    @task
    def reserve_task(self):
        post_response = self.client.post(
            url="/reserve.php",
            params={"toPort": "Buenos Aries", "fromPort": "Paris"})


class BlazeDemoUser(HttpLocust):
    task_set = BlazeDemoTaskSet
    min_wait = 500
    max_wait = 1500
    host = "http://www.blazedemo.com"

    def setup(self):
        print("hello from httplocust setup")

    def teardown(self):
        print("hello from httplocust teardown")

and I run it with:

locust -f tests/blazedemo.py --no-web -c 1 -r 1 -n 2

I'm not seeing the HttpLocust setup or teardown methods being executed, and I'm not seeing the TaskSet setup, on_stop, or teardown methods being executed. The only methods that get run are on_start and reserve_task

According to the docs all of these methods should be run. Setup and teardown once per run, and on_start and on_stop for each user that is started.

Here is the entire output from Locust:

[2018-03-22 18:26:50,698] C02PV7NSG8WP/INFO/locust.main: Starting Locust 0.8.1
[2018-03-22 18:26:50,698] C02PV7NSG8WP/INFO/locust.runners: Hatching and swarming 1 clients at the rate 1 clients/s...
 Name                                                          # reqs      # fails     Avg     Min     Max  |  Median   req/s
--------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------
 Total                                                              0     0(0.00%)                                       0.00

[2018-03-22 18:26:50,699] C02PV7NSG8WP/INFO/stdout: hello from taskset on_start
[2018-03-22 18:26:50,699] C02PV7NSG8WP/INFO/stdout: 
[2018-03-22 18:26:51,703] C02PV7NSG8WP/INFO/locust.runners: All locusts hatched: BlazeDemoUser: 1
[2018-03-22 18:26:51,703] C02PV7NSG8WP/INFO/locust.runners: Resetting stats

 Name                                                          # reqs      # fails     Avg     Min     Max  |  Median   req/s
--------------------------------------------------------------------------------------------------------------------------------------------
 POST /reserve.php?toPort=Buenos+Aries&fromPort=Paris               1     0(0.00%)     140     140     140  |     140    0.00
--------------------------------------------------------------------------------------------------------------------------------------------
 Total                                                              1     0(0.00%)                                       0.00

[2018-03-22 18:26:53,268] C02PV7NSG8WP/INFO/locust.runners: All locusts dead

[2018-03-22 18:26:53,268] C02PV7NSG8WP/INFO/locust.main: Shutting down (exit code 0), bye.
 Name                                                          # reqs      # fails     Avg     Min     Max  |  Median   req/s
--------------------------------------------------------------------------------------------------------------------------------------------
 POST /reserve.php?toPort=Buenos+Aries&fromPort=Paris               2     0(0.00%)     139     139     140  |     140    0.00
--------------------------------------------------------------------------------------------------------------------------------------------
 Total                                                              2     0(0.00%)                                       0.00

Percentage of the requests completed within given times
 Name                                                           # reqs    50%    66%    75%    80%    90%    95%    98%    99%   100%
--------------------------------------------------------------------------------------------------------------------------------------------
 POST /reserve.php?toPort=Buenos+Aries&fromPort=Paris                2    140    140    140    140    140    140    140    140    140
--------------------------------------------------------------------------------------------------------------------------------------------

What have I missed? Thank you in advance!

George Imerlishvili
  • 1,816
  • 2
  • 12
  • 20
user1641178
  • 71
  • 1
  • 4

2 Answers2

4

According to the docs all of these methods should be run.

this functionality is not yet available in the latest stable release on PyPI (currently 0.8.1). setup/terdown support was recently merged and will be released in version 0.9. For now, you must install master branch from the locustio Git repo to use these.

installing from master:

either run:

  • pip install -e git+https://github.com/locustio/locust.git@master#egg=locustio

or clone the git repo, and then run:

  • pip install -e .
TylerLubeck
  • 608
  • 4
  • 6
Corey Goldberg
  • 59,062
  • 28
  • 129
  • 143
  • FYI the docs for latest stable release are available here: https://docs.locust.io/en/stable/ – Corey Goldberg Mar 26 '18 at 14:26
  • EDIT: I looked at the docs and they do not mention this functionality is not yet available. Looks like the docs need to be updated? – user1641178 Mar 27 '18 at 14:42
  • no, the docs are up to date already. The "stable" docs are for the current release (0.8.1), while the "latest" docs are generated from master (where this feature exists). Once released, you will see it in the "stable" docs. – Corey Goldberg Mar 27 '18 at 15:18
  • 1
    Ah! Ok, now I see. I've found the 'stable' docs on the site, and you're correct, it does not list the setup/teardown as a feature. Cool. Thank you for pointing me in the correct direction Corey! – user1641178 Mar 29 '18 at 14:26
  • 2
    When is it planned to have the 0.9 version released? It will be very interesting to use this feature – A. Romeu Jun 20 '18 at 15:45
  • looks like `pip install -e` does not work with that git url – Ghilteras Jul 26 '18 at 20:06
  • @CoreyGoldberg, when I initialize an object in setup(), it's only available to a single locust instance, is there any way to make it accessible for every locust? – ustinov Aug 21 '18 at 02:19
3

Locust.setup, Locust.teardown, TaskSet.setup and TaskSet.teardown hooks have been removed with version 1.0. Description can be found on the docs.

test_start and test_stop events can be used to achieve the same thing for the versions >= 1.0

From the docs:

from locust import events

@events.test_start.add_listener
def on_test_start(**kw):
    print("test is starting")

@events.test_stop.add_listener
def on_test_stop(**kw):
    print("test is stopping")
basickarl
  • 37,187
  • 64
  • 214
  • 335