Send Us a Message
Contact Information
Skizzle Technolabs India Pvt. Ltd.
Noel Focus, Kakkanad,
Kerala, India – 682021
Locust is an open-source load testing tool written in Python. It lets you write tests against your web application that mimic your user’s behavior, and then run the tests at scale to help find bottlenecks or other performance issues.
Any testing is a complex task that requires planning, preparation, performance control, and results analysis. For performance testing, all the data that can affect the outcome must be gathered, if possible.
The easiest way to install Locust is from PyPI, using pip:
$ pip3 install locust
Validate your installation and show the Locust version number:
$ locust -V
In Locust, your user behavior can be defined in Python code. Simply use the locust command and (optionally) its web interface to spawn and simulate a number of those users while gathering request statistics.
One of the nicest features of Locust is that configuration is done via “Plain Old Python.” Simply, you can create a file named locustfile.py and all configuration for your performance tester and its tests is done there.
Let’s look an example locustfile.py, which explains a simple user behavior which consists of a single “task” which gets a particular webpage:
from locust import HttpLocust, TaskSet, task
class UserBehavior(TaskSet):
@task
def get_something(self):
self.client.get(“/something”)
class WebsiteUser(HttpLocust):
task_set = UserBehavior
A second task can be added as follows:
class UserBehavior (TaskSet):
@task
def get_something (self):
self.client.get (“/something”)
@task
def get_something_else (self):
self.client.get (“/something-else”)
Locust will randomly choose between each of the tasks and run them When the above UserBehavior is run. If you want to weigh different tasks differently so that one is run twice as much as the other, you can add weighting as follows:
class UserBehavior (TaskSet):
@task(2)
def get_something (self):
self.client.get (“/something”)
@task(1)
def get_something_else (self):
self.client.get (“/something-else”)
These task weights are ratios across all defined tasks, so here get_something would happen twice as often during the test.
You can also write tasks that compose other tasks, to perform a sequential or serial set of tasks in a specific order. This lets you define a user flow through multiple requests. For example:
class UserBehavior (TaskSet):
@task
def get_something (self):
self.client.get (“/something”)
@task
def get_something_else (self):
self.client.get (“/something-else”)
@task
def get_two_things (self):
self.get_something ()
self.get_something_else ()
A TaskSet class can optionally declare an on_start function, which is called when a simulated user starts executing that TaskSet class. This can be used to log in or apply credentials once before beginning the performance test:
class UserBehavior (TaskSet):
def on_start (self):
self.client.post(“/login”, {
‘username’: ‘foo’, ‘password’: ‘bar’
})
@task
def get_something (self):
self.client.get (“/something”)
To run Locust locally, you run the locust command in the same directory as your locustfile.py:
$ locust –host=http://localhost:5000
Once the command is run, Locust starts up a local webserver which you can visit in your browser:
(Source: locust.io)
Fill out the form and try it out! (but note that if you don’t change your locust file to match your actual target system you’ll mostly get error responses)
After selecting the number of users and the spawn rate, you can begin the test, which will show you a live view of the running test:
(Source: locust.io)
Once a single machine isn’t enough to simulate the number of users that you need, Locust supports running tests distributed across multiple machines.
Running locally is fine for basic testing and getting started with Locust, but if you’re just running it from your local machine, most applications will not receive a significant load. It’s almost always advisable to run it in distributed mode. This is easy to do with a couple of AWS nodes.
After installing Locust and moving your locustfile.py to all nodes, you can start the “master” node:
$ locust –host=http://localhost:5000 –master
Then start any “slave” nodes, giving them a reference to the master node:
$ locust –host=http://localhost:5000 –slave\
–master-host=192.168.10.100
Locust has been created to solve certain specific issues of existing performance tools and it has done a great job. You can write performance scripts pretty quick, store them within your Python project, spend minimal time on maintenance without additional GUI applications and simulate thousands of test users even on very slow machines with this framework and some Python experience.
If your codebase is Python, it’s a shoe-in for the best tool you can be used, due to the opportunity to pull in data, models, or domain logic from your existing code base, but even if you’re not using Python, you can easily integrate it. Put your code to the test!
Looking for software testing help? Contact Skizzle and our expert team will be happy to assist you!
High-performing teams are the distinctive feature of an industry-leading business. This can ensure the business's success in the long run. Working in a team...
Read MoreSince the pandemic hit the world, the new normal is working from home. Gen Y and Gen Z are the largest workforce units of...
Read MoreYou have much to consider when looking for a solution for human resources (HR) technology. What is the right fit for you? How are...
Read MoreAs we have seen human resources and information technology have become more intertwined during the last decade. Manufacturing, financials, sales and marketing, and supply...
Read MoreSkizzle Technolabs India Pvt. Ltd.
Noel Focus, Kakkanad,
Kerala, India – 682021