Skip to content

Commit

Permalink
[test] Cleanup hdf5 test, and split in test and benchmark. (#1344)
Browse files Browse the repository at this point in the history
  • Loading branch information
KerstinKeller authored Feb 2, 2024
1 parent 8b30e33 commit 753f67f
Show file tree
Hide file tree
Showing 4 changed files with 367 additions and 530 deletions.
3 changes: 3 additions & 0 deletions samples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ 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)
if(HAS_HDF5)
add_subdirectory(cpp/benchmarks/measurement)
endif()
add_subdirectory(cpp/benchmarks/multiple_rec_cb)
add_subdirectory(cpp/benchmarks/multiple_snd)
add_subdirectory(cpp/benchmarks/performance_rec)
Expand Down
37 changes: 37 additions & 0 deletions samples/cpp/benchmarks/measurement/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# ========================= eCAL LICENSE =================================
#
# Copyright (C) 2016 - 2019 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(benchmark_hdf5)

find_package(eCAL REQUIRED)

ecal_add_sample(${PROJECT_NAME} src/main.cpp)

target_link_libraries(${PROJECT_NAME}
eCAL::hdf5
)

target_compile_features(${PROJECT_NAME} PRIVATE cxx_std_14)

ecal_install_sample(${PROJECT_NAME})

set_property(TARGET ${PROJECT_NAME} PROPERTY FOLDER samples/cpp/benchmarks/measurement)
148 changes: 148 additions & 0 deletions samples/cpp/benchmarks/measurement/src/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
/* ========================= eCAL LICENSE =================================
*
* Copyright (C) 2016 - 2019 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 <chrono>
#include <fstream>
#include <iostream>
#include <string>
#include <thread>

#include <ecalhdf5/eh5_meas.h>

#define TEST_RAW_PERF 1
#define TEST_HDF5_PERF 1

#define USE_OFSTREAM 0
#define TEST_POST_SLEEP 2 // seconds

#define DATA_SET_SIZE_1 (1024) // 1 kB
#define DATA_SET_NUMBER_1 (64*1024)

#define DATA_SET_SIZE_2 (128*1024) // 128 kB
#define DATA_SET_NUMBER_2 (8*1024)

#define DATA_SET_SIZE_3 (1024*1024) // 1 MB
#define DATA_SET_NUMBER_3 (1024)

#define DATA_SET_SIZE_4 (4096*1024) // 4 MB
#define DATA_SET_NUMBER_4 (256)

namespace
{
// properties
std::string output_dir = "measurement_dir";
const size_t max_size_per_file = 500; // MB
std::vector<char> data;
}

void MeasPerf(const std::string& file_name, const size_t pkg_size, const size_t pkg_num)
{
data.resize(pkg_size);
const size_t write_loops(pkg_num);

#if TEST_RAW_PERF
// Test raw performance
{
// start time
auto start = std::chrono::high_resolution_clock::now();

#if USE_OFSTREAM

std::ofstream rfile(file_name + "_raw", std::ios::trunc | std::ios::out | std::ios::binary);
if (rfile)
{
for (size_t loops = 0; loops < write_loops; ++loops)
{
rfile.write(data.data(), data.size());
}
}
rfile.close();

#else // USE_OFSTREAM

FILE* pFile(nullptr);
std::string fname = file_name + "_raw";
pFile = fopen(fname.c_str(), "wb");
if (pFile)
{
for (size_t loops = 0; loops < write_loops; ++loops)
{
fwrite(data.data(), 1, data.size(), pFile);
}
fclose(pFile);
}

#endif // USE_OFSTREAM

// end time
auto finish = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> elapsed = finish - start;
size_t sum_data = data.size() * write_loops;
std::cout << std::endl;
std::cout << "Packages number : " << write_loops << std::endl;
std::cout << "Packages size : " << data.size()/1024 << " kB" << std::endl;
std::cout << "Sum payload : " << sum_data / (1024*1024) << " MB" << std::endl;
std::cout << "Throughput RAW : " << int((sum_data / (1024.0 * 1024.0)) / elapsed.count()) << " MB/s " << std::endl;
std::cout << std::endl;

std::this_thread::sleep_for(std::chrono::seconds(TEST_POST_SLEEP));
}
#endif

#if TEST_HDF5_PERF
// Test HDF5 performance
{
// start time
auto start = std::chrono::high_resolution_clock::now();

eCAL::eh5::HDF5Meas writer(output_dir, eCAL::eh5::CREATE);
writer.SetFileBaseName(file_name + "_hdf5");
writer.SetMaxSizePerFile(max_size_per_file);
for (size_t loop = 0; loop < write_loops; ++loop)
{
writer.AddEntryToFile(static_cast<void*>(data.data()), data.size(), 0, 0, "myChannel", 0, loop);
}
writer.Close();

// end time
auto finish = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> elapsed = finish - start;
size_t sum_data = data.size() * write_loops;
std::cout << std::endl;
std::cout << "Packages number : " << write_loops << std::endl;
std::cout << "Packages size : " << data.size() / 1024 << " kB" << std::endl;
std::cout << "Sum payload : " << sum_data / (1024 * 1024) << " MB" << std::endl;
std::cout << "Throughput HDF5 : " << int((sum_data / (1024.0 * 1024.0)) / elapsed.count()) << " MB/s " << std::endl;
std::cout << std::endl;

std::this_thread::sleep_for(std::chrono::seconds(TEST_POST_SLEEP));
}
#endif
}

int main()
{
MeasPerf("meas_1_pkg", DATA_SET_SIZE_1, DATA_SET_NUMBER_1);

MeasPerf("meas_128_pkg", DATA_SET_SIZE_2, DATA_SET_NUMBER_2);

MeasPerf("meas_1024_pkg", DATA_SET_SIZE_3, DATA_SET_NUMBER_3);

MeasPerf("meas_4096_pkg", DATA_SET_SIZE_4, DATA_SET_NUMBER_4);
}
Loading

0 comments on commit 753f67f

Please sign in to comment.