Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Performance tests demo #288

Merged
merged 9 commits into from
Mar 11, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,12 @@ if(BUILD_TESTING)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14")
endif()

# BENCHMARK_F seems to break cppcheck 2.1
# set(ament_cmake_cppcheck_FOUND TRUE)

find_package(ament_cmake_google_benchmark REQUIRED)
find_package(performance_test_fixture REQUIRED)

find_package(ament_cmake_gmock REQUIRED)
find_package(ament_cmake_gtest REQUIRED)
find_package(ament_cmake_pytest REQUIRED)
Expand Down Expand Up @@ -505,6 +511,15 @@ if(BUILD_TESTING)
rcutils_custom_add_gtest(test_macros
test/test_macros.cpp
)

add_performance_test(benchmark_test_logging test/test_benchmark_logging.cpp)
target_link_libraries(benchmark_test_logging ${PROJECT_NAME})
target_include_directories(benchmark_test_logging PUBLIC include)

add_performance_test(benchmark_test_err_handle test/test_benchmark_error_handling.cpp)
target_link_libraries(benchmark_test_err_handle ${PROJECT_NAME})
target_include_directories(benchmark_test_err_handle PUBLIC include)

if(TARGET test_macros)
target_link_libraries(test_macros ${PROJECT_NAME})
endif()
Expand Down
2 changes: 2 additions & 0 deletions package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<buildtool_depend>ament_cmake_ros</buildtool_depend>
<buildtool_depend>python3-empy</buildtool_depend>

<test_depend>ament_cmake_google_benchmark</test_depend>
<test_depend>ament_cmake_gmock</test_depend>
<test_depend>ament_cmake_gtest</test_depend>
<test_depend>ament_cmake_pytest</test_depend>
Expand All @@ -20,6 +21,7 @@
<test_depend>launch_testing</test_depend>
<test_depend>launch_testing_ament_cmake</test_depend>
<test_depend>osrf_testing_tools_cpp</test_depend>
<test_depend>performance_test_fixture</test_depend>

<export>
<build_type>ament_cmake</build_type>
Expand Down
33 changes: 33 additions & 0 deletions test/test_benchmark_error_handling.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright 2016 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 <string>

#include "rcutils/error_handling.h"

#include <benchmark/benchmark.h>
#include <cassert>

static void benchmark_err_handling(benchmark::State & state) {
for (auto _ : state) {
rcutils_ret_t ret =
rcutils_initialize_error_handling_thread_local_storage(rcutils_get_default_allocator());
assert(ret == RCUTILS_RET_OK);
rcutils_reset_error();
const char * test_message = "test message";
RCUTILS_SET_ERROR_MSG(test_message);
}
}

BENCHMARK(benchmark_err_handling);
88 changes: 88 additions & 0 deletions test/test_benchmark_logging.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// 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 <string>
#include <vector>

#include "./allocator_testing_utils.h"
#include "osrf_testing_tools_cpp/scope_exit.hpp"
#include "rcutils/logging.h"

#include <benchmark/benchmark.h>
#include <cassert>

#ifdef RMW_IMPLEMENTATION
# define CLASSNAME_(NAME, SUFFIX) NAME ## __ ## SUFFIX
# define CLASSNAME(NAME, SUFFIX) CLASSNAME_(NAME, SUFFIX)
#else
# define CLASSNAME(NAME, SUFFIX) NAME
#endif

size_t g_log_calls = 0;
struct LogEvent
{
const rcutils_log_location_t * location;
int level;
std::string name;
rcutils_time_point_value_t timestamp;
std::string message;
};
LogEvent g_last_log_event;

static void simple_benchmark(benchmark::State & state) {
for (auto _ : state) {
rcutils_logging_initialize();
OSRF_TESTING_TOOLS_CPP_SCOPE_EXIT(
{
rcutils_logging_shutdown();
});
g_rcutils_logging_default_logger_level = RCUTILS_LOG_SEVERITY_DEBUG;

auto rcutils_logging_console_output_handler = [](
const rcutils_log_location_t * location,
int level, const char * name, rcutils_time_point_value_t timestamp,
const char * format, va_list * args) -> void
{
g_log_calls += 1;
g_last_log_event.location = location;
g_last_log_event.level = level;
g_last_log_event.name = name ? name : "";
g_last_log_event.timestamp = timestamp;
char buffer[1024];
vsnprintf(buffer, sizeof(buffer), format, *args);
g_last_log_event.message = buffer;
};

rcutils_logging_output_handler_t original_function = rcutils_logging_get_output_handler();
rcutils_logging_set_output_handler(rcutils_logging_console_output_handler);
int original_level = rcutils_logging_get_default_logger_level();

// check all attributes for a debug log message
rcutils_log_location_t location = {"func", "file", 42u};
rcutils_log(&location, RCUTILS_LOG_SEVERITY_DEBUG, "name1", "message %d", 11);
rcutils_logging_set_default_logger_level(RCUTILS_LOG_SEVERITY_ERROR);
rcutils_log(NULL, RCUTILS_LOG_SEVERITY_INFO, "name2", "message %d", 22);
rcutils_logging_set_default_logger_level(RCUTILS_LOG_SEVERITY_UNSET);
rcutils_log(NULL, RCUTILS_LOG_SEVERITY_INFO, "name3", "message %d", 33);
rcutils_log(NULL, RCUTILS_LOG_SEVERITY_WARN, "", "%s", "");
rcutils_log(NULL, RCUTILS_LOG_SEVERITY_ERROR, "", "%s", "");
rcutils_log(NULL, RCUTILS_LOG_SEVERITY_FATAL, NULL, "%s", "");

// restore original state
rcutils_logging_set_default_logger_level(original_level);
rcutils_logging_set_output_handler(original_function);
}
}

BENCHMARK(simple_benchmark);