Commit bdf26bcd1016dbf146015c2e942d5cc51d5960ad
1 parent
26e28ae0
feat(pubstress): example application to punish your broker
Showing
3 changed files
with
81 additions
and
0 deletions
CMakeLists.txt
| @@ -52,4 +52,5 @@ target_compile_options(${PROJECT_NAME} PRIVATE -Wall -Wextra -Wpedantic -Werror) | @@ -52,4 +52,5 @@ target_compile_options(${PROJECT_NAME} PRIVATE -Wall -Wextra -Wpedantic -Werror) | ||
| 52 | install(TARGETS ${PROJECT_NAME} LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}) | 52 | install(TARGETS ${PROJECT_NAME} LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}) |
| 53 | install(FILES ${CMAKE_BINARY_DIR}/truemqtt.pc DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/pkgconfig) | 53 | install(FILES ${CMAKE_BINARY_DIR}/truemqtt.pc DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/pkgconfig) |
| 54 | 54 | ||
| 55 | +add_subdirectory(example/pubstress) | ||
| 55 | add_subdirectory(example/pubsub) | 56 | add_subdirectory(example/pubsub) |
example/pubstress/CMakeLists.txt
0 → 100644
| 1 | +# | ||
| 2 | +# Copyright (c) TrueBrain | ||
| 3 | +# | ||
| 4 | +# This source code is licensed under the MIT license found in the | ||
| 5 | +# LICENSE file in the root directory of this source tree. | ||
| 6 | +# | ||
| 7 | + | ||
| 8 | +cmake_minimum_required(VERSION 3.16) | ||
| 9 | + | ||
| 10 | +project(truemqtt_pubstress) | ||
| 11 | + | ||
| 12 | +set(CMAKE_CXX_STANDARD 17) | ||
| 13 | +set(CMAKE_CXX_STANDARD_REQUIRED ON) | ||
| 14 | + | ||
| 15 | +include_directories(SYSTEM ${CMAKE_CURRENT_SOURCE_DIR}/../../include) | ||
| 16 | + | ||
| 17 | +add_executable(${PROJECT_NAME} main.cpp) | ||
| 18 | +target_link_libraries(${PROJECT_NAME} truemqtt) |
example/pubstress/main.cpp
0 → 100644
| 1 | +/* | ||
| 2 | + * Copyright (c) TrueBrain | ||
| 3 | + * | ||
| 4 | + * This source code is licensed under the MIT license found in the | ||
| 5 | + * LICENSE file in the root directory of this source tree. | ||
| 6 | + */ | ||
| 7 | + | ||
| 8 | +#include <TrueMQTT.h> | ||
| 9 | +#include <iostream> | ||
| 10 | +#include <thread> | ||
| 11 | + | ||
| 12 | +int main() | ||
| 13 | +{ | ||
| 14 | + // Create a connection to the local broker. | ||
| 15 | + TrueMQTT::Client client("localhost", 1883, "test"); | ||
| 16 | + | ||
| 17 | + client.setLogger(TrueMQTT::Client::LogLevel::WARNING, [](TrueMQTT::Client::LogLevel level, std::string message) | ||
| 18 | + { std::cout << "Log " << level << ": " << message << std::endl; }); | ||
| 19 | + client.setPublishQueue(TrueMQTT::Client::PublishQueueType::FIFO, 100); | ||
| 20 | + client.setErrorCallback([](TrueMQTT::Client::Error error, std::string message) | ||
| 21 | + { std::cout << "Error " << error << ": " << message << std::endl; }); | ||
| 22 | + client.setLastWill("test/lastwill", "example pubsub finished", true); | ||
| 23 | + | ||
| 24 | + client.connect(); | ||
| 25 | + std::this_thread::sleep_for(std::chrono::milliseconds(100)); | ||
| 26 | + | ||
| 27 | + int stop = 0; | ||
| 28 | + | ||
| 29 | + // Subscribe to the topic we are going to stress test. | ||
| 30 | + client.subscribe("test/test/test", [&stop](const std::string topic, const std::string payload) {}); | ||
| 31 | + | ||
| 32 | + // Send a lot of packets constantly, while telling us when publishing is failing. | ||
| 33 | + // The expected behaviour is that this goes okay for a while, till the broker | ||
| 34 | + // backs up, after which it starts to fail intermittently. To push the broker | ||
| 35 | + // to its breaking point, it helps to add additional subscriptions by other | ||
| 36 | + // means. | ||
| 37 | + bool is_failing = true; | ||
| 38 | + while (true) | ||
| 39 | + { | ||
| 40 | + if (!client.publish("test/test/test", "Hello World!", false)) | ||
| 41 | + { | ||
| 42 | + if (!is_failing) | ||
| 43 | + { | ||
| 44 | + is_failing = true; | ||
| 45 | + std::cout << "Failed to publish message" << std::endl; | ||
| 46 | + } | ||
| 47 | + } | ||
| 48 | + else | ||
| 49 | + { | ||
| 50 | + if (is_failing) | ||
| 51 | + { | ||
| 52 | + is_failing = false; | ||
| 53 | + std::cout << "Succeeded to publish message" << std::endl; | ||
| 54 | + } | ||
| 55 | + } | ||
| 56 | + } | ||
| 57 | + | ||
| 58 | + // This application never ends, but for good measure, a disconnect. | ||
| 59 | + client.disconnect(); | ||
| 60 | + | ||
| 61 | + return 0; | ||
| 62 | +} |