logo

... locust.


It's nice to know how much traffic your webapp can handle before it gets this amount of traffic for the first time. A tool that can help you calculate this is called locust.


Notes

Note that to get started with this series of videos you will need to install some packages.

pip install locust fastapi uvicorn flask

We're going to need two files. One file fastapp.py will contain the webapp that we're going to benchmark and another file benchmark.py will contain a definition of the average user that will use said webapp. This is the contents of fastapp.py:

from fastapi import FastAPI

app = FastAPI()

@app.get("/hello/")
def root():
    return {"message": "hello world"}

You can run this service locally by running;

uvicorn fastapp:app --reload --workers 1

The --workers flag is set so that we're more sure that we're benchmarking fastapi instead of uvicorn. This is the contents of benchmark.py:

from locust import HttpUser, between, task

class WebsiteUser(HttpUser):
    wait_time = between(2, 4)

    @task
    def attempt(self):
        self.client.get("/hello/")

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

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