EMQX- Publish MQTT Topic with unique identifier is taking much more time than Static MQTT Topic

397 Views Asked by At

I was trying to publish messages on emqx broker on different topics.Scenario takes much time while publishing with dynamic topic with one client and if we put topic name as static it takes much less time.

Here I have posted result and code for the same.

I am using EMQX broker with Eclipse paho client Version 3 and Qos level 1.

Time for different topics with 100 simple publish message (Consider id as dynamic here):

Total time pattern 1: /config/{id}/outward::36 sec -----------------> HERE TOPIC is DYNAMIC. and {id} is a variable whose value is changing in loop as shown in below code

Total time pattern 2: /config/test::1.2 sec -----------------------> HERE TOPIC is STATIC

How shall I publish message with different id so topic creation wont take much time?

public class MwttPublish {
    static IMqttClient instance= null;
public static IMqttClient getInstance() {
        try {
            if (instance == null) {         
                instance = new MqttClient(mqttHostUrl, "SimpleTestMQTT");
            }
            if (!instance.isConnected()) {
                MqttConnectOptions options = new MqttConnectOptions();
                options.setUserName("test");
                options.setPassword("test".toCharArray());
                options.setAutomaticReconnect(true);
                options.setCleanSession(false);
                options.setConnectionTimeout(10);

                instance.connect(options);
            }
        } catch (final Exception e) {
            System.out.println("Exception in mqtt: {}" + e.getMessage());
        }
        return instance;
    }
    public static void publishMessage() throws MqttException {
        IMqttClient iMqttClient = getInstance();
        MqttMessage mqttMessage = new MqttMessage("Hello".getBytes());
        mqttMessage.setQos(1);
        mqttMessage.setRetained(true);

        System.out.println("Publish Start for pattern 1");
        int i =0;
        final BigDecimal mqttmsgPublishstartTime = new BigDecimal(System.currentTimeMillis());
        do {
            iMqttClient.publish("/config/" +i +"/outward", mqttMessage);
            i++;
        }while(i<100);
        System.out.println("Total time  pattern 1 /config/i/outward::" + (new BigDecimal(System.currentTimeMillis())).subtract(mqttmsgPublishstartTime));


        System.out.println("Publish Start for pattern 2");

        final BigDecimal mqttmsgPublishstartTime1 = new BigDecimal(System.currentTimeMillis());
        i =0;
        do {
            iMqttClient.publish("/config/test", mqttMessage);
            i++;
        }while(i<100);
        System.out.println("Total time pattern 2 /config/test::" + (new BigDecimal(System.currentTimeMillis())).subtract(mqttmsgPublishstartTime1));

        
    }
}
1

There are 1 best solutions below

6
hardillb On

This is not a valid test, you've fallen into many of the clasic micro benchmark traps e.g.

  • Way too small a sample size
  • No account for JVM JIT warm up or GC overhead
  • Not comparing like to like e.g. time taken to concatenate the strings for the topics

Please check out the following: https://stackoverflow.com/a/2844291/504554

Also from a MQTT point of view topics are ephemeral they only really "exist" for the instant a message is published while the broker checks for subscribed clients with a matching pattern.