Perform simple load test using Locust

Performance engineering is a very important aspect of developing a performant application. When you are developing any application, it is important that you measure performance of your application after every major release as well as when significant changes are made to an application's implementation. For a long period of time, I have used Microsoft Visual Studio web test and load test framework. Microsoft decided to retire the load test framework. Microsoft has asked its users to look for other solutions. Now, I have switched to using Locust as my preferred load testing framework.

In this post I will discuss how to write a very simple load test using Locust. Before I discuss any further, let me give you reference to website where you will find more information about Locust.

  • Locust documentation has some good information in its Quick Start page. I will recap some of that information here. At the heart of writing a load test using Locust is locustfile.py file. From the file extension, you know that Locust load test is written using Python language. In your load test file, you will need to create a file that derives from HttpUser. This class represents simulated user for your test. You are not limited to using only one user class. You can write as many classes derived from HtttpUser class. By having multiple classes, you can simulate behavior of multiple types of users. E.g. in your application you would want to run some tests for Anonymous users and some tests of Registered users. To simulate this you will have two classes. Inside this class you will write tests that must be executed.

    Following code snippet shows a very simple load test that loads home page of my website.

    import time
    import json
    import logging
    from locust import HttpUser, task, between
    
    class AnonymousUser(HttpUser):
        host = ""
        wait_time = between(1, 6.5)
    
        @task
        def home_page_view(self):
            logging.info(f'Viewing home page of {self.configuration["host"]}')
            self.client.get(self.configuration["host"])
            pass
    
        def __load_configuration(self):
            # Read configuration file
            try:
                with open("config.json", "r") as f:
                    self.configuration = json.loads(f.read())
            except FileNotFoundError:
                print("Configuration file not found!")
            else:
                pass
    
    
        def on_start(self):
            self.__load_configuration()
            host = self.configuration["host"]
            return super().on_start()
    
        def on_stop(self):
            return super().on_stop()
    

    There are few important points in this code that I will point out. You must provide host value for your load test. This host URL is base URL that will be available to you in client attribute of user class. You can provide value of host attribute in user class or on command line when you execute load test. In your test strategy, you would not want to hardcode this host URL. You will have this URL in some configuration file. I put configuration parameters of my load test in config.json file. I read that file in on_start function. This way I set host value at run time. If you do not specify host value in code or command line, your test will not run and throw exception letting you know that you have not specified host value. For sake of simplicity, I have shown reading of configuration file in main test file. In actual implementation, I have refactored this step in a separate Config class. In that class, default values of load test parameters are set when config file is missing or some parameters do not have valid values.

    Once your load test file ready, just run locust command from the folder where you have your locustfile.py file. Then follow the instructions from Quick Start page and bring up web interface to monitor the load test. It will look like below.

    Search

    Social

    Weather

    24.7 °C / 76.5 °F

    weather conditions Clouds

    Monthly Posts

    Blog Tags