DEV Community

AJ Tarachanowicz II
AJ Tarachanowicz II

Posted on

Load Testing with Locust

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
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

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

https://learnosity.com/edtech-blog/how-we-manipulated-locust-to-test-system-performance-under-pressure/#:~:text=Locust%20is%20an%20open%2Dsource,(TaskSets)%20attached%20to%20them.

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)