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

Here's the flask app that we're running;

# flaskapp.py
from flask import Flask, jsonify
import time 

app = Flask(__name__)


@app.route('/hello/')
def hello_json():
    return jsonify({"message": "hello world again"})


@app.route('/sleep/')
def hello_sleep():
    time.sleep(0.1)
    return jsonify({"message": "slept"})

Here's the command to run the flask app;

export FLASK_APP=flaskapp.py; flask run --port 8000

Here's the user definition for locust;

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

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

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

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

As a final reminder: think hard about what you would like to benchmark. Otherwise you may risk that you're measuring something that is not that useful for your app.


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.