@task decorator
A key feature of Locust is the @task
decorator, which allows you to define the specific actions (or tasks) your simulated users will perform.
n this chapter, we’ll dive into the @task
decorator and illustrate its use with an example that prints the current date and time, along with a friendly greeting.
What Is the @task
Decorator?
@task
Decorator?The @task
decorator in Locust is used to mark methods in a User
class as tasks to be executed during a load test. Each method decorated with @task
represents an action that the simulated user will perform.
Locust automatically identifies these tasks and executes them based on the configuration you define, such as the wait time between tasks or their relative weighting.
Example: Printing DateTime and a Greeting
Here’s a simple example of a Locust User
class with two tasks: printing the current date and time and printing a greeting message.
Explanation of the Code
MyUser
Class This class represents the behavior of a simulated user. Every simulated user will execute the tasks defined in this class.wait_time
Thewait_time
attribute determines how long a user will wait before executing the next task.Here,
between(1, 5)
specifies a random delay between 1 and 5 seconds.
Task Methods
print_datetime
: Prints the current date and time using Python'sdatetime.now()
.print_greeting
: Prints the message"Hello!"
.
@task
Decorator The@task
decorator marks the methodsprint_datetime
andprint_greeting
as tasks for the simulated users.
Fine-Tuning Tasks with Weights
You can adjust the relative frequency of tasks using weights. For instance:
Last updated