JMeter has built-in HTTP/HTTPS, TCP, etc. to support multiple protocols, and also has a plug-in extension mechanism.

The MQTT protocol is the mainstream protocol in the IoT world. Although it is not a protocol type that comes with JMeter, it is extremely common in IoT test scenarios. In order to support the scale testing of the MQTT protocol, EMQ has developed an open source test plug-in for the MQTT protocol based on JMeter: https://github.com/xmeter-net/mqtt-jmeter .

After several iterations, the latest version of the JMeter MQTT plugin is 2.0.2, which supports connection, message publishing, message subscription and other samplers, and can be combined to build more complex test scenarios.

In this article we will specifically describe how to use the MQTT plugin in JMeter.

Install the MQTT plugin

The MQTT plugin is installed in a similar way to other JMeter third-party plugins.

  1. Download the latest version of the plugin mqtt-xmeter-2.0.2-jar-with-dependencies.jar from GitHub, which supports JMeter 3.2 and above.
  2. Copy the plugin jar package to the JMeter plugin directory: $JMETER_HOME/lib/ext
  3. Restart JMeter.

Main components in the MQTT plugin

MQTT Connect Sampler (MQTT Connect)

Connect the sampler to simulate an IoT device and initiate an MQTT connection.

MQTT 连接

Server name or IP: points to the MQTT server address under test.

Port number: Take EMQ X as an example, the default port is 1883 for TCP connection and 8883 for SSL connection. For specific ports, please refer to the specific configuration of the server.

MQTT version : Currently supports MQTT 3.1 and 3.1.1 versions.

Timeout: Connection timeout setting, in seconds.

Protocols: supports TCP, SSL, WS and WSS connections to the MQTT server. When choosing SSL or WSS encrypted channel connection, you can choose one-way or two-way authentication (Dual). If you want to perform two-way authentication, you also need to specify the corresponding client certificate (p12 certificate) and the corresponding file protection password (Secret).

User authentication: If the MQTT server is configured with user authentication, you need to provide the corresponding user name ( User name ) and password ( Password ).

ClientId: of the virtual user. If "Add random suffix for ClientId" is checked, a uuid string will be added as a suffix to each virtual user based on the ClientId, and the whole will be used as a virtual user ID.

Keep alive(s): Heartbeat signal sending interval. For example, 300 means that the client pings the server every 300 seconds to keep the connection alive.

Connect attempt max: The maximum number of attempts to reconnect during the first connection. If the number of times is exceeded, the connection is considered to have failed. If you want to always try to reconnect, you can set it to -1.

Reconnect attempt max: The maximum number of reconnect attempts during the subsequent connection process. If the number of times is exceeded, the connection is considered to have failed. If you want to always try to reconnect, you can set it to -1.

Clean session : Set this option to false if you want to preserve session state between connections. Set this to true if you do not want session state to be preserved on new connections.

MQTT message publishing sampler (MQTT Pub Sampler)

The message publishing sampler reuses the MQTT connection established in the connection sampler to publish messages to the target MQTT server.

发布 MQTT 消息

QoS Level: Quality of service, the value is 0, 1, 2, which represent at most once (AT_MOST_ONCE), at least once (AT_LEAST_ONCE), and exactly once (EXACTLY_ONCE) in the MQTT protocol specification.

Retained messages : If you want to use "retained messages", you can set this option to true, the MQTT server will store the retained messages published by the plugin and their QoS, and when a subscription occurs on the corresponding topic, the last message is directly retained The message is delivered to the subscriber, so that the subscriber can obtain the latest status value of the publisher without waiting.

Topic name: The topic to which the message belongs.

Add timestamp in payload: If checked, the current timestamp will be attached at the beginning of the published message body. With the Payload includes timestamp option of the message subscription sampler, the message arrival delay can be calculated at the message receiver. If unchecked, only the actual message body will be sent.

Payloads Message type: Currently supports three message types

  • String: normal string
  • Hex String: A string represented by a hexadecimal value, such as the string Hello, which can be represented as 48656C6C6F (where 48 corresponds to the letter H in the ascii table, and so on). Usually hexadecimal strings are used to construct non-text message bodies, such as describing some private protocol interaction and control information, etc.
  • Random string with fixed length: Generate a random string with the specified length (in bytes) as the message body.

MQTT message subscription sampler (MQTT Sub Sampler)

The message publishing sampler reuses the MQTT connection established in the connection sampler to subscribe messages from the target MQTT server.

MQTT 消息订阅

QoS Level: QoS, the meaning is the same as the message publishing sampler.

Topic name(s): The topic to which the subscribed message belongs. Supports a single message subscription sampler to subscribe to multiple topics, separated by commas.

Payload includes timestamp: If checked, the sending timestamp will be parsed from the beginning of the message body, which can be used to calculate the receiving delay of the message in conjunction with the Add timestamp in payload option of the message publishing sampler. If not checked, only the actual message body will be parsed.

Sample on : The sampling method, the default is " specified elapsed time(ms) ", that is, sampling every specified millisecond time. You can also select " number of received messages ", that is, sampling every time the specified number of messages is received.

Debug response: If checked, the message content will be printed in the JMeter response result. This option is mainly used for debugging purposes. It is not recommended to check it when running the test formally, so as not to affect the test efficiency.

MQTT DisConnect Sampler (MQTT DisConnect)

Disconnects the MQTT connection established in the connection sampler.

断开 MQTT 连接

For flexibility, the property values in the above samplers can all refer to JMeter's system or custom variables.

In this article, we introduced the test components of the JMeter MQTT plugin. In the next article, we will introduce in detail how to use the MQTT plugin to build test scripts for different test scenarios.


EMQX
336 声望436 粉丝

EMQ(杭州映云科技有限公司)是一家开源物联网数据基础设施软件供应商,交付全球领先的开源 MQTT 消息服务器和流处理数据库,提供基于云原生+边缘计算技术的一站式解决方案,实现企业云边端实时数据连接、移动、...