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!
In order to run this test we'll need to execute the command locust in the script's directory from the command line, which will start a web user interface on port 8089. Just navigate to http://localhost:8089 on the browser to access it.
A Custom Load Shape lets you control the spawned number of users in code, but if you want to ensure you're getting a certain amount of Requests Per Second (RPS), that's going to be up to how you write your code. The code your Locust users runs needs to be written in such a way that each user is only making one request.
It's important to remember that the locustfile.py is just an ordinary Python module that is imported by Locust. From this module you're free to import other python code just as you normally would in any Python program. The current working directory is automatically added to python's sys.
e.g. say I want a user to make 3 GET requests after sign in. I want the user count as 3. In this case, each user should sign-in and make 3 GET request and locust should stop.
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.
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 .
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")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With