From c9307ef991799541810ed2076cd2da6a6b55943f Mon Sep 17 00:00:00 2001 From: Rex Schilasky <49162693+rex-schilasky@users.noreply.github.com> Date: Fri, 19 Jul 2024 08:56:15 +0200 Subject: [PATCH] [sample] benchmark-sample-massive_pub_sub-added (#1666) --- samples/CMakeLists.txt | 4 +- .../benchmarks/massive_pub_sub/CMakeLists.txt | 41 ++++++ .../massive_pub_sub/src/massive_pub_sub.cpp | 128 ++++++++++++++++++ 3 files changed, 172 insertions(+), 1 deletion(-) create mode 100644 samples/cpp/benchmarks/massive_pub_sub/CMakeLists.txt create mode 100644 samples/cpp/benchmarks/massive_pub_sub/src/massive_pub_sub.cpp diff --git a/samples/CMakeLists.txt b/samples/CMakeLists.txt index 54c5f882e7..4b3bb66b65 100644 --- a/samples/CMakeLists.txt +++ b/samples/CMakeLists.txt @@ -1,6 +1,6 @@ # ========================= eCAL LICENSE ================================= # -# Copyright (C) 2016 - 2019 Continental Corporation +# Copyright (C) 2016 - 2024 Continental Corporation # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -48,6 +48,8 @@ add_subdirectory(cpp/benchmarks/latency_rec) add_subdirectory(cpp/benchmarks/latency_snd) add_subdirectory(cpp/benchmarks/many_connections_rec) add_subdirectory(cpp/benchmarks/many_connections_snd) +add_subdirectory(cpp/benchmarks/massive_pub_sub) + if(HAS_HDF5) add_subdirectory(cpp/benchmarks/measurement) endif() diff --git a/samples/cpp/benchmarks/massive_pub_sub/CMakeLists.txt b/samples/cpp/benchmarks/massive_pub_sub/CMakeLists.txt new file mode 100644 index 0000000000..5c2f37e547 --- /dev/null +++ b/samples/cpp/benchmarks/massive_pub_sub/CMakeLists.txt @@ -0,0 +1,41 @@ +# ========================= eCAL LICENSE ================================= +# +# Copyright (C) 2016 - 2024 Continental Corporation +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# ========================= eCAL LICENSE ================================= + +cmake_minimum_required(VERSION 3.10) + +set(CMAKE_FIND_PACKAGE_PREFER_CONFIG ON) + +project(massive_pub_sub) + +find_package(eCAL REQUIRED) + +set(massive_pub_sub_src + src/massive_pub_sub.cpp +) + +ecal_add_sample(${PROJECT_NAME} ${massive_pub_sub_src}) + +target_link_libraries(${PROJECT_NAME} + eCAL::core +) + +target_compile_features(${PROJECT_NAME} PRIVATE cxx_std_14) + +ecal_install_sample(${PROJECT_NAME}) + +set_property(TARGET ${PROJECT_NAME} PROPERTY FOLDER samples/cpp/benchmarks/massive_pub_sub) diff --git a/samples/cpp/benchmarks/massive_pub_sub/src/massive_pub_sub.cpp b/samples/cpp/benchmarks/massive_pub_sub/src/massive_pub_sub.cpp new file mode 100644 index 0000000000..7cf864a4e6 --- /dev/null +++ b/samples/cpp/benchmarks/massive_pub_sub/src/massive_pub_sub.cpp @@ -0,0 +1,128 @@ +/* ========================= eCAL LICENSE ================================= + * + * Copyright (C) 2016 - 2024 Continental Corporation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * ========================= eCAL LICENSE ================================= +*/ + +#include + +#include +#include +#include +#include +#include + +const int subscriber_number (5000); + +const int publisher_number (5000); +const int publisher_type_encoding_size_bytes (10*1024); +const int publisher_type_descriptor_size_bytes (10*1024); + +const int in_between_sleep_sec (5); +const int final_sleep_sec (120); + +std::string GenerateSizedString(const std::string& name, size_t totalSize) +{ + if (name.empty() || totalSize == 0) { + return ""; + } + + std::string result; + result.reserve(totalSize); + + while (result.size() + name.size() <= totalSize) { + result += name; + } + + if (result.size() < totalSize) { + result += name.substr(0, totalSize - result.size()); + } + + return result; +} + +int main(int argc, char** argv) +{ + // initialize eCAL API + eCAL::Initialize(argc, argv, "massive_pub_sub"); + + eCAL::Util::EnableLoopback(true); + + // create subscriber + std::vector vector_of_subscriber; + std::cout << "Subscriber creation started. (" << subscriber_number << ")" << std::endl; + { + // start time measurement + auto start_time = std::chrono::high_resolution_clock::now(); + + for (int i = 0; i < subscriber_number; i++) + { + // publisher topic name + std::stringstream tname; + tname << "TOPIC_" << i; + + // create subscriber + vector_of_subscriber.emplace_back(tname.str()); + } + // stop time measurement + auto end_time = std::chrono::high_resolution_clock::now(); + + // calculate the duration + auto duration = std::chrono::duration_cast(end_time - start_time).count(); + std::cout << "Time taken for subscriber creation: " << duration << " milliseconds" << std::endl; + } + + // sleep for a few seconds + std::this_thread::sleep_for(std::chrono::seconds(in_between_sleep_sec)); + + // create publisher + std::vector vector_of_publisher; + std::cout << "Publisher creation started. (" << publisher_number << ")" << std::endl; + { + // start time measurement + auto start_time = std::chrono::high_resolution_clock::now(); + + eCAL::SDataTypeInformation data_type_info; + data_type_info.name = "TOPIC_TYPE_NAME"; + data_type_info.encoding = GenerateSizedString("TOPIC_TYPE_ENCODING", publisher_type_encoding_size_bytes); + data_type_info.descriptor = GenerateSizedString("TOPIC_TYPE_DESCRIPTOR", publisher_type_descriptor_size_bytes); + + for (int i = 0; i < publisher_number; i++) + { + // publisher topic name + std::stringstream tname; + tname << "TOPIC_" << i; + + // create publisher + vector_of_publisher.emplace_back(tname.str(), data_type_info); + } + // stop time measurement + auto end_time = std::chrono::high_resolution_clock::now(); + + // calculate the duration + auto duration = std::chrono::duration_cast(end_time - start_time).count(); + std::cout << "Time taken for publisher creation: " << duration << " milliseconds" << std::endl; + } + std::cout << std::endl; + + // sleep for a few seconds + std::this_thread::sleep_for(std::chrono::seconds(final_sleep_sec)); + + // finalize eCAL API + eCAL::Finalize(); + + return(0); +}