Iterative Load Testing with Step Size in Locust
Why Use Step-Load Testing?
Example Code: Step-Load with Locust
from locust import HttpUser, task, LoadTestShape, constant
class StepLoadShape(LoadTestShape):
step_time = 60 # Each step lasts 60 seconds
step_users = 10 # Add 10 users per step
spawn_rate = 5 # Spawn 5 users per second
max_users = 100 # Maximum of 100 users
def tick(self):
run_time = self.get_run_time()
# Calculate the current step based on elapsed time
current_step = run_time // self.step_time + 1
if current_step * self.step_users > self.max_users:
return None # Stop the test if the maximum user count is reached
user_count = current_step * self.step_users
return user_count, self.spawn_rate
class MyReqRes(HttpUser):
wait_time = constant(1) # 1 second wait time between tasks
host = "http://localhost:8001"
@task
def get_todos(self):
# Request a specific resource and validate the response
with self.client.get("/todos/104", name="test todo", catch_response=True) as resp1:
if resp1.status_code == 200 and "Christian Adams" in resp1.json().get("name", ""):
resp1.success()
else:
resp1.failure("Validation failed for 'Christian Adams'")
How the Step-Load Works
PreviousUsing catch_response in Locust for Custom Response ValidationNextEnhancing Your Locust Tests with Custom Logging
Last updated