diff --git a/rcl_logging_spdlog/CMakeLists.txt b/rcl_logging_spdlog/CMakeLists.txt index 6b89afc..0ae2837 100644 --- a/rcl_logging_spdlog/CMakeLists.txt +++ b/rcl_logging_spdlog/CMakeLists.txt @@ -41,6 +41,11 @@ if(BUILD_TESTING) find_package(ament_lint_auto REQUIRED) ament_lint_auto_find_test_dependencies() + find_package(performance_test_fixture REQUIRED) + # Give cppcheck hints about macro definitions coming from outside this package + get_target_property(ament_cmake_cppcheck_ADDITIONAL_INCLUDE_DIRS + performance_test_fixture::performance_test_fixture INTERFACE_INCLUDE_DIRECTORIES) + find_package(ament_cmake_gtest REQUIRED) find_package(rcpputils REQUIRED) ament_add_gtest(test_logging_interface test/test_logging_interface.cpp) @@ -49,6 +54,10 @@ if(BUILD_TESTING) target_compile_definitions(test_logging_interface PUBLIC RCUTILS_ENABLE_FAULT_INJECTION) ament_target_dependencies(test_logging_interface rcpputils) endif() + add_performance_test(benchmark_logging_interface test/benchmark/benchmark_logging_interface.cpp) + if(TARGET benchmark_logging_interface) + target_link_libraries(benchmark_logging_interface ${PROJECT_NAME}) + endif() endif() ament_export_dependencies(ament_cmake rcl_logging_interface rcutils spdlog_vendor spdlog) diff --git a/rcl_logging_spdlog/package.xml b/rcl_logging_spdlog/package.xml index d05b5d6..373bc2e 100644 --- a/rcl_logging_spdlog/package.xml +++ b/rcl_logging_spdlog/package.xml @@ -21,6 +21,7 @@ ament_lint_auto ament_lint_common + performance_test_fixture rcpputils rcl_logging_packages diff --git a/rcl_logging_spdlog/test/benchmark/benchmark_logging_interface.cpp b/rcl_logging_spdlog/test/benchmark/benchmark_logging_interface.cpp new file mode 100644 index 0000000..8abe0b4 --- /dev/null +++ b/rcl_logging_spdlog/test/benchmark/benchmark_logging_interface.cpp @@ -0,0 +1,120 @@ +// Copyright 2020 Open Source Robotics Foundation, Inc. +// +// 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. + + +#include +#include + +#include + +#include + +#include "performance_test_fixture/performance_test_fixture.hpp" + +using performance_test_fixture::PerformanceTest; + +namespace +{ +constexpr const uint64_t kSize = 4096; +} + +class LoggingBenchmarkPerformance : public PerformanceTest +{ +public: + void SetUp(benchmark::State & st) + { + rcl_logging_ret_t ret = rcl_logging_external_initialize( + nullptr, + rcutils_get_default_allocator()); + if (ret != RCL_LOGGING_RET_OK) { + st.SkipWithError(rcutils_get_error_string().str); + } + + data = std::string(kSize, '0'); + PerformanceTest::SetUp(st); + } + void TearDown(benchmark::State & st) + { + PerformanceTest::TearDown(st); + rcl_logging_ret_t ret = rcl_logging_external_shutdown(); + if (ret != RCL_LOGGING_RET_OK) { + st.SkipWithError(rcutils_get_error_string().str); + } + } + + static void setLogLevel(int logger_level, benchmark::State & st) + { + rcl_logging_ret_t ret = rcl_logging_external_set_logger_level(nullptr, logger_level); + if (ret != RCL_LOGGING_RET_OK) { + st.SkipWithError(rcutils_get_error_string().str); + } + } + + std::string data; +}; + +BENCHMARK_F(LoggingBenchmarkPerformance, log_level_hit)(benchmark::State & st) +{ + setLogLevel(RCUTILS_LOG_SEVERITY_INFO, st); + + rcl_logging_external_log(RCUTILS_LOG_SEVERITY_INFO, nullptr, data.c_str()); + reset_heap_counters(); + + for (auto _ : st) { + rcl_logging_external_log(RCUTILS_LOG_SEVERITY_INFO, nullptr, data.c_str()); + } +} + +BENCHMARK_F(LoggingBenchmarkPerformance, log_level_miss)(benchmark::State & st) +{ + setLogLevel(RCUTILS_LOG_SEVERITY_INFO, st); + for (auto _ : st) { + rcl_logging_external_log(RCUTILS_LOG_SEVERITY_DEBUG, nullptr, data.c_str()); + } +} + +BENCHMARK_F(PerformanceTest, logging_reinitialize)(benchmark::State & st) +{ + rcutils_allocator_t allocator = rcutils_get_default_allocator(); + rcl_logging_ret_t ret = rcl_logging_external_initialize(nullptr, allocator); + + reset_heap_counters(); + + for (auto _ : st) { + ret = rcl_logging_external_initialize(nullptr, allocator); + if (ret != RCL_LOGGING_RET_OK) { + st.SkipWithError(rcutils_get_error_string().str); + } + } + + ret = rcl_logging_external_shutdown(); + if (ret != RCL_LOGGING_RET_OK) { + st.SkipWithError(rcutils_get_error_string().str); + } +} + +BENCHMARK_F(PerformanceTest, logging_initialize_shutdown)(benchmark::State & st) +{ + rcutils_allocator_t allocator = rcutils_get_default_allocator(); + for (auto _ : st) { + rcl_logging_ret_t ret = rcl_logging_external_initialize(nullptr, allocator); + if (ret != RCL_LOGGING_RET_OK) { + st.SkipWithError(rcutils_get_error_string().str); + } + ret = rcl_logging_external_shutdown(); + if (ret != RCL_LOGGING_RET_OK) { + st.SkipWithError(rcutils_get_error_string().str); + } + } +}