Locust is a python based load testing framework.

module-showcase

Description

Locust is a python based load testing framework, it is incredibly useful to do load testing before an application launch to ensure stability for your users once many of them get into an application.

Definitions

Load Testing

Load testing is faking user interactions in a web app to see how the system handles large groups of users.

Locust

Locust is a user load testing framework built in python. It allows you to create task queues (i.e. log in, write an article, log out), set spawn and max user rates, and provide analytics to tell you how many users failed and or how much the system slowed down while all the 'users' were executing their task queues.

Demo Description

The demo provided sets up a small website that has a user counter that goes up by 1 for every request made (defined in demo_site.py). Then in locust_demo.py I define a set of Tasks (in this case just 1) to have each 'user' go through and ping the websites root URL "/" (the homepage).

Usage

Dependencies

First you will need to install all dependencies this can be done by running either pip install -r requirements.txt or pip3 install -r requirements.txt.

Running

Below is the code for a demo that can be run with a few steps:

  1. Start the demo site using python site_demo.py or python3 site_demo.py
  2. Run locust locust -f locust_demo.py --host=http://localhost:5000; The -f is to specify your configuration file, and the host is the starting URL of the site. (Note you will need a separate terminal instance to do this.)
  3. Locust will give you a port you can access the frontend through (i.e. localhost:8089) and from there you can choose how many users to create, and set a spawn rate.

Real World Applications

Load testing is an integral part of setting up an application to make sure it can handle at least as many expected users as you have. It also allows you to test things like DOS (denial of service) protection, and make sure your app (or even your server hardware) doesn't fall apart if there is a sudden spike.

Additional info

Locust Website

Locust Quickstart

Flask Documentation

requirements.txt

Flask
locustio

Files

site_demo.py
"""A demo site that increments a counter with every new visitor""" from flask import Flask app = Flask(__name__) visitor_count = 0 @app.route('/') def hello(): """On page visit increments and displays visitor count""" global visitor_count # Grabs the visitor_count global variable visitor_count += 1 # Increments the visitor_count global variable return f"There have been: {visitor_count} visitor(s)" if __name__ == "__main__": """App needs to be published on a port available to outside services since locust emulates users connecting from different users""" app.run(host="0.0.0.0")


locust_demo.py
"""A test to ping a hosts root URL with GET requests""" from locust import HttpLocust, TaskSet, task class UserBehavior(TaskSet): """Extends the TaskSet class; used to define user behaviours you want the load test to go through per user.""" def on_start(self): """on_start is called when a Locust start before any task is scheduled""" pass def on_stop(self): """on_stop is called when the TaskSet is stopping""" pass @task(1) def index(self): """Does a GET request to the root URL of the defined host""" self.client.get("/") class WebsiteUser(HttpLocust): """Extends the HttpLocust class; used to define user variables such as TaskSet(s) and wait time between tasks""" task_set = UserBehavior # Set each users behaviour to the defined UserBehavior class min_wait = 5000 # Minimum amount of time (in milliseconds) to wait between tasks max_wait = 9000 # Maximum amount of time (in milliseconds) to wait between tasks