avatarAman Ranjan Verma

Summary

The provided content is a comprehensive tutorial on setting up and running Locust, an open-source load testing tool, on a Google Cloud Platform (GCP) virtual machine (VM) to test API endpoints effectively.

Abstract

The article "Mastering Load Testing with Locust on a GCP VM: A Step-by-Step Guide" offers a beginner-friendly guide to installing and configuring Locust on a GCP VM. It outlines the benefits of using a VM for load testing, such as avoiding the impact of unstable internet connections and network penalties on test results. The tutorial includes detailed instructions on creating a GCP VM instance, setting up VPC firewall rules to allow traffic on the default Locust port (8089), and installing Locust via pip. It also provides a sample Locust test file and explains how to run and interpret the results of a load test using the Locust web interface. The guide emphasizes best practices and aims to enable users to confidently perform load testing on their API endpoints, optimize performance, and scale their services effectively.

Opinions

  • The author suggests that running Locust on a GCP VM is superior to running it on a local machine, especially when the service being tested is geographically distant from the tester's location.
  • There is an emphasis on the importance of stable network conditions for accurate load testing, as unstable connections can skew latency statistics.
  • The tutorial is designed to be accessible to beginners, providing step-by-step instructions and code examples to facilitate understanding and implementation.
  • The author advises following the provided parameters and steps closely to avoid errors during the setup process.
  • The guide promotes the use of publicly available endpoints for testing purposes, with a reminder to be considerate and not generate excessive query-per-second (QPS) rates.
  • The conclusion celebrates the reader's completion of the guide and encourages them to apply their new knowledge to scale and optimize their API end

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.

ARV Creation

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 InstanceStep 1: Go to https://console.cloud.google.com/compute/instancesStep 2: Click on VM Instances -> CREATE INSTANCEStep 3: Scroll down and click on Advanced options -> Networking · Create VPC firewall ruleStep 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 LocustStep 1: Install LocustStep 2: Create a Locust test fileStep 3: Run LocustStep 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

GCP Screenshot

Step 2: Click on VM Instances -> CREATE INSTANCE

GCP Screenshot

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

GCP Screenshot

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

GCP Screenshot

Step 2: Select the Create Firewall Rule option.

GCP Screenshots

Input Name as http-allow-8089.

Step 3: Scroll and Input few more fields

GCP Screenshots

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:8089

Terminal ScreenShot

Open the URL in any web browser.

Locust Screen

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/users

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:

Python
Locust
Load Testing
Google Cloud Platform
Google
Recommended from ReadMedium