Calmcode - locust: setup

Install and setup Locust in Python.

1 2 3 4 5 6 7 8

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/")