Run Locust Standalone on Cloud Run

Ali Altoğ
ÇSTech
Published in
4 min readJul 17, 2023

Hi Everyone,

Load testing is a crucial step in ensuring the performance and scalability of applications. In this tutorial, we will walk through the process of running Locust, a popular open-source load testing tool, on Google Cloud Run. Cloud Run offers a serverless platform for running containerized applications, providing scalability, cost-effectiveness, and simplified deployment. By the end of this tutorial, you can set up Locust on Cloud Run and easily perform load tests.

Prerequisites:

  • A Google Cloud Platform (GCP) account
  • Basic familiarity with GCP services and concepts
  • Docker installed on your local machine

Step 1: Set up a Google Cloud Platform (GCP) project and enable Cloud Run

  • Create a new GCP project or use an existing one.
  • Enable the Cloud Run API for your project.
  • Install and set up the Google Cloud SDK on your local machine.

Step 2: Create a Locust test script

  • Install Locust on your local machine using pip: pip install locust.
  • Create a new Python file, e.g., locustfile.py, and define your load test script using Locust's syntax. Refer to the Locust documentation for writing custom test scenarios.

Step 3: Build a Docker container for Locust

  • Create a new file named Dockerfile in the same directory as your Locust test script.
  • Use the following contents for the Dockerfile:
FROM python:3.9-slim

COPY locustfile.py .

RUN pip install locust

EXPOSE 8089

CMD ["locust", "--host=http://your-target-app.com"]

Step 4: Deploy the Locust container to Cloud Run

  • Build the Docker container by running the following command in the terminal:
docker build -t locust-container .

Push the container image to Google Container Registry:

docker tag locust-container gcr.io/[PROJECT-ID]/locust-container
docker push gcr.io/[PROJECT-ID]/locust-container

Deploy the container to Cloud Run:

  1. Select the Docker image that you built and push it to your registry.
  2. Give a name to your service.
  3. Select your Region.
  4. Select the allocation type you want. (Check pricing)
  5. Set the number of min and max instances. (Set to 1 to reduce cold starts)

6. Select the ingress control option.

7. Select the authentication type for security.

8. Set container port.

9. Set the resources that the container uses for allocation.

10. Select execution env.

11. Set the env variables for locust mode and custom locustfile.py location.

12. On networking configuration you can select your private VPC or HTTP setting.

13. On the Security tab, you can create a sa with permission to use Cloud Run or use one of the existing ones.

Now you can review the settings and click “Create” to deploy the Locust service to Cloud Run.

  • Once the deployment is complete, navigate to the Cloud Run service URL provided in the output.
  • Open the URL in your web browser to access the Locust web interface.
  • Specify the desired number of users and hatch rate in the Locust interface.
  • Click the “Start swarming” button to initiate the load test.
  • Monitor the test progress and view the results in real time.

Conclusion: Congratulations! You have successfully set up and deployed Locust Standalone on Cloud Run using the GCP UI. Now you can easily perform load tests and gain insights into your application’s performance and scalability. Cloud Run’s serverless platform eliminates the need for infrastructure management, allowing you to focus on load testing effortlessly.

Feel free to explore additional features and configuration options in Locust to further enhance your load-testing capabilities. Happy load testing!

Sources:

--

--