Is there a way to perform certain action by only first user at the end of test?

Viewed 68

I have just started using locust with one custom client. At the end of the test (on_stop) I am fetching some results from APM and logging them.

Issue : when I use below command, all 1000 users are fetching those details and logging the data from APM. But I want only the 1st user to do this job and let other users to ignore this call.

$ locust -f --headless -u 1000 -r 100 --run-time 1h30m

Please let me know if someone has done something similar earlier. Thanks in advance.

2 Answers

My understanding is that you are not running in distributed mode and you are using User class' on_stop method. I would try handling it with some global variable, it is quick and might need some work, like this:

apm_logging_user_assigned = False
class MyUser(User):
    test stuff here ...

    def on_start(self):
        if apm_logging_user_assigned is False:
            self.apm_log = True
            apm_logging_user_assigned = True
        super().on_start()

    def on_stop(self):
        if self.apm_log:
            do your apm stuff here...
        super().on_stop()
Related