Mastering Load Testing with Locust on a GCP VM: A Step-by-Step Guide
Learn How to Set Up and Run Locust on a Google Cloud Platform Virtual Machine for Comprehensive API Endpoint Testing
In this beginner-friendly tutorial, we’ll show you how to install and set up Locust on a Google Cloud Platform (GCP) virtual machine (VM) so that you can effectively load test your API endpoints. To help you master the art of load testing using Locust and GCP, this comprehensive guide offers you step-by-step instructions, best practices, and insightful advice.
We can install Locust on our local machine and perform the load testing, but the reason for installing Locust on a GCP VM is to overcome unstable internet connections and network penalties. For example, let’s suppose your service is running in US-CENTAL-1 and you are performing load tests from your local system in India. Your unstable internet connection will show up in the latency statistics. Also, for each call to your service, you will have to pay a network penalty of 300 ms, which will show up in the p99, p90, e.t.c. latency stats.
Table of Contents:
· Create a GCP Compute Engine VM Instance ∘ Step 1: Go to https://console.cloud.google.com/compute/instances ∘ Step 2: Click on VM Instances -> CREATE INSTANCE ∘ Step 3: Scroll down and click on Advanced options -> Networking · Create VPC firewall rule ∘ Step 1: Go https://console.cloud.google.com/networking/firewalls/ ∘ Step 2: Select the Create Firewall Rule option. ∘ Step 3: Scroll and Input few more fields · Install and test Locust ∘ Step 1: Install Locust ∘ Step 2: Create a Locust test file ∘ Step 3: Run Locust ∘ Step 4: Running your 1st load test on locust
Create a GCP Compute Engine VM Instance
Step 1: Go to https://console.cloud.google.com/compute/instances
This will take you to Compute Engine
Step 2: Click on VM Instances -> CREATE INSTANCE
Note: In some of the fields, you can give any parameter of your choice, but if you are following along, please stick to the one that I have used to avoid errors.
You can modify these three fields:
- Name: locust-test-vm
- Region: asia-south1 (Mumbai), Zone: asia-south1-c
- Machine configuration: Series(E2), Machine Type(e2-standard-4)
Step 3: Scroll down and click on Advanced options -> Networking
Under networking, input network tags as allow-8089
. We will create a firewall rule for this network tag in a later part of the blog. The rule will allow outside traffic from the internet to this VM instance through port 8089. Note that the locust app runs on port 8089 by default.
Scroll down and click on create. Your VM instance will be up and running in some time.
Create VPC firewall rule
Step 1: Go https://console.cloud.google.com/networking/firewalls/
This will take you to VPC Network -> Firewall
Step 2: Select the Create Firewall Rule option.
Input Name as http-allow-8089
.
Step 3: Scroll and Input few more fields
Input these fields:
- Target Tags:
allow-8089
, It is the same value that we gave while creating the VM instance. - Source IPv4 ranges:
0.0.0.0/0
- TCP: Ports(
8089
)
Click on create, and your firewall rules will be ready in some time.
Install and test Locust
Step 1: Install Locust
Locust requires Python 3.6 or higher. You can install Locust using pip:
pip install locust
Step 2: Create a Locust test file
Create a new Python file (e.g., locustfile.py
) and add the following code to define a simple test scenario for a website:
from locust import HttpUser, task, between
class WebsiteUser(HttpUser):
wait_time = between(1, 2) # Users will wait between 1 and 2 seconds before executing a new task
@task
def load_main_page(self):
self.client.get("/") # Simulate a user loading the main page
Step 3: Run Locust
On the terminal, navigate to the directory containing your locustfile.py
, and run the following command:
locust
This will start the Locust web interface on http://localhost:8
089
Open the URL in any web browser.
Step 4: Running your 1st load test on locust
One of the prerequisites to running Locust is actually having an endpoint to test. You can use this guide to quickly set up a fast API service on a GCP VM or on your local machine.
If you want to avoid this hassle, you can use any publicly available endpoint as well, but please don’t generate too much QPS. In this example, we will use: https://gorest.co.in/public/v2/us
ers
Here are the test stats while the load test is running.
Conclusion
Congratulations on finishing this step-by-step guide to load testing API endpoints with Locust on a GCP VM! This tutorial should help you confidently load-test with Locust and GCP. These steps created a GCP Compute Engine VM instance, set up VPC firewall rules, and installed and tested Locust on your VM. You can now scale and optimize API endpoints with this knowledge. Test well!
Towards Data Engineering
Thanks for being a part of our community! Before you go:
- 👏 Clap for the story and follow the author 👉
- 📰 View more content in the Towards Data Engineering
- 👌 Data Engineering interview questions ⇒ View Course
- 🔔 Catch me on: Twitter | LinkedIn