Skip to content
Akash LM edited this page Apr 26, 2022 · 5 revisions

Locust is an open source load testing tool. The target of locust is load-testing web sites and checking number of concurrent users a system can handle.

Distributed and Scalable : Run load tests distributed over multiple machines Suitable for testing highly concurrent workloads

Web UI : Locust has web interface that shows the progress of the test in real time. Can change the load while the test is running.

Can test any system: Locust primarily designed for http web-based testing . Supports multiple protocols,the deault protocol http. Locust can be used to test any system or protocol.

Language :

Test scenarios can be written in Python.

Installation : Install Python 3.6 or later Locust can be installed using pip: pip install locust

When run a performance script locust automatically starts a server on http://localhost:8089 with a web interface. Locust can also visualize the results as charts.

Comparing with other tools :

*In compare with Jmeter , Locust monitoring doesn’t take up so many of your machines resources.That why Locust has a great benefit over Jmeter. If you strong with Python or prefer coding over UI tests creation, then you can go with Locust . *In compare to K6, locust scripts are written in python code,distributed and figuring out how many concurrent users a system can handle. *When it comes to other tools ,need to spend time to setup the distributed mode.But in Locust,it is easy to setup,just spinup the master and mutiple worker nodes and you can initiate your tests on multiple systems.

Getting Started:

Locust test is a Python program. Save the code in a filenamed locustfile.py in your current directory and run locust. Once you started the locust it opens web interface.

Using the web UI is optional, can supply the load parameters on command line and get reports on the results in text form.

Running without the web UI : locust -f locust_files/locust_file.py --headless -u 1000 -r 100

                 ( --headless in command indicates that we would disable the web UI )

-u specifies the number of Users to spawn, and -r specifies the spawn rate (number of users to start per second).

If you want to specify the run time for a test, you can do that with --run-time or -t

example:

locust -f --headless -u 1000 -r 100 --run-time 1h30m

Running Distributed :

Once your application gets bigger, so do your test. After a while, a single machine can’t simulate the number of users you expect using your app. For that reason, we need to distribute load tests across multiple machines.

//Assume that you have your test script. You can start Locust on master node with this command

  locust -f locustfile.py --master

//Then to start worker nodes using this

 locust -f locustfile.py --worker –master-host=master ip

Locust in Docker :

 docker run -p 8089:8089 -v $PWD:/mnt/locust locustio/locust -f /mnt/locust/locustfile.py

This command will run the locust as docker container by mapping the port 8089 to container port 8089 and going to map the volume present working directory to the container volume mount locust. Next,going to pull the image locustio/locust and going to run the locustfile.py-this should be present in the working directory. When we run this command,it gives the Container Id and it will open the port 8089 and we can interact with Web UI.

Distributed Load testing in Docker : For Distributing load testing in docker need to use docker compose.To run multiple workers you can use this command, example : docker-compose up --scale worker=4

Running in Kubernetes :

Locust supports running load tests on multiple machines.

Running Locust in distributed mode :

  • can have one master and multiple worker nodes
  • need to tell the workers where the master is (give them the IP address and ports) *need to supply each worker with the tests code (locustfiles) *the workers need to be able to connect to the master.

The easiest way to run Locust on Kubernetes is to use a Helm chart

Testing non-http systems :

Locust only comes with built-in support for HTTP/HTTPS but it can be extended to test almost any system like XML-RPC, gRPC. Locust also support DB connection. Can write a custom client class that uses whatever protocol you want: http://docs.locust.io/en/latest/testing-other-systems.html

Locust Monitoring with Grafana :

Load tests with locust, export metrics instantly with Prometheus, and show the metrics we want in Grafana.

     https://medium.com/devopsturkiye/locust-real-time-monitoring-with-grafana-66654bb4b32
Clone this wiki locally