Skip to content

Commit 1790d12

Browse files
authored
Merge pull request #3216 from ionutab/add_mqtt
Adding mqtt capabilities to main locust repo
2 parents 0f21d9b + d31082f commit 1790d12

File tree

9 files changed

+619
-8
lines changed

9 files changed

+619
-8
lines changed

docs/api.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ FastHttpUser class
2828
:members: wait_time, tasks, client, abstract, rest
2929
:noindex:
3030

31+
MqttUser class
32+
==================
33+
.. autoclass:: locust.contrib.mqtt.MqttUser
34+
:members: __init__, host, port, transport, ws_path, tls_context, client_cls, client_id, username, password, protocol
35+
3136
.. _socketio:
3237

3338
SocketIOUser class

docs/testing-other-systems.rst

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,22 @@ Performance/load testing AI services is a little different. While you could call
109109

110110
OpenAIUser is experimental and may change without notice.
111111

112+
MQTT
113+
====
114+
115+
Locust uses to `paho-mqtt https://github.com/eclipse-paho/paho.mqtt.python`_ to provide Mqtt connection capabilities.
116+
117+
.. literalinclude:: ../examples/mqtt/locustfile.py
118+
119+
Alternatively, if you need more control over the Mqtt client you can use a custom implementation.
120+
121+
.. literalinclude:: ../examples/mqtt/locustfile_custom_mqtt_client.py
122+
123+
.. note::
124+
125+
MqttUser is experimental and may change without notice.
126+
127+
112128
Other examples
113129
==============
114130

examples/mqtt/README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# MQTT Load testing with Locust
2+
3+
## Prerequisites
4+
5+
Have access to a running mqtt broker.
6+
7+
```bash
8+
# start mosquitto locally
9+
# the configuration file is required to start
10+
# WINDOWS
11+
docker run -d -p 1883:1883 --name mqtt-broker -v .\\examples\\mqtt\\mosquitto_config\\mosquitto.conf:/mosquitto/config/mosquitto.conf eclipse-mosquitto:latest
12+
# UNIX
13+
docker run -d -p 1883:1883 --name mqtt-broker -v ./examples/mqtt/mosquitto_config/mosquitto.conf:/mosquitto/config/mosquitto.conf eclipse-mosquitto:latest
14+
```
15+
16+
Install Locust with MQTT support:
17+
18+
```bash
19+
# Using pip
20+
pip install locust[mqtt]
21+
```
22+
23+
## Usage
24+
25+
```bash
26+
# Run simple example without web UI
27+
locust -f examples/mqtt/locustfile.py --headless
28+
# Run simple custom client example without web UI
29+
locust -f examples/mqtt/locustfile_custom_mqtt_client.py --headless
30+
```
31+

examples/mqtt/locustfile.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
from locust import task
2+
from locust.contrib.mqtt import MqttUser
3+
from locust.user.wait_time import between
4+
5+
import time
6+
7+
8+
class MyUser(MqttUser):
9+
host = "localhost"
10+
port = 1883
11+
12+
# We could uncomment below to use the WebSockets transport
13+
# transport = "websockets"
14+
15+
# ws_path = "/mqtt/custom/path"
16+
17+
# We'll probably want to throttle our publishing a bit: let's limit it to
18+
# 10-100 messages per second.
19+
wait_time = between(0.01, 0.1)
20+
21+
# Uncomment below if you need to set MQTTv5
22+
# protocol = paho.mqtt.client.MQTTv5
23+
24+
# Sleep for a while to allow the client time to connect.
25+
# This is probably not the most "correct" way to do this: a better method
26+
# might be to add a gevent.event.Event to the MqttClient's on_connect
27+
# callback and wait for that (with a timeout) here.
28+
# However, this works well enough for the sake of an example.
29+
def on_start(self):
30+
time.sleep(5)
31+
32+
@task
33+
def say_hello(self):
34+
self.client.publish("hello/locust", b"hello world")
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
from locust import task
2+
from locust.contrib.mqtt import MqttClient, MqttUser
3+
from locust.user.wait_time import between
4+
5+
import time
6+
7+
8+
# extend the MqttClient class with your own custom implementation
9+
class MyMqttClient(MqttClient):
10+
# you can override the event name with your custom implementation
11+
def _generate_event_name(self, event_type: str, qos: int, topic: str):
12+
return f"mqtt:{event_type}:{qos}"
13+
14+
15+
class MyUser(MqttUser):
16+
host = "localhost"
17+
port = 1883
18+
19+
# We could uncomment below to use the WebSockets transport
20+
# transport = "websockets"
21+
22+
# ws_path = "/mqtt/custom/path"
23+
24+
# We'll probably want to throttle our publishing a bit: let's limit it to
25+
# 10-100 messages per second.
26+
wait_time = between(0.01, 0.1)
27+
28+
# override the client_cls with your custom MqttClient implementation
29+
client_cls = MyMqttClient
30+
31+
# Sleep for a while to allow the client time to connect.
32+
# This is probably not the most "correct" way to do this: a better method
33+
# might be to add a gevent.event.Event to the MqttClient's on_connect
34+
# callback and wait for that (with a timeout) here.
35+
# However, this works well enough for the sake of an example.
36+
def on_start(self):
37+
time.sleep(5)
38+
39+
@task
40+
def say_hello(self):
41+
self.client.publish("hello/locust", b"hello world locust custom client")
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# mosquitto_config/mosquitto.conf
2+
3+
listener 1883
4+
allow_anonymous true

0 commit comments

Comments
 (0)