-
Notifications
You must be signed in to change notification settings - Fork 142
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
allowing more extensibility to customize the mqtt events logged in th…
…e database (#173) * allowing more extensibility in order to be able to customize the mqtt events logged in the database * fixed syntax issue * added example with simple custom Mqtt client implementation * removed unrequired override of initializer * removed unrequired dependencies
- Loading branch information
Showing
2 changed files
with
41 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import time | ||
import typing | ||
|
||
from locust import task, TaskSet | ||
from locust_plugins.users.mqtt import MqttUser | ||
from locust_plugins.users.mqtt import MqttClient | ||
|
||
|
||
# extend the MqttClient class with your own custom implementation | ||
class MyMqttClient(MqttClient): | ||
|
||
# you can override the event name with your custom implementation | ||
def _generate_event_name(self, event_type: str, qos: int, topic: str): | ||
return f"mqtt:{event_type}:{qos}" | ||
|
||
|
||
class MyUser(MqttUser): | ||
# override the client_cls with your custom MqttClient implementation | ||
client_cls: typing.Type[MyMqttClient] = MyMqttClient | ||
|
||
@task | ||
class MyTasks(TaskSet): | ||
# Sleep for a while to allow the client time to connect. | ||
# This is probably not the most "correct" way to do this: a better method | ||
# might be to add a gevent.event.Event to the MqttClient's on_connect | ||
# callback and wait for that (with a timeout) here. | ||
# However, this works well enough for the sake of an example. | ||
def on_start(self): | ||
time.sleep(5) | ||
|
||
@task | ||
def say_hello(self): | ||
self.client.publish("hello/locust", b"hello world") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters