... locust: tasks


The FastApi app that we end up with is listed below:

from fastapi import FastAPI
from pydantic import BaseModel
import time
import random

class Simulation(BaseModel):
    uid: str
    n_sim: int

app = FastAPI()

def root():
    return {"message": "hello world"}

def simulate(sim:Simulation):
    return {"uid": sim.uid, "result": sum([random.random() for i in range(sim.n_sim)])}

If this syntax looks unfamiliar, feel free to check out the fastapi video series.

The new user behavior definition is listed below:

import random 
from locust import HttpUser, between, task, tag

class WebsiteUser(HttpUser):
    wait_time = between(1, 1.5)

    def on_start(self):
        self.uid = str(random.randint(0, 100_000)).zfill(6)

    def attempt(self):

    def simulate(self):
        self.client.post("/simulate/", json={
            "uid": self.uid, 
            "n_sim": random.randint(10, 20)

Feedback? See an issue? Something unclear? Feel free to mention it here.

If you want to be kept up to date, consider signing up for the newsletter.