Skip to content

Commit

Permalink
[sample] benchmark-sample-massive_pub_sub-added (#1666)
Browse files Browse the repository at this point in the history
  • Loading branch information
rex-schilasky authored Jul 19, 2024
1 parent e140a1c commit c9307ef
Show file tree
Hide file tree
Showing 3 changed files with 172 additions and 1 deletion.
4 changes: 3 additions & 1 deletion samples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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()
Expand Down
41 changes: 41 additions & 0 deletions samples/cpp/benchmarks/massive_pub_sub/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
128 changes: 128 additions & 0 deletions samples/cpp/benchmarks/massive_pub_sub/src/massive_pub_sub.cpp
Original file line number Diff line number Diff line change
@@ -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 <ecal/ecal.h>

#include <iostream>
#include <chrono>
#include <sstream>
#include <thread>
#include <vector>

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<eCAL::CSubscriber> 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<std::chrono::milliseconds>(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<eCAL::CPublisher> 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<std::chrono::milliseconds>(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);
}

0 comments on commit c9307ef

Please sign in to comment.