Commit 20d67a1c85e63522aa78a7b4b4fb9ab1d21cda4a

Authored by Peter M. Groen
1 parent 03833229

Added speedtest

.gitignore
1 1 /build
  2 +/CMakeLists.txt.user
... ...
CMakeLists.txt
... ... @@ -53,3 +53,6 @@ install(TARGETS ${PROJECT_NAME} LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} PUBL
53 53 install(FILES ${CMAKE_BINARY_DIR}/truemqtt.pc DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/pkgconfig)
54 54  
55 55 add_subdirectory(example/pubsub)
  56 +add_subdirectory(example/speedtest/pub)
  57 +add_subdirectory(example/speedtest/sub)
  58 +
... ...
example/speedtest/pub/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_pubspeed)
  11 +
  12 +set(CMAKE_CXX_STANDARD 17)
  13 +set(CMAKE_CXX_STANDARD_REQUIRED ON)
  14 +
  15 +include_directories(SYSTEM ${CMAKE_SOURCE_DIR}/include)
  16 +
  17 +add_executable(${PROJECT_NAME} main.cpp)
  18 +target_link_libraries(${PROJECT_NAME} truemqtt)
... ...
example/speedtest/pub/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 <chrono>
  10 +#include <iostream>
  11 +#include <thread>
  12 +#include <unistd.h>
  13 +
  14 +enum TIME_RES
  15 +{
  16 + T_MICRO,
  17 + T_MILLI,
  18 + T_SECONDS
  19 +};
  20 +
  21 +std::uint64_t getEpochUSecs()
  22 +{
  23 + auto tsUSecs = std::chrono::time_point_cast<std::chrono::microseconds>(std::chrono::system_clock::now());
  24 + return static_cast<std::uint64_t>(tsUSecs.time_since_epoch().count());
  25 +}
  26 +
  27 +int main(int argc, char* argv[])
  28 +{
  29 + // Create a connection to a local broker over IPv4 and setup the MQTT-Stack
  30 + // =========================================================================
  31 + TrueMQTT::Client client("127.0.0.1", 1883, "SpeedTestPub");
  32 +
  33 + client.setLogger(TrueMQTT::Client::LogLevel::TRACE, [](TrueMQTT::Client::LogLevel level, std::string message)
  34 + { std::cout << "Log " << level << ": " << message << std::endl;});
  35 +
  36 + client.setPublishQueue(TrueMQTT::Client::PublishQueueType::FIFO, 10);
  37 +
  38 + client.setErrorCallback([](TrueMQTT::Client::Error error, std::string message)
  39 + { std::cout << "Error " << error << ": " << message << std::endl;});
  40 +
  41 + client.setLastWill("SpeedTest/lastWill", "SpeedTest Publisher finished", true);
  42 +
  43 + client.connect();
  44 + std::this_thread::sleep_for(std::chrono::milliseconds(100));
  45 + // =========================================================================
  46 + //
  47 + // Keep sending until we press ctrl-c
  48 +
  49 + // Clear the SpeedTest topic before blowing the fuses
  50 + client.publish("SpeedTest/", "", false);
  51 + std::this_thread::sleep_for(std::chrono::milliseconds(100));
  52 +
  53 + unsigned int message_number = 0;
  54 + while(1)
  55 + {
  56 + if(message_number < 10)
  57 + {
  58 + client.publish("SpeedTest/message[" + std::to_string(message_number) + "]", std::to_string(getEpochUSecs()), false);
  59 + message_number++;
  60 + }
  61 + else
  62 + {
  63 + message_number = 0;
  64 + std::this_thread::sleep_for(std::chrono::milliseconds(100));
  65 + }
  66 + }
  67 +
  68 +}
... ...
example/speedtest/sub/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_subspeed)
  11 +
  12 +set(CMAKE_CXX_STANDARD 17)
  13 +set(CMAKE_CXX_STANDARD_REQUIRED ON)
  14 +
  15 +include_directories(SYSTEM ${CMAKE_SOURCE_DIR}/include)
  16 +
  17 +add_executable(${PROJECT_NAME} main.cpp)
  18 +target_link_libraries(${PROJECT_NAME} truemqtt)
... ...
example/speedtest/sub/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 <chrono>
  10 +#include <iostream>
  11 +#include <thread>
  12 +#include <unistd.h>
  13 +#include <vector>
  14 +
  15 +
  16 +enum TIME_RES
  17 +{
  18 + T_MICRO,
  19 + T_MILLI,
  20 + T_SECONDS
  21 +};
  22 +
  23 +std::vector<int> vecTimings;
  24 +
  25 +std::uint64_t getEpochUSecs()
  26 +{
  27 + auto tsUSecs = std::chrono::time_point_cast<std::chrono::microseconds>(std::chrono::system_clock::now());
  28 + return static_cast<std::uint64_t>(tsUSecs.time_since_epoch().count());
  29 +}
  30 +
  31 +void processTimes(int epochTat)
  32 +{
  33 + vecTimings.emplace_back( epochTat );
  34 +
  35 + if(vecTimings.size() == 10)
  36 + {
  37 + float avgTat = 0;
  38 + int totalTat = 0;
  39 + for(auto timing : vecTimings)
  40 + {
  41 + totalTat += timing;
  42 + }
  43 + avgTat = totalTat / vecTimings.size();
  44 +
  45 + std::cout << "Average : " << std::to_string(static_cast<int>(avgTat)) << " uSecs" << "( " << std::to_string( 1000000 / avgTat ) << " messages / second )" << std::endl;
  46 +
  47 + // Clear the vector
  48 + vecTimings.clear();
  49 + }
  50 +}
  51 +
  52 +int main(int argc, char* argv[])
  53 +{
  54 + // Create a connection to a local broker over IPv4 and setup the MQTT-Stack
  55 + // =========================================================================
  56 + TrueMQTT::Client client("127.0.0.1", 1883, "SpeedTestSub");
  57 +
  58 + client.setLogger(TrueMQTT::Client::LogLevel::TRACE, [](TrueMQTT::Client::LogLevel level, std::string message)
  59 + { std::cout << "Log " << level << ": " << message << std::endl;});
  60 +
  61 + client.setPublishQueue(TrueMQTT::Client::PublishQueueType::FIFO, 10);
  62 +
  63 + client.setErrorCallback([](TrueMQTT::Client::Error error, std::string message)
  64 + { std::cout << "Error " << error << ": " << message << std::endl;});
  65 +
  66 + client.setLastWill("SpeedTest/lastWillSub", "SpeedTest Subscriber finished", true);
  67 +
  68 + client.connect();
  69 + std::this_thread::sleep_for(std::chrono::milliseconds(100));
  70 + // =========================================================================
  71 + //
  72 + // Keep receiving until we press ctrl-c
  73 +
  74 + client.subscribe("SpeedTest/#", [](const std::string topic, const std::string payload)
  75 + {
  76 + char *end;
  77 + int tat = getEpochUSecs() - std::strtoull(payload.c_str(), &end, 10);
  78 + processTimes(tat);
  79 + });
  80 +
  81 + while(1)
  82 + {
  83 + std::this_thread::sleep_for(std::chrono::milliseconds(1000));
  84 + }
  85 +
  86 +}
... ...