Locust - load test using python code

Locust - load test using python code


What is locust?

An open source load-testing tool written in Python. It lets you write tests against your web application which mimic your user’s behavior, and then run the tests at scale to help find bottlenecks or other performance issues.

 It is intended for load-testing web sites and figuring out how many concurrent users a system can handle.

The idea is that during a test, a swarm of locusts will attack your website. The behavior of each locust ( test user) is defined by you and the swarming process is monitored from a web UI in real-time. This will help you battle test and identify bottlenecks in your code before letting real users in.

Locust is completely event-based, and therefore it’s possible to support thousands of concurrent users on a single machine. In contrast to many other event-based apps it doesn’t use callbacks. Instead it uses light-weight processes. Each locust swarming your site is actually running inside its own process. This allows you to write very expressive scenarios in Python without complicating your code with callbacks.

Distributed load testing

The one important feature which locust boasts of is the ability to distribute the tests on multiple systems

Is an extensible opensource testing tool which provides real time testing statistics while performing tests on an API.

examples of API's : Web API , RPC , XML API’s, SOAP API

Simultaneous users simulation

What is a 'simultaneous user' ?

simultaneous users are active users performing the same activity at a given point in time.

Simultaneous vs Concurrent

  • The users using the system irrespective of the activities they are doing are called as 'Concurrent Users'.
  • The simultaneous users can be concurrent users, but concurrent users cannot be simultaneous users.
  • The number of concurrent users on an application is more than the number of simultaneous users.

Install Locust

Locust runs in a Python environment so you need to setup Python and its package install tools, if you don’t already have them. Thankfully OS X and most Linux versions come with Python installed.

To verify that you have Python installed, open a terminal window and run the following command:

$ python --version

Hopefully you see a result like this:

Python 3.3.0

To run Locust you will need either Python 2.7.x or any version of Python 3 above 3.3. If you do not have a Python runtime installed please visit the Python site.

Once you have Python installed, we will install several packages from pypi. To do this, Python leverages Pip to install packages locally. To check if we have Pip installed open a terminal window and type

$ pip --version

Run this command to install Locust. (Alternative methods here):

$ pip install locustio

Now that we have Locust installed we can create and run a Locust script. But first, we need a server to hit.

For this example we can use the example provided by Locust in their quick start documentation.

Your locustfile.py should contain the following:

from locust import HttpLocust, TaskSet, task
class UserBehavior(TaskSet):def on_start(self):""" on_start is called when a Locust start before 
            any task is scheduled
        """
        self.login()
    def login(self):
        self.client.post("/login",
                         {"username":"john",
                          "password":"testuser123"})
    @task(2)def index(self):
        self.client.get("/")
    @task(1)def profile(self):
        self.client.get("/profile")
class WebsiteUser(HttpLocust):
    task_set = UserBehavior
    min_wait = 5000
    max_wait = 9000

Now that we have our locustfile we can do a test.

First ensure your example server is running:

$ ./example_serverrunningat8080

Now, in a new terminal we will run Locust. We will pass it the name of our test file, locustfile.py, and tell it to run against our example server on port 8080 of localhost.

$ locust -f locustfile.py --host=https://localhost:8080

With Locust running we can open the web user interface at: https://localhost:8089.

For this test, we will simulate 1 user and specify 1 for the hatch rate. Click the Start swarming button. You should now see something similar to the following in the terminal running example server:

2017/09/13 15:24:33 Login Request
2017/09/13 15:24:33 Profile Request
2017/09/13 15:24:40 Profile Request
2017/09/13 15:24:46 Index Request
2017/09/13 15:24:55 Index Request
2017/09/13 15:25:00 Index Request
2017/09/13 15:25:07 Profile Request
2017/09/13 15:25:12 Index Request

In the Locust UI you will see a list of the endpoints being hit. You will see the request counts incrementing for / and /profile. There should be no failures unless Locust is having issues connecting to your server.

Locust’s web interface

Once you’ve started Locust using one of the above command lines, you should open up a browser and point it to https://127.0.0.1:8089 (if you are running Locust locally). Then you should be greeted with something like this:


Click on start swarming and you would be able to view the results and detailed reports of the load tests as given below


Advantages of Locust

Despite being web-based, Locust can test almost any system if you write a client for it. Among the main benefits of Locust are concurrency, scalability,maintainability and customization.

It is very useful in finding the server's issues, like resource exhausted, memory leak, etc.




要查看或添加评论,请登录

社区洞察

其他会员也浏览了