Skip to content Skip to sidebar Skip to footer

Multiple Tasks And Reading User And Hatch Rate From Locust Config File

How to call separate end points with separate no of users and hatch rate, as specified in config file. Following is basic code. Please suggest. from locust import HttpUser, task, b

Solution 1:

It depends on your use case and exactly what it is you're looking for. If it'd be acceptable for each user Locust generates to first run getInfo() and then deleteEstatement(), you could use a SequentialTaskSet and define the tasks to be done in order. To run deleteEstatement() twice it could be duplicated in two different tasks or the same task could just do the same code twice. In this case, the every user would run the exact same steps in the same order and with a single steady hatch rate you'd have a steady hatch rate for both tasks.

If you don't need an exact number of tasks performed but just need the general idea of roughly 2 deleteEstatement() for every 1 getInfo(), you can use weights for either tasks or users. Locust will then randomly choose a task to perform for each user but, for example, could have 2 deleteEstatement() tasks and 1 getInfo() to choose from for each user so you'd roughly have a 2:1 ratio for the tasks you defined. As in the first scenario, you'd still have a single hatch rate for both tasks but the exact run count for each task would be a bit fuzzy (which is typically desirable as that is often closer to how real world traffic is).

As an aside, in both of those situations you would define tasks in your ApiUser class just once like tasks = [PostAndDeleteTasks, GetCallTasks]. How you have it, tasks would only have GetCallTasks in it so anything in PostAndDeleteTasks would never be run.

Another option is running 2 separate Locust instances, one running only getInfo() and one running only deleteEstatement(). You could then operate each one and dynamically change the user count and hatch rate for each at will. This is really the only built-in and supported way of having different hatch rates for different TaskSets.

But if you really don't want to have multiple Locust instances to manage, Locust is capable of running just about any code and has several different ways you can hook into Locust. The docs have an example about what they call custom clients that show you one way to do that. The key part is self._locust_environment.events.request_success.fire() and self._locust_environment.events.request_failure.fire() as that's where the code would send a message to Locust about what's happening and whether the task was a success or failure. You can also overwrite self.tasks in a TaskSet with either a list of functions to run as tasks in a sequence or as a dict of function: weight pairs. Again, this would still have one user count and hatch rate that would trigger this but then you're in full control of what happens from there.

And lastly, depending on what you need to do there are also useful event hooks and ways to use Locust as a library you could look into.


Post a Comment for "Multiple Tasks And Reading User And Hatch Rate From Locust Config File"