Performance tests are designed to check the ability of server, database and application on how it performs under load. Performance testing is often associated with the concepts of Load Testing, Stress Testing, or Endurance Testing. In fact, these are not terms that can be used interchangeably and Performance testing should be treated as a higher-level category, containing all the aforementioned types of tests.
Now come to our discussion about Locust Vs JMeter,
Locust is a distributed, load testing tool predominantly used for the web application, it’s as simple as we say. The behavior of each locust is defined by us and the swarming process is monitored from a web UI in real-time.
Here comes the Question,
Why do we need to look into this new tool when there are lots of well-established tools available in the market for decades like JMeter, LoadRunner, ApacheBench, Gatling, Tsung, etc…
Here is why we should give a try for this new tool,
* Allows a user to write code in plain python.
* Supports distributed user.
* Supports default HTML + JS based reporting.
* Open to test other systems by writing clients.
* Supports user to write code in other languages using locust slave, if needed.
* Open source under the MIT license.
* 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, through gevent.
* Greenlet allows a user to write an expressive scenario in Python without complicating your code with callbacks.
Comparison of Locust with Jmeter:
When we talk about performance testing, the tool which comes into everyone’s mind is JMeter, including myself. But when we dive deep here are the drawbacks it has:
* Point-and-click interface, a graphical interface to point and execute the click events.
* JMeter is thread bounded, internally it means each user we stimulate needs a separate thread, benchmarking thousands of user in a single machine is not feasible.
Locust is created to address these pain points in load testing.
The execution flow starts with the main function of the python file(main.py). Locust class is parsed and associated weight tasks(functions) are parsed and stored. Locust browser client works with python standard library urllib2 to manage the HTTP requests. The “browser” that is used by the simulated users is defined in clients.py. When a request is executed the time until a response has been received is recorded. Locust is implemented using events. When a request is finished, an event is fired to allow the stats module to store a response time or possibly a failure. Each simulated users run in Greenlet threads. A greenlet is actually not a real thread but it makes sense to think of it as such.
How to install Locust:
<strong>Pre-requisite to install Locust:</strong>
This configuration setup is for mac,
1. Install Brew on mac, paste the below command on the terminal to get brew installed.
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
2. Install python 2 or 3 version if it's not installed. Command to try: brew install python3
3. To install get-pip.py, use the following command:
sudo python get-pip.py
4. Once the above package is downloaded, run this command,
sudo python get-pip.py
<strong>for Python 2.7:</strong>
python -m pip install locustio
<strong>for Python 3:</strong>
python3 -m pip install locustio
Once locust is installed locust commands will be available in the shell.
For other OS, installation steps can be found here: https://docs.locust.io/en/stable/installation.html
Writing a locust script:
It’s a plain python file. Only thing is, it should have one class which inherits from the class locust.
Sample code snippet:
Below is a code for a very basic locust-file (a python-file) to load test towards locust’s own web
server. It sends GET request to the index page and a URL for fetching updates of the
Triggering the Locust file:
<em>from locust import HttpLocust, TaskSet, task</em>
<em> # one can specify tasks like this</em>
<em> tasks = [index, stats]</em>
<em> # but it might be convenient to use the @task decorator</em>
<em> def page404(self):</em>
<em> Locust user class that does requests to the locust web server running on localhost</em>
<em> host = "http://127.0.0.1:8089"</em>
<em> min_wait = 2000</em>
<em> max_wait = 5000</em>
<em> task_set = UserTasks</em>
Now start the Locust service with our locust file as mentioned below through terminal:
$ locust -f <locustfile> –web –host http://127.0.0.1:8089
In the next step, you can run the graphical interface in the browser, which is located in http://127.0.0.1:8089.
The interface will open a page with two inputs. In the first of them, “Number of users to simulate”, we have to enter the number of users (e.g. 10.0). in the second, “Hatch rate”, you specify the number of users that will be created in a second (e.g. 1). When running a test with given values, you will see the test statistic.
Workaround for dynamic user allocation:
Dis-advantage in both Jmeter and Locust is not having a direct option for the user to dynamically change the number of users, In Jmeter this can be achieved through BeanShell and in Locust, this is a one-time process of editing the core.py to get the UI option to do the dynamic user allocation option.
QUICK REFERENCE TABLE:
|Open Source/License||Yes. MID Licensed||Yes. Apache 2.0 Licensed|
|Supported Protocols||Primarily HTTP and supports everything using a custom script.||HTTP,JDBC, FTP, LDAP, SMTP and more.|
|Support of “Test as Code”||Strong(Python)||Moderate(Java)|
|Concurrent Users||Efficient and powerful||Moderate|
|Resources Consumption||Less (1X)||More(GUI – 30X Non-GUI – 14X)|
|Ramp-up Flexibility||Customizable using code||Yes.|
|Easy to Use VCS||Yes.||No|
So, which one you should choose?In summary, the choice between JMeter and Locust is definitely not easy. Although both tools are open source, behind the JMeter is the giant in the form of The Apache Software Foundation. On the other hand, Locust is being developed by a small team of developers and the community centered around the tool. Jmeter is written in Java, Groovy and BeanShell, but Locust is written in Python. Locust is an apt tool to measure the load test in real time Big data flow application to avoid peak time application down scenario. This works better where the Dev-QA combined load testing happens to measure the real-time load balancing before moving the product/software to production.