diff --git a/CMakeLists.txt b/CMakeLists.txt index e597a81..53eaf19 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -52,4 +52,5 @@ target_compile_options(${PROJECT_NAME} PRIVATE -Wall -Wextra -Wpedantic -Werror) install(TARGETS ${PROJECT_NAME} LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}) install(FILES ${CMAKE_BINARY_DIR}/truemqtt.pc DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/pkgconfig) +add_subdirectory(example/pubstress) add_subdirectory(example/pubsub) diff --git a/example/pubstress/CMakeLists.txt b/example/pubstress/CMakeLists.txt new file mode 100644 index 0000000..eee0cb3 --- /dev/null +++ b/example/pubstress/CMakeLists.txt @@ -0,0 +1,18 @@ +# +# Copyright (c) TrueBrain +# +# This source code is licensed under the MIT license found in the +# LICENSE file in the root directory of this source tree. +# + +cmake_minimum_required(VERSION 3.16) + +project(truemqtt_pubstress) + +set(CMAKE_CXX_STANDARD 17) +set(CMAKE_CXX_STANDARD_REQUIRED ON) + +include_directories(SYSTEM ${CMAKE_CURRENT_SOURCE_DIR}/../../include) + +add_executable(${PROJECT_NAME} main.cpp) +target_link_libraries(${PROJECT_NAME} truemqtt) diff --git a/example/pubstress/main.cpp b/example/pubstress/main.cpp new file mode 100644 index 0000000..359944d --- /dev/null +++ b/example/pubstress/main.cpp @@ -0,0 +1,62 @@ +/* + * Copyright (c) TrueBrain + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +#include +#include +#include + +int main() +{ + // Create a connection to the local broker. + TrueMQTT::Client client("localhost", 1883, "test"); + + client.setLogger(TrueMQTT::Client::LogLevel::WARNING, [](TrueMQTT::Client::LogLevel level, std::string message) + { std::cout << "Log " << level << ": " << message << std::endl; }); + client.setPublishQueue(TrueMQTT::Client::PublishQueueType::FIFO, 100); + client.setErrorCallback([](TrueMQTT::Client::Error error, std::string message) + { std::cout << "Error " << error << ": " << message << std::endl; }); + client.setLastWill("test/lastwill", "example pubsub finished", true); + + client.connect(); + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + + int stop = 0; + + // Subscribe to the topic we are going to stress test. + client.subscribe("test/test/test", [&stop](const std::string topic, const std::string payload) {}); + + // Send a lot of packets constantly, while telling us when publishing is failing. + // The expected behaviour is that this goes okay for a while, till the broker + // backs up, after which it starts to fail intermittently. To push the broker + // to its breaking point, it helps to add additional subscriptions by other + // means. + bool is_failing = true; + while (true) + { + if (!client.publish("test/test/test", "Hello World!", false)) + { + if (!is_failing) + { + is_failing = true; + std::cout << "Failed to publish message" << std::endl; + } + } + else + { + if (is_failing) + { + is_failing = false; + std::cout << "Succeeded to publish message" << std::endl; + } + } + } + + // This application never ends, but for good measure, a disconnect. + client.disconnect(); + + return 0; +}