Random User Simulation with Locust

  • Share this:

Code introduction


This code defines a Locust user class named RandomUser, which inherits from HttpUser. This user class will wait randomly between 1 to 5 seconds, and then send a POST request to a random endpoint. The request body contains randomly generated usernames and email addresses.


Technology Stack : Locust, HttpUser, task, between

Code Type : Function

Code Difficulty : Advanced


                
                    
from locust import HttpUser, task, between
import random

def random_http_user():
    class RandomUser(HttpUser):
        wait_time = between(1, 5)

        @task
        def random_post(self):
            self.client.post("/random_endpoint", json={
                "name": f"User_{random.randint(1, 1000)}",
                "email": f"user{random.randint(1, 1000)}@example.com"
            })

    return RandomUser