Locust Performance Testing Using Java and Kotlin
If you have already put your hands on performance testing with Locust, then you should know it’s a really great code-based framework that provides outstanding capabilities for writing your tests in the Python scripting language. In one of our previous articles, which has a quick comparison of JMeter and Locust, we presented Locust as a wonderful tool for engineers who have some Python scripting experience.
Running Performance Tests in Locust with Custom Languages
But does that mean that this tool is not for you if you are not a Python fan and for some reason do not prefer this powerful scripting language? Surprisingly, it doesn't! We have great news for you! The Locust framework gives you the option to run its custom slaves on other programming languages! Basically, if you have programming experience, you can implement your own custom client to support the custom language you are using. It worth to mention that there are already two ready clients implementations that allow you to create Locust slaves using Golang and Java programming languages.
In this article, we will demonstrate how to use one of these clients for implementing your performance tests in the Locust framework. We will use the Java client but you can apply the same steps for Golang language as well, because the principles are the same. Moreover, we will show you how to use the same Java Locust library to create your tests using Kotlin. Kotlin has all the benefits provided by Java, but with an additional huge batch of benefits that you can learn from the Kotlin official site. As for me, I love both languages with a slight preference for the Kotlin. But as Kotlin and Java can be easily used in the same project, I thought it would be nice to have them both as examples.
The main idea behind the approach for using a custom programming language in Locust performance tests, is that you have the Locust master service running in Python and any number of slave nodes that can be run using any programming language. Each of the Locust slaves sends the metrics to the master, which aggregates the data and runs the Locust web application, which you can use to view the metrics:
Creating a Load Test in Java for Locust
First, let's start with a project that will contain the load tests written in Java programming language. In this article, our examples will be based on API HTTP requests against http://blazedemo.com web app. In order to make API calls we can use REST Assured, one of the best Java frameworks for API testing..
As we need to run written tests as Locust performance scripts, we also need to have the dependency for the Locust4j Java library, which creates all the magic for the Locust integration. To download and use the mentioned libraries in our project we will use Maven. All you need is to create a Maven project in your favorite IDE and use this pom.xml file:
The provided pom file contains REST Assured and Locust4j dependencies, plus it contains a configuration that allows creating a Java jar file with all the dependencies packed inside. This jar file is important in order to run the project as a Locust slave, without using the IDE environment later.
Running the Locust Master
As we explained above, we need to run Locust master if we want to use custom programming languages on Locust slaves. To run the Locust master, it is enough to create the simplest Locust script with a dummy task inside. Let’s create a Python file that we can place in the root of our test project and called it “locust-master.py”.
You can run the master service by this command:
This command says that we are going to run the script in master mode on the local machine and the 5557 port (you can use any another free port on the machine). As a result, you should something like this:
Now the Locust master is running and ready to get some slaves on board.
Creating the Main Class for the Locust Slave
Let’s create the main class that will be used to run the Locust Java slave. The code is very simple. You just need to create the Locust object and specify which host and port are being used to run the master service:
Inside the locust.run(...) function you need to provide the list of tests that you are going to run. Let’s run a small test that just opens the http://blazedemo.com application:
As you can see, tests implementation is also relatively simple. All you need to do is to extend the new class from the “AbstractTask” class located inside the Locust4j framework and override some of the functions:
- getWeight() - should return the weight of the task. This represents how often a specified task is going to be used. In other words, a task with weight 2 will run twice as often as a task with weight 1
- getName() - should return the name of the task that we will see in the Locust monitoring and report, to identify the task
- execute() - contains the body of the test that you should use to put your task logic inside
In addition, we made the custom class constructor for the task to accept the task weight as an argument (check this article to learn more about constructors). Using this approach, we can configure the weights of the implemented tasks from the main class, which contains all the tasks that are going to be executed, separated by commas.
If you need to create some assertions, you can put them inside the same execute() function that contains the main logic of the task. Let’s create one additional task that simulates a flights search between Paris and Buenos Aires. In the same task we will verify that the response code is 200, which means that request has been executed successfully:
As soon as our tasks are implemented, we can specify them inside the locust.run() function separated by a comma:
Running the Locust Load Test
After that you can run the Locust Java slave right from the IDE:
After the script has started, the master should show you the message:
What if you want to run the same slave but via the console? First, you need to create the jar file with all the dependencies packed inside. If you are following our steps, you should already have the pom.xml file with the correct configuration to create the jar file. Use this command:
After that, you can run the Locust Java slave using this command (assuming that your console is opened at the root of our test project):
Now, if you run the locust UI (http://localhost:8089), you should see that you have 1 slave:
Congratulations! You are running your first Locust script written in pure Java!
Creating a Load Test in Kotlin for Locust
But what about Kotlin? As for me, I personally love Kotlin as the main language for all kinds of testing activities. It has all the benefits provided by Java but at the same time, it is much faster to write the tests because of simplicity of syntax and different syntax sugar. We are not going to compare these two wonderful programming languages. We will provide you with some steps explaining how you can use Kotlin instead of Java to implement Locust performance scripts.
Installing the Kotlin Runner
First, you need to install the Kotlin runner to execute the Kotlin scripts. You can easily do this by using the brew package manager:
brew install kotlin
Adding Dependencies and Plugins
After that, you need to create the list of updates in our pom.xml to add the Kotlin dependencies and plugins that will be used to compile the Kotlin scripts:
Converting Tests from Java
If you use the Idea IDE programming environment, then in order to create the same tests in Kotlin, all you need is to create empty files with the same names. (It’s better to add a prefix like ‘Kotlin’ in order to differentiate the Kotlin tests from the existing Java tests). Then, just copy and paste the Java code inside accordingly. The conversion from Java to Kotlin will be done automatically. This is an example of the task that finds the flights between Paris and Buenos Aires, but written on Kotlin this time:
As soon as all tasks and the main runner function are converted (if you had trouble with the Java -> Kotlin conversion you can check the final classes by this link) you need to package the jar file one more time by using this command:
Running the Locust Load Test
After that, the Locust Kotlin slave can be run by this command:
As you can see, there is nothing difficult about using Locust with other programming languages. Powerful extensibility is one of the main distinctive features of the Locust framework. So, if you have some programming experience, you should be able to extend the Locust framework with any custom functionality, to achieve any kind of specific goals for your performance testing suite. Couldn’t find Locust integration with your favorite programming language? You can be the person who can creates your own and shares with a grateful Locust community.
As always, you can find the whole project based on provided snippets in the git repo through this link.
That’s it! You now know how to run your load tests in Locust with your custom language, like Java or Kotlin.
Run your Locust tests with BlazeMeter! Get scalability, multiple geo-locations and advanced reporting. To learn more, request a demo. Or, put your URL in the box below and your test will start in minutes.