This week at work I was tasked with continuing some load testing that a previous Engineer had started. They had used locust which is an open source load testing tool to run the initial load testing on the staging
environment. I now needed to do the same for production
so I followed in their footsteps.
First I installed locust
using homebrew
brew install locust
locust --version
Next, I asked ChatGPT to give me a basic test that worked with basic http authentication and let me send GET variables to make a GraphQL request to Magento 2.
from locust import HttpUser, TaskSet, task, between
from requests.auth import HTTPBasicAuth
class WebsiteTasks(TaskSet):
# def on_start(self):
# """ on_start is called when a Locust user starts before any task is scheduled """
# self.client.auth = HTTPBasicAuth('username', 'password')
# @task(1)
# def index(self):
# self.client.get("/")
@task(1)
def graphql_query(self):
query = "/graphql?query=query+getCurrencyData%7Bcurrency%7Bdefault_display_currency_code+available_currency_codes+__typename%7D%7D&operationName=getCurrencyData&variables=%7B%7D"
self.client.get(query)
class WebsiteUser(HttpUser):
tasks = [WebsiteTasks]
wait_time = between(5, 15)
To get a baseline I started things off by scaling down to a single instance for the api endpoint and tested in 50 user increments up to 200 users.
Next, I enabled autoscaling and started to increase the number of users until the alarm to scale up was triggered increasing the number of instances from 2 to 4. I kept increasing users until I scaled up to 5 total instances.
I had a pretty basic use case for this tool but if you'd like to see how it can be used to do more in-depth testing checkout this article
I also found a tool to convert HAR files into locust test files. This would make it easy to record a user session and replay it with load testing.
https://github.com/SvenskaSpel/har2locust
This was definitely the easiest load testing tool I've used to get up and running out of the box.
Top comments (0)