JMeter- Need a bit more Freedom? Here you go LOCUST!

Apache JMeter is a great tool and it’s very popular.

In my perspective, JMeter may have certain limitations in terms of providing complete freedom for load testing. Now, let’s explore how Locust compares in this aspect. In this article, we will make a direct comparison between JMeter and Locust to understand their differences and capabilities.

Let’s take a closer look at the comparison between JMeter and Locust.

JMeter is a widely-used load testing tool known for its versatility in testing the performance of web applications and services. It offers a comprehensive set of features, but some users may encounter constraints and a steeper learning curve due to its complex user interface and configuration.

In contrast, Locust is a modern, Python-based load-testing tool that prioritizes simplicity and ease of use. It allows users to write test scenarios as code, which proves beneficial for developers who prefer more control and flexibility in their tests. Due to its straightforward and intuitive approach, Locust has gained popularity among developers and QA teams.

While JMeter boasts a broad range of plugins and built-in functionalities for extensibility and customization, Locust’s code-based approach offers developers greater flexibility in customizing test scenarios and integrating with other tools and libraries.

Ultimately, the choice between JMeter and Locust will depend on the specific needs and preferences of the testing team, as well as the nature of the load testing scenarios they wish to conduct.

What are the Limitations?

Script programming is not allowed in JMeter. Furthermore, there are limitations in executing a large number of services and APIs on a single machine. To use the Auto-correlation plugin effectively, it is highly recommended to run a load test in non-GUI mode and ensure that all instances of JMeter are closed during the test. Additionally, copying elements, like a Regex Extractor, between different versions of JMeter may result in an error.

Here is the Magic in Locust:

Write user test scenarios in plain-old Python

No need for clunky UIs or bloated XML—just code as you normally would. Based on coroutines instead of callbacks, your code looks and behaves like normal, blocking Python code.

Distributed & Scalable – supports hundreds of thousands of users

Locust supports running load tests distributed over multiple machines. Being event-based, even one Locust node can handle thousands of users in a single process. Part of the reason behind this is that even if you simulate that many users, not all are actively hitting your system. Often, users are idle in figuring out what to do next. Requests per second != the number of users online.

Web-based UI 

It has a neat HTML+JS user interface that shows relevant test details in real time. And since the UI is web-based, it’s cross-platform and easily extendable.

Hackable 

Locust is small and very hackable and we intend to keep it that way. All heavy lifting of evented I/O and coroutines are delegated to the event. The brittleness of alternative testing tools was the reason we created it.

An interesting solution made from it:

Locust has consistently demonstrated its remarkable capability to effectively simulate millions of simultaneous users, making it an exceptionally reliable choice for load testing. Furthermore, Battlelog, the web app for Battlefield games, notably relies on Locust for its load testing, thus providing concrete evidence of its battle-tested performance and reliability.

Let’s try this:

Step one: Locust is available on PyPI and can be installed with pip.

for python 2.7

$ python -m pip install locustio

for Python 3:

python3 -m pip install locustio

If you want the bleeding edge version, you can use pip to install it directly from our Git repository. For example, to install the master branch using Python 3:

$ python3 -m pip install -e git://github.com/locustio/locust.git@master#egg=locustioL

to make sure you got in

locust --help

Let’s do simple login function for your easiness

Assignment: Login function via Rest Services

from locust import HttpLocust, TaskSet, task 
from locust import clients
from locust.clients import HttpSession 
import requests
global BaseUrl 
class auth():
    
    BaseUrl = 'https://[Your-server]-api.domain.co'
    #Auth API's Definitions
    def login( self,userName,passWord):
        s = HttpSession('https://[Your-server]-api.domain.co')
        response = s.post('https://[Your-server]-api.domain.co/auth/login', {'username':userName, 'password':passWord})
        return response

    def getLogUser( self,accress_token):
        s = HttpSession('https://[Your-server]-api.domain.co')
        response = s.get('https://[Your-server]-api.domain.co/profile', headers={'access_token':accress_token})
        return response



These two services as you see

-Login request(Post) with the parameter Username and password

-Request(Get) to get the profile information of the user

What we need next. send the parameter to this API and create some load 🙂

First, we need external data handling for the credentials. In my case, I kept.CSV file for storing different credentials of the users. Let’s try to read and push the data to APIs

class runnerAgent(TaskSet):
    global json_data
    global accress_token
    global path
    global ccLogin
    global API_KEY
    global getAr
    global VarCD
   
    @task
    def Login(self):
        #Object from global file 
        conf = config.config()
        path = conf.getLocation()
        #object from Admin Auth
        login = authAgent.authAgent()
        PWConbv = util.util()
        #radis Cache Initialization
        Rcache = redis.Redis(host='127.0.0.1', port=6379, db=0)
        with open('//oadTestv0.1/globe/credentials.csv') as csv_file:
            csv_reader = csv.reader(csv_file, delimiter=',')
            for row in csv_reader:
                print(' Login Agent with Username--->'+row[0]+' Who has Password --->'+row[1])
                #login Request 
                Loginresponse=login.login(row[0],row[1])
                print Loginresponse
       --> (*)  assert Loginresponse.status_code is 200, 'Unexpected response code: ' + Loginresponse.status_code
                json_data =json.loads(Loginresponse.text)
                accress_token =json_data['content']['access_token'] 
                ccLogin =json_data ['content']['ccLogin']
                agentID=json_data['content']['id']
                print agentID
                ObjectAgent =json.dumps({ 'UserName':row[0],'PassWord':row[1],'ccLogin':ccLogin,'id':agentID,'accress_token':accress_token})
                Rcache.set(row[0],ObjectAgent)
                API_KEY = conf.getAPI_key()
                print API_KEY
                if Loginresponse.status_code==200:
                    login.AutoLog(accress_token) 
                else:
                    login.login(row[0],EncriptedPW)
    
class LoginWithUniqueUsersTest(HttpLocust):
    task_set = runnerAgent


For the initial try let’s not bother about the #radis cache or other JSON readings. Just try to assert response code — > (*)

Run the test: There are two methods you can run the test

-With GUI

$ locust -f locust_files/my_locust_file.py --host=https://example.com

You will the running port in your local machine :

Once you’ve started Locust using one of the above command lines, you should open up a browser and point it to http://127.0.0.1:8089 (if you are running Locust locally), and at the same time, the greetings will be something like this:

In the context of load testing, a locust class represents an individual user or, figuratively, a swarming locust. Locust spawns or hatches one instance of the locust class for each user.

During the load testing process, the locust class plays an important role in effectively characterizing each user and their behavior. To achieve this, the locust class should define several attributes that accurately represent and simulate the users’ actions and interactions with the system.

You can check the real-time statistics too.

JMeter vs Locust

-Non-GUI

locust -f my_locust_file.py  --clients=02 --hatch-rate=01   --host=http://example.com  --no-web 

The console will print the summary for you.

 

JMeter vs Locust