Over a million developers have joined DZone.

Load Testing MQTT Servers With Java, Maven, and Gatling

DZone 's Guide to

Load Testing MQTT Servers With Java, Maven, and Gatling

As MQTT protocol has been becoming more and more popular, it's becoming increasingly important to load test IoT devices.

Free Resource

The rapid growth of the IoT market dramatically increased the popularity of MQTT protocol. MQTT is supported by the most popular IoT platforms and is used for data collection, push notifications, real-time messaging, and more.

We would like to share our experience with running performance tests against MQTT servers in this article. We are going to concentrate on the tools that we used to generate load on the server. The results of the performance tests for our platform will be published as a separate post.


We have chosen Gatling as a framework to run our test due to a number of reasons:

  • Generates beautiful reports out-of-the-box.
  • High performance and low overhead.
  • Easy setup of load scenarios.
  • Scalability of simulations.

Unfortunately, the Gatling.io framework from doesn’t support MQTT protocol out of the box. At the same time, Gatling is an open-source framework, and we have found an unofficial MQTT plugin.

Gatling MQTT Plugin

The Gatling-MQTT plugin was developed by Muneyuki Noguchi, and at the moment, it is hosted on GitHub under an Apache 2.0 License. We have started the implementation of a Thingsboard performance testing project using Gatling and the Gatling-MQTT plugin. Some time later, we have realized that the plugin doesn’t support scenarios that we would like to verify and the behavior of default scenario is not something that we have expected.

The problem with the default scenario of the unofficial Gatling-MQTT plugin is that each time some data is published, the client waits for a reply from the server and sends MQTT disconnect. So, the message sequence looks like this:

Image title

This approach is very resource-consuming and gives minimal benefits comparing to HTTP or other protocols. This behavior is normal for an HTTP requests-response model, but not for MQTT. The typical MQTT session is maintained for certain time and multiple MQTT publish messages are sent and received between client and MQTT broker. Of course, there are other types of MQTT messages, but they were out of scope for our tests. In our scenario, load testing of the IoT platform must be done in the following way:

Image title

Once we have connected a device to the IoT platform that acts as an MQTT broker, we will reuse the session and publish MQTT messages using the same session. Sure, the session could be recreated at some point, but not every time we would like to publish a message to the server.

In order to support this scenario, we have decided not to implement something new from the scratch, but rather to use Gatling-MQTT plugin as a base and considering fact that this is open-source we are free to modify the software as we wish to meet our needs.

Gatling MQTT Plugin Fork

We have done the fork of Gatling-MQTT plugin, spent some time on investigation how the plugin was constructed, modified it, and added Connect, Publish, and Disconnect actions as separate steps.

Then, we were able to support the expected scenario. The extended version of Gatling-MQTT plugin is located here Extended Gatling-MQTT.

Finally, we were able to implement the scenario that suits our needs. The Gatling simulation below will create separate MQTT session for 10 simulated devices and will send 100 publish messages for each session.

class MqttSimulation extends Simulation {
    val mqttConfiguration = mqtt
        // MQTT broker

    val connect = exec(mqtt("connect")

    // send 100 publish MQTT messages
    val publish = repeat(100) {
            // topic: "foo"
            // payload: "Hello"
            // QoS: AT_LEAST_ONCE (1)
            // retain: false
            .publish("foo", "Hello", QoS.AT_LEAST_ONCE, retain = false))
            // 1 seconds pause between sending messages
            .pause(1000 milliseconds)

    val disconnect = exec(mqtt("disconnect")

    val scn = scenario("MQTT Test")
      .exec(connect, publish, disconnect)

        // linearly connect 10 devices over 1 second 
        // and send 100 publish messages
        .inject(rampUsers(10) over (1 seconds))

Performance Tests Project

Our performance test project is hosted on GitHub. It is mainly written in Java and uses Maven as a build tool. Gatling and Gatling-MQTT plugin are written in Scala, and use SBT tool for building sources and running tests. However, here at Thingsboard, we are more Java guys than Scala. That’s why we have implemented custom Java code that connects to the platform, creates a device, warms it up, and provides the credentials ID string back.


// get the device credential ids of the created devices
val deviceCredentialsIds: Array[String] = MqttStressTestTool.createDevices(testParams).asScala.toArray


RestClient restClient = new RestClient(params.getRestApiUrl());
// login to the Thingsboard server
restClient.login(params.getUsername(), params.getPassword());
for (int i = 0; i < params.getDeviceCount(); i++) {
    // create device using REST API
    Device device = restClient.createDevice("Device " + UUID.randomUUID());
    // get credentials from the created device
    DeviceCredentials credentials = restClient.getCredentials(device.getId());
    // store in the array that eventually will be used by Simulation class    
    String[] mqttUrls = params.getMqttUrls();
    String mqttURL = mqttUrls[i % mqttUrls.length];
    MqttStressTestClient client = new MqttStressTestClient(results, mqttURL, credentials.getCredentialsId());
    // connect to the server and do the warm up 

The list of credential IDs is used in the MqttSimulation.scala file to do the stress test itself:

// get the device credential ids of the created devices
val deviceCredentialsIds: Array[String] = MqttStressTestTool.createDevices(testParams).asScala.toArray

// provide device credential id as username during the connection phase to the Thingsboard server
val mqttConf = mqtt

val connect = exec(mqtt("connect").connect())

val publish = repeat(testParams.getPublishTelemetryCount.toInt) {
        // publish single message and verify that it was delivered at least once
        .publish("v1/devices/me/telemetry", "{\"temp\":73.2}", QoS.AT_LEAST_ONCE, retain = false))
        .pause(testParams.getPublishTelemetryPause milliseconds)

val disconnect = exec(mqtt("disconnect").disconnect())
// create map of device credential ids of the connected devices  
// and use it as a feeder in the scenario
val deviceCredentialsIdsFeeder = deviceCredentialsIds.map( x => {Map("deviceCredentialsId" -> x)})

val scn = scenario("Scenario Name")
    // go over the map and take column deviceCredentialsId as username
    .exec(connect, publish, disconnect)

    .inject(rampUsers(deviceCredentialsIds.length) over (1 seconds))

For the guys who prefer Java and Maven, there is a Maven plugin gatling-maven-plugin:


These plugin find Simulation files in your project and compiles and runs them. Results will be stored inside of the target folder in a pretty format that you are able to inspect after the run:

Image title

Image title

Image title

To run the tests, you simply can type:

mvn clean install gatling:execute


In general, we have described our approach how we have generated high loads on Thingsboard IoT platform and verified that it provides good results. We will share the results of platform performance tests in the next post. Also, we’ll describe code changes, improvements and instances tuning that we have done to achieve processing of more than 1 million MQTT messages per minute. These are basically the first steps for us in the direction of performance testing of the platform, and any feedback regarding this approach is more than welcome.

We hope that the experience provided will help you to verify your solutions against load specifications that you expect to see in the production. Stay tuned and star our project on GitHub.

mqtt ,gatling ,maven ,iot platform ,performance ,load testing

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}