0

In my locustfile I defined test_on_start and test_on_stop events to read a file needed for the test and to write detailed statistics in a CSV at the end of the test. when running in distributed mode, these events occur on the master, not the worker. I am assembling a list of detailed stats for each task in a task sequence and at the end of the test writing a CSV file when the test stops. I found this stackoverflow question which references a setup and teardown. I added these to my class User(HttpUser): but they appear to not be executed.

How can I mimic these events when the test is running on a worker in distributed mode?

Is there a better way?

I am using User on_start and on_stop already - my on_start calls a function to select a random user from a list which was created when the @events.test_start.add_listener is fired, which only happens on the master and not on the workers, so the worker doesn't have any user login data.

It seems counter productive to open the file, read it, select a user at random and close it every time the User on_start method is called. User on_start also sets up the iteration list [] which is where i store the times per task.

When the task sequence is done, meaning the last task is executed, i do a self.interrupt() which runs on_stop, which is where I take the iteration times, and put them into a second list, which is later written using the CSV module. maybe it would be better to just write the data to the CSV during on_stop

Chris Hare
  • 161
  • 2
  • 12

1 Answers1

0

The setup/teardown for individual Users has been removed (because they were confusing, as it was run on the first instance of that User class, and when people set properties on that instance got very confused by the fact that later instances didnt get that). Tbh, I wish they had just been replaced by class methods...

The User still has on_start/stop methods though, and if you combine that with a flag it may be able to do what you want. Something like this:

class MyUser(HttpUser):
    stopped = False
... 
    def on_stop(self):
        if not MyUser.stopped:
             MyUser.stopped = True
             # write your csv
             # this doesnt guarantee that all your Users are finished though.

https://docs.locust.io/en/stable/writing-a-locustfile.html#on-start-and-on-stop-methods

Cyberwiz
  • 11,027
  • 3
  • 20
  • 40
  • Hmm... I'm not sure there is a smooth built in solution for what you're trying to do. In the past I've created a data file on master, spilt it in one part per worker and scp:ed it to them (either in setup or before even launching locust at all), but nowadays I always use a database to store my testdata. – Cyberwiz May 28 '20 at 05:46