---
title: "How to Use Python to Load Test Websites"
description: "Need a fail-safe to handle increased traffic load on your web application? Learn how to load test your web application with Python in this tutorial!"
canonical: "https://adamtheautomator.com/load-test/"
---

# How to Use Python to Load Test Websites

> Need a fail-safe to handle increased traffic load on your web application? Learn how to load test your web application with Python in this tutorial!

Source: https://adamtheautomator.com/load-test/

---

ATA Learning

Tap to hide

[

ATA Learning

](/)

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Search for:  

*   [](https://twitter.com/adbertram)
*   [](https://github.com/Adam-the-Automator)
*   [](https://www.linkedin.com/company/adam-the-automator-llc)
*   [](/feed/)

![How to Use Python to Load Test Websites](https://adamtheautomator.com/wp-content/uploads/2021/11/How-to-Use-Python-to-Load-Test-Websites.jpg)

# How to Use Python to Load Test Websites

[![](https://secure.gravatar.com/avatar/5989a502f0009a732739a0b9015d53ea609ead5436ee9de6e7e662a34534bacd?s=192&d=mm&r=g)Chris Blackden](https://adamtheautomator.com/author/chris/)8 November 20218 min. read

Categories: [DevOps](/category/devops/)

Tags:[Python](/tag/python/)

Table of Contents

*   [Prerequisites](#prerequisites)
*   [Setting up a Static Website](#setting-up-a-static-website)
*   [Installing the Locust Framework](#installing-the-locust-framework)
*   [Running a Load Test Against Static Sites](#running-a-load-test-against-static-sites)
*   [Running a Load Test Against APIs](#running-a-load-test-against-apis)
*   [Conclusion](#conclusion)

Imagine that you have a new web application, and you know it performs well with a small group of users. But how would your application respond to the increased traffic that goes beyond the expected load? Run a load test against your web application with Python!

Not a reader? Watch this related video tutorial!

**_Not seeing the video? Make sure your ad blocker is disabled._**

In this article, you’ll learn how to run a load test on a web application by simulating hundreds of users accessing your application at once.

Read on to start load testing websites!

## Prerequisites

This tutorial will be a hands-on demonstration. If you’d like to follow along, be sure you have the following:

*   An Ubuntu Linux machine – This tutorial uses Ubuntu 21.04, but macOS or other Linux distros will also work, including WSL.
*   [Python 3.8](https://www.python.org/downloads/) or higher and installed.
*   [Pip package manager](https://pip.pypa.io/en/stable/) and the [Virtual Environment module](https://docs.python.org/3/library/venv.html).
*   A Microsoft Azure account with a [storage account](https://docs.microsoft.com/en-us/azure/storage/common/storage-account-overview).

Related:[How to Generate an Azure SAS Token to Access Storage Accounts](https://adamtheautomator.com/azure-sas-token/)

## Setting up a Static Website

Before running a load test against a website, you first must set up a demo site. You’ll create a static website and a demo API for this tutorial, but here you will focus on the static website first. A static website will show you the load testing basics, while an API will show you how to pass different parameters simulate different operations.

Related:[How to Host an Azure Static Website Backed by Cloudflare](https://adamtheautomator.com/azure-static-website/)

> _If you prefer to start running the load test against API, you can skip to the next section, where you’ll install Locust and run it on your machine. The demos in this tutorial use the [Swagger Petstore demo API](https://petstore.swagger.io/#/)._

1.  Create a new file in your preferred code editor, copy the code below into the new file and save it with your preferred name. For this example, the file is named _load\_test\_page.html_.

Below is an HTML document, which displays the “This is a test page.” text when opened on a web browser.

```markup
<!DOCTYPE html>
<html>
	<head>
		<title>ATA Load Balancing Demo</title>
	</head>
	<body>
		<h1>This is a test page.</h1>
	</body>
</html>
```

2\. Next, open your favorite web browser, and navigate to your storage account in the [Azure Portal](https://portal.azure.com/#home).

3\. Follow the steps below to enable static website hosting within a storage account, and set your website’s home page:

*   Click **Static website** on the left-hand navigation panel. The **Static website** page is where you’ll upload the files for your demo site.
*   Click **Enabled**, as shown below, to enable [static website hosting](https://aws.amazon.com/getting-started/hands-on/host-static-website/).
*   Enter **load\_test\_page.html** in the **Index document name** field, and click **Save**. Doing so automatically creates a blob container and a URL (primary endpoint), which points to your website. The URL won’t work yet, but save it for later.

![Enabling Static Website Hosting](https://adamtheautomator.com/wp-content/uploads/2021/11/image-94.png)

Enabling Static Website Hosting

4\. Click on **Storage Explorer (Preview)** in the left panel and navigate to **Blob Containers →** **$web**. The **$web** blob container, created by the Static website feature, will host all your static files.

Click **Upload**, and then upload the _load\_test\_page.html_ you created in step one.

![Uploading Static Files (HTML Page)](https://adamtheautomator.com/wp-content/uploads/2021/11/image-95.png)

Uploading Static Files (HTML Page)

5\. Finally, open a new tab on your web browser, navigate to the URL you noted in step two, and see if the URL is working now.

![Accessing Static Website's URL](https://adamtheautomator.com/wp-content/uploads/2021/11/image-96.png)

Accessing Static Website’s URL

## Installing the Locust Framework

You now have a site to test, but you still need load testing tools to test your website. Writing your code to manage high load testing is time-consuming and error-prone. So why not use the [Locust framework?](https://locust.io/) Locust is an open-source tool written in Python that you can install via the pip package manager.

> _If you need to brush up on your Python, check out [Python functions for newbies](https://adamtheautomator.com/python-function-return/)._

Locust can run load tests against static sites, web applications, or even APIs. The only requirement is that whatever you are testing has an HTTP(S) endpoint that you can reach from your device.

> _By default, the `pip` package manager installs packages under the ~/.local directory on Linux and MacOS, which is not on the system path. That path is available to a Python virtual environment only, which is why you’ll use `pip` in this tutorial._

1.  Open your terminal and run the commands below to create a new virtual environment and install Locust.

```bash
# Uses the venv module to create a new python virtual environment
python3 -m venv venv 
# Activate the virtual environment
source venv/bin/activate 
# Install locust in the new virtual environment
python3 -m pip install locust
```

![Starting and installing locust in a virtual environment](https://adamtheautomator.com/wp-content/uploads/2021/11/image-97.png)

Starting and installing locust in a virtual environment

2\. Run the commands below to make the new directory and navigate to it. Doing so keeps the locust files isolated from the rest of your codebase, making it easier to organize. For this example, the directory is named _locust\_dir_.

```bash
mkdir locust_dir
cd locust_dir
```

3\. Finally, create a new file called _locustfile.py_ in the _locust\_dir_ directory, then copy/paste the code below to the _locustfile.py_ file. This file will contain your test instructions, like how to simulate users, which endpoints those users will hit, and what parameters to pass along.

> _When Locust runs, it looks for a specific file named locustfile.py. So it’s best to save that file in a separate directory (locust\_dir), enabling Locust to find it, and you can create additional tests without overwriting other tests._

The code below runs an HTTP GET request against the path of the hostname you provide, which is equivalent to running `curl http://<Host>/` from the command line.

Once a response is received, Locust immediately sends the subsequent request, and the whole process repeats for every virtual user in the test.

```python
# Imports classes from Locust
from locust import HttpUser, task 

# Instantiate a new virtual user
class HelloWorldUser(HttpUser): 
    # This tells locust to treat the method below 
    # as something the virtual user would do
    @task 
    # Define a new method
    def hello_world(self): 
        # This method will run an HTTP GET request on the path `/` 
        # of the site you are testing
        self.client.get("/") 
```

> _If the path in the method looks incomplete, that’s because it is. Only the path and the query parameters are defined in a locust file, while the host is passed at runtime. This method will make more sense in the next step._

Related:[Getting Started: Python Functions for Newbies](https://adamtheautomator.com/python-function-return/)

## Running a Load Test Against Static Sites

Now that you’ve installed Locust, it’s time to run a load test! Load testing is about more than writing a series of web requests to see what your application does. A complete test records response times, failure rates, and error codes across different endpoints and configurations.

Locust is flexible in running as it can be called from the command line or through a web interface. For this tutorial, you’ll use the web interface to run a load test against a static site.

Run the `locust` command to start the locust graphical application using the test scenario from your _locustfile.py_ file.

```bash
locust 
```

![Run the locust command](https://adamtheautomator.com/wp-content/uploads/2021/11/image-98.png)

Run the locust command

2\. Open a web browser and navigate to http://localhost:8089/ to access Locust. The _8089_ port is the default port that Locust runs on.

Remember the URL (_primary endpoint_) that you noted in step three under the “Setting up a Static Website” section? Now is the time to use it in the next step!

3\. Finally, enter the URL (_primary endpoint_) into the Host field seen in the screenshot below, then click **Start swarming** to start the load test. Doing so tells any tests in your _locustfile.py_ to run against the URL you entered in the **Host** field.

> _For this demo, changing the value of the **Number of users** or the **Spawn rate** fields is optional._
> 
> _The **Number of users** is the max number of virtual users to create simultaneously. At the same time, **Spawn rate** is the number of users created every second until Locust reaches the peak concurrency of virtual users. The number of virtual users stays at max until you stop the test._

![](https://adamtheautomator.com/wp-content/uploads/2021/11/image-99-1024x690.png)

Running Locust load test on localhost:8089

While running tests, Locust adds the host, then sends a request to each path in the user class in real-time. Once a response is received, Locust immediately sends another.

You’ll see a list of endpoints tested shown below, as well as some statistics about the response times, failures, and requests per second.

![Viewing Locust Chart Showing the Number of Requests, Failures, and Response Times for the Test Case](https://adamtheautomator.com/wp-content/uploads/2021/11/image-100.png)

Viewing Locust Chart Showing the Number of Requests, Failures, and Response Times for the Test Case

> _If you prefer to see your website’s performance metrics in a graphical output, as shown below, click on the **Charts** tab._

![Viewing graphical output of load tests statistics](https://adamtheautomator.com/wp-content/uploads/2021/11/image-101.png)

Viewing graphical output of load tests statistics

## Running a Load Test Against APIs

Now that you’re familiar with the basics of Locust, let’s look at a more realistic test case. A static site is pretty stable, and there’s not a lot that can go wrong when you run performance testing. So why run a load test against an API?

An API can have a lot more variability in responses, less stable hosting options, and a lot more potential impact for your organization if they experience downtime.

> _If you’re not familiar with APIs and how they work, there’s an [article](https://adamtheautomator.com/invoke-restmethod/) that goes over basics like authentication, HTTP methods, and JSON responses. The article is intended for PowerShell, but the API concepts will be important later in this article._

1.  Replace the contents of the _locustfile.py_ file with the code below, which does a few things:

*   Runs a GET request to authenticate the user when Locust instantiates a new virtual user
*   Runs a GET request on the host, which will fail because that resource does not exist
*   Runs a POST request that passes a JSON body over a range of items in the same endpoint`.`

```python
import time # Importing time from Python system library
from locust import HttpUser, task

class HelloWorldUser(HttpUser):

    # This task will run when the user is instanciated
		def on_start(self): 
        # The petstore API doesn't have a user database, 
        # so any username and password will work
        username = "foo" 
        password = "bar"
        # This needs to conform to the loginUser endpoint from the API docs
        self.client.get("/user/login?username="+username+"&password="+password) 

    # This task will fail because there is no endpoint at https://petstore.swagger.io/v2/
    @task
    def hello_world(self): 
        self.client.get("/")

    # The (3) after the task decorator tells Locust to run this task 3 times as often
    @task(3) 
    def update_pets(self):
        # Using a for loop, count from 0-4 and use in HTTP request below
        for pet_id in range(5): 
            # Run HTTP POST request and pass a JSON Body
            self.client.post(f"/pet?petId={pet_id}", json={"name":"Mittens"}) 
            time.sleep(1) # Wait for 1 second before continuing 
```

> _Without adding an `on_start` statement to your test class, other methods would return a “403 Not Authorized” error when Locust tried to test them. Try commenting out this method and re-running Locust to see how it affects the test._

Related:[Understanding Python Loops and Flow Control for Newbies](https://adamtheautomator.com/python-loop/)

2\. Next, open the [docs for the pet store API](https://petstore.swagger.io/). The test case above is written specifically for this API, so having this page open alongside this tutorial will help you go through the endpoints.

Run the `locust` command below as you did in the “Running a Load Test Against Static Sites” section (step one).

```javascript
locust
```

![Run the locust command](https://adamtheautomator.com/wp-content/uploads/2021/11/image-102.png)

Run the locust command

Finally, enter _https://petstore.swagger.io/v2_ as the host in the Locust UI.

![Viewing endpoints of pet store API being tested](https://adamtheautomator.com/wp-content/uploads/2021/11/image-103.png)

Viewing endpoints of pet store API being tested

## Conclusion

In this tutorial, you’ve learned how to run a load test against a static website and an API. Now you’ve got a better idea of how to test your site for a large influx of users. Load testing comes in handy when preparing an application for release to customers. The same is true in testing auto-scaling configuration or application performance for low latency.

Now think about your current applications or ones that you support. How would they respond when a swarm of concurrent users is using it? What kinds of things do you think can load test scenarios point out to make your application perform better?

Share this article

[Share on X](https://twitter.com/intent/tweet?url=https%3A%2F%2Fadamtheautomator.com%2Fload-test%2F&text=How%20to%20Use%20Python%20to%20Load%20Test%20Websites)[Share on Facebook](https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fadamtheautomator.com%2Fload-test%2F)[Share on LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fadamtheautomator.com%2Fload-test%2F)

## Related Posts

![](https://adamtheautomator.com/wp-content/uploads/2025/11/55418e927e511ae263219c072e27d637c2a967a5036de7020b329582db775c26.png)

### [Automating Docker Container Health Checks with Python and Local Notifications](/docker-health-checks-python/)

Docker's built-in health checks are passive—they tell Docker when a container fails, but do they tell you? In this tutorial, we'll build a lightweight Python monitoring system that runs entirely on your infrastructure with zero external dependencies. You'll learn to detect container failures in real-time, send instant alerts, and maintain a complete audit log of every state change.

![](https://adamtheautomator.com/wp-content/uploads/2023/01/google-cloud-storage-python.jpg)

### [How to Install & Use the Google Cloud Storage Python Client](/google-cloud-storage-python-client/)

Discover how to use the Google Cloud Storage Python client in your projects and make your DevOps pipelines work even better!

![](https://adamtheautomator.com/wp-content/uploads/2022/10/How-to-Manage-Python-Libraries-with-Ansible-Pip.jpg)

### [How to Manage Python Libraries with Ansible Pip](/ansible-pip/)

Learn how to effectively manage Python libraries with the Ansible Pip module and take control of your Python dependencies!

## Categories

*   [IT Ops](/category/it-ops/)
*   [Cloud](/category/cloud/)
*   [DevOps](/category/devops/)
*   [Home Ops](/category/home-ops/)
*   [Information Security](/category/infosec/)
*   [Software Development](/category/software-development/)

## Site

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Copyright 2026© ATA Learning | [Privacy Policy](/privacy/)
