Random User Interaction Simulation

  • Share this:

Code introduction


This code defines two tasks: random_task and random_click, which simulate random webpage clicks and wait times for a user. The random_task task adds a random number to the user's stats, the random_click task simulates a random GET or POST request, and the random_wait task simulates a random wait time before the next action.


Technology Stack : Locust

Code Type : The type of code

Code Difficulty :


                
                    
from locust import Locust, task, between
import random

def generate_random_user(locust):
    @task
    def random_task():
        # Generate a random number and add it to the Locust user's stats
        random_number = random.randint(1, 100)
        locust.stats['random_number'] += random_number
        # Sleep for a random duration to simulate real user behavior
        between(1, 5).sleep()

def generate_random_user(locust):
    @task
    def random_click():
        # Simulate a random click on a webpage
        if random.choice([True, False]):
            locust.client.get("/random-page")
        else:
            locust.client.post("/random-action", data={"key": "value"})

    @task
    def random_wait():
        # Simulate a random wait time before the next action
        between(1, 5).sleep()                
              
Tags: