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

Retrieving response time on Master while execution is going on through custom code in no-web mode #1351

Closed
kiranbhadale opened this issue Apr 23, 2020 · 3 comments

Comments

@kiranbhadale
Copy link

Does Locust has any function which can be used to read the request statistics on Master node in distributed mode while the execution is in progress. I want to read live stats and store them into a DB. I don't want to use the csv that gets generated at the end of the execution.
Note: The test will be in no-web mode.

@heyman
Copy link
Member

heyman commented Apr 23, 2020

In Locust 1.0 (haven't been released yet, but works in master) you can do this:

import gevent
from locust import HttpUser, events, task, constant
from locust.env import Environment
from locust.runners import LocalRunner, MasterRunner

class MyUser(HttpUser):
    wait_time = constant(1)
    @task
    def t(self):
        self.client.get("/")

greenlet = [None]
@events.init.add_listener
def on_init(environment: Environment, **kw):
    if isinstance(environment.runner, (MasterRunner, LocalRunner)):
        def stats_thread():
            while True:
                gevent.sleep(1)
                # here we can access to current stats in the master node through environment.stats
                print("Total requests made", environment.stats.total.num_requests)
        greenlet[0] = gevent.spawn(stats_thread)

# kill our stats greenlet when locust quits
@events.quitting.add_listener
def on_quitting(**kw):
    if greenlet[0]:
        gevent.kill(greenlet[0])

EDIT:

It's possible to do this is the latest released version as well but you'd have to retrieve references to stats and runner from other places (since we've refactored stuff). IIRC it would be:

from locust.runners import locust_runner
from locust.stats import global_stats

The init event is also new, but you could start the greenlet at the module level instead.

@kiranbhadale
Copy link
Author

@heyman Thanks for the code and the details. I am using 0.12.2 and looks like few import classes are not available in this version. Which version is recommended to incorporate the above code in my script?

@heyman
Copy link
Member

heyman commented Apr 23, 2020

See the additional info I added to my post. (Below "EDIT:" which I added now )

@heyman heyman closed this as completed Apr 29, 2020
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

2 participants