Javatpoint Logo
Javatpoint Logo

Python Locust Module

Python Locust: Unlike JMeter and Tsung, Python-based option for load testing your server.

Tired of the current devices for benchmarking and load testing a current server, I, as of late, found an open-source apparatus called Locust to use in one of my undertakings.

By perusing this tutorial, you'll have the option to utilize this heap testing device. This furnishes you with a harsh thought of how many demands each second is (RPS) upheld by your server.

Locust is a simple-to-utilize, circulated, client load testing device. It is expected to load-test sites (or different frameworks) and sort out the number of simultaneous clients a framework can deal with.

Why is this module named Locust?

The thought is that many locusts (grasshoppers) will go after your site(or website) during a test. The way of behaving of every Locust (grasshopper or test client, maybe) is characterized by you, and the amassing system is continuously checked from a web UI. This will assist you with combating tests and recognizing bottlenecks in your code before giving genuine clients access.

Locust is totally occasion based, and in this manner, supporting a great many simultaneous clients on a solitary machine is conceivable. As opposed to numerous other occasion-based applications, it doesn't utilize callbacks.

Rather it utilizes lightweight processes through events. Every Locust amassing your site is running inside its cycle (or greenlet, to be right). This allows you to compose extremely expressive situations in Python without confusing your code with callbacks.

Highlights or Features

1. Write client test situations in regular Python

Assuming you maintain that your clients should circle, play out some contingent way of behaving or do a few estimations, you utilize the normal programming builds given by Python. Grasshopper runs each client inside its greenlet (a lightweight cycle/coroutine). This empowers you to compose your tests like typical (hindering) Python code instead of utilizing callbacks or another component. Since your situations are "just python", you can utilize your standard IDE and adaptation control your tests as customary code (rather than a few different instruments that utilization XML or paired designs)

2. Distributed and Scalable - upholds a huge number of clients

Locust makes it simple to run load tests dispersed over different machines. It is occasion-based (utilizing gevent), which makes it workable for a solitary cycle to deal with a huge number of simultaneous clients. While there might be different instruments that can do more demands each second on given equipment, the low above of every Locust client makes it entirely reasonable for testing exceptionally simultaneous jobs.

3. Online UI (web user interface)

Locust has an easy-to-understand web interface that progressively shows your test's advancement. You might change the heap while the test is running. It can likewise run without the UI, making CI/CD testing simple.

4. Can test any framework

Even though Locust works with sites/administrations, testing practically any framework or protocol can be utilized. Compose a client for what you need to test or investigate some made by the local area.

5. Hackable

Locust's code base is deliberately kept little and doesn't tackle everything out of the crate. All things being equal, we attempt to make it simple to adjust to any circumstance you might go over, utilizing normal Python code. To send revealing information to that data set and diagramming framework you like, wrap calls to a REST API to deal with the points of interest of your framework or run a custom burden design; there isn't anything halting you!

Command Line Interface

The module gives us a method for running burden testing through the order line interface. This permits us to carry out a robotized load testing stream effectively. Add the no-web boundaries while running Locust. You must incorporate the host, number of clients, and lid rate.

Locust - f locustfile.py - - no-web - - have wing - c 1000 - r 100

  1. c - Number of clients
  2. f - Path to the document
  3. no-web - Run the recreation without a web interface
  4. r - Hatch rate

Installation Process Python Locust Module

With pip command, Locust is installed.

Command:

When the Locust is effectively installed, a command of Locust ought to be accessible in your shell.

To see more accessible choices:

Command:

Load Testing Using LOCUST Module

Locust is an open-source load testing device. Load testing is programming testing directed to look at the resilience/conduct of the framework under a particular anticipated load. The objective of the grasshopper is load-trying sites and checking the number of simultaneous clients a framework can deal with.

During a locust test, a lot of errors will go after the objective, i.e., site. The way of behaving of every Locust is configurable, and the amassing system is checked from a web UI continuously.

Source Code:

Explanation of the above code:

Firstly importing all the required modules. Then define the functions and classes.

The on_start method is called when a simulated user starts executing that TaskSet class. Admin login and retrieving its access token. The task_sets attribute should point to a class TaskSet which tells the status of the customer.

Begin Locust:

To run the above code, make a Python record named locustfiles.py, and open the terminal in the registry of the above-made document. Then compose the accompanying order in the terminal.

Note: By default, Locust looks for locustfiles.py.

After the effective execution of the above order, you ought to open a program and hit http://127.0.0.1:8089

The Locust UI will seem like beneath:

Output:

Python Locust Module

Summary

Locust is an exceptionally overall quite convenient device for stack testing regarding Python. Testing REST APIs is exceptionally basic. Yet, testing non-RESTful APIs may be somewhat more earnestly. We have shown you one method for testing non-RESTful APIs. There are different instruments like JMeter for load testing. JMeter has a high expectation to learn and adapt that you face each time it's expected to test something more chaotic than a "Hi World" application. In this way, what you ought to pick very relies upon you.

Gratitude for reading!!!


Next TopicPython Time Module





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA