Add benchmarks for components (#1479)
Cherry-picked from 08963df92644a5de963d80bd206b4185de77d822 Signed-off-by: Scott K Logan <logans@cottsay.net>
This commit is contained in:
parent
bea9c5a8f6
commit
965b4d2c24
3 changed files with 134 additions and 0 deletions
|
@ -65,6 +65,10 @@ endif()
|
||||||
|
|
||||||
if(BUILD_TESTING)
|
if(BUILD_TESTING)
|
||||||
find_package(ament_lint_auto REQUIRED)
|
find_package(ament_lint_auto REQUIRED)
|
||||||
|
find_package(ament_cmake_google_benchmark REQUIRED)
|
||||||
|
find_package(benchmark REQUIRED)
|
||||||
|
# Give cppcheck hints about macro definitions coming from outside this package
|
||||||
|
get_target_property(ament_cmake_cppcheck_ADDITIONAL_INCLUDE_DIRS benchmark::benchmark INTERFACE_INCLUDE_DIRECTORIES)
|
||||||
ament_lint_auto_find_test_dependencies()
|
ament_lint_auto_find_test_dependencies()
|
||||||
|
|
||||||
set(components "")
|
set(components "")
|
||||||
|
@ -109,6 +113,14 @@ if(BUILD_TESTING)
|
||||||
if(TARGET test_component_manager_api)
|
if(TARGET test_component_manager_api)
|
||||||
target_link_libraries(test_component_manager_api component_manager)
|
target_link_libraries(test_component_manager_api component_manager)
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
|
ament_add_google_benchmark(benchmark_components
|
||||||
|
test/benchmark/benchmark_components.cpp
|
||||||
|
APPEND_ENV AMENT_PREFIX_PATH=${CMAKE_CURRENT_BINARY_DIR}/test_ament_index/$<CONFIG>
|
||||||
|
APPEND_LIBRARY_DIRS "${append_library_dirs}")
|
||||||
|
if(TARGET benchmark_components)
|
||||||
|
target_link_libraries(benchmark_components component_manager)
|
||||||
|
endif()
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
install(
|
install(
|
||||||
|
|
|
@ -20,6 +20,7 @@
|
||||||
<exec_depend>composition_interfaces</exec_depend>
|
<exec_depend>composition_interfaces</exec_depend>
|
||||||
<exec_depend>rclcpp</exec_depend>
|
<exec_depend>rclcpp</exec_depend>
|
||||||
|
|
||||||
|
<test_depend>ament_cmake_google_benchmark</test_depend>
|
||||||
<test_depend>ament_cmake_gtest</test_depend>
|
<test_depend>ament_cmake_gtest</test_depend>
|
||||||
<test_depend>ament_lint_auto</test_depend>
|
<test_depend>ament_lint_auto</test_depend>
|
||||||
<test_depend>ament_lint_common</test_depend>
|
<test_depend>ament_lint_common</test_depend>
|
||||||
|
|
121
rclcpp_components/test/benchmark/benchmark_components.cpp
Normal file
121
rclcpp_components/test/benchmark/benchmark_components.cpp
Normal file
|
@ -0,0 +1,121 @@
|
||||||
|
// 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 "benchmark/benchmark.h"
|
||||||
|
|
||||||
|
#include <rcutils/logging.h>
|
||||||
|
|
||||||
|
#include <memory>
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
#include "rclcpp_components/component_manager.hpp"
|
||||||
|
|
||||||
|
class ComponentTest : public benchmark::Fixture
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
ComponentTest()
|
||||||
|
: component_manager_name("my_manager")
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifdef __GNUC__
|
||||||
|
#pragma GCC diagnostic push
|
||||||
|
#pragma GCC diagnostic ignored "-Woverloaded-virtual"
|
||||||
|
#endif
|
||||||
|
void SetUp(benchmark::State &) override
|
||||||
|
{
|
||||||
|
rcutils_logging_set_default_logger_level(RCUTILS_LOG_SEVERITY_WARN);
|
||||||
|
|
||||||
|
context = std::make_shared<rclcpp::Context>();
|
||||||
|
context->init(0, nullptr, rclcpp::InitOptions().auto_initialize_logging(false));
|
||||||
|
|
||||||
|
rclcpp::ExecutorOptions exec_options;
|
||||||
|
exec_options.context = context;
|
||||||
|
|
||||||
|
executor = std::make_shared<rclcpp::executors::SingleThreadedExecutor>(exec_options);
|
||||||
|
|
||||||
|
manager = std::make_shared<rclcpp_components::ComponentManager>(
|
||||||
|
executor, component_manager_name, rclcpp::NodeOptions().context(context));
|
||||||
|
executor->add_node(manager);
|
||||||
|
}
|
||||||
|
|
||||||
|
void TearDown(benchmark::State &) override
|
||||||
|
{
|
||||||
|
context->shutdown("Test is complete");
|
||||||
|
|
||||||
|
manager.reset();
|
||||||
|
executor.reset();
|
||||||
|
context.reset();
|
||||||
|
}
|
||||||
|
#ifdef __GNUC__
|
||||||
|
#pragma GCC diagnostic pop
|
||||||
|
#endif
|
||||||
|
|
||||||
|
const std::string component_manager_name;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
rclcpp::Context::SharedPtr context;
|
||||||
|
rclcpp::executors::SingleThreadedExecutor::SharedPtr executor;
|
||||||
|
std::shared_ptr<rclcpp_components::ComponentManager> manager;
|
||||||
|
};
|
||||||
|
|
||||||
|
BENCHMARK_F(ComponentTest, get_component_resources)(benchmark::State & state)
|
||||||
|
{
|
||||||
|
for (auto _ : state) {
|
||||||
|
std::vector<rclcpp_components::ComponentManager::ComponentResource> resources =
|
||||||
|
manager->get_component_resources("rclcpp_components");
|
||||||
|
if (resources.size() != 3) {
|
||||||
|
state.SkipWithError("Wrong number of components found");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
BENCHMARK_F(ComponentTest, create_component_factory)(benchmark::State & state)
|
||||||
|
{
|
||||||
|
const std::vector<rclcpp_components::ComponentManager::ComponentResource> resources =
|
||||||
|
manager->get_component_resources("rclcpp_components");
|
||||||
|
if (resources.size() != 3) {
|
||||||
|
state.SkipWithError("Wrong number of components found");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (auto _ : state) {
|
||||||
|
manager->create_component_factory(resources[0]).reset();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
BENCHMARK_F(ComponentTest, create_node_instance)(benchmark::State & state)
|
||||||
|
{
|
||||||
|
const std::vector<rclcpp_components::ComponentManager::ComponentResource> resources =
|
||||||
|
manager->get_component_resources("rclcpp_components");
|
||||||
|
if (resources.size() != 3) {
|
||||||
|
state.SkipWithError("Wrong number of components found");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Choosing resource 0 - the other two test components were shown empirically to yield
|
||||||
|
// the same performance charactarisitics, so they shouldn't need their own benchmarks.
|
||||||
|
const std::shared_ptr<rclcpp_components::NodeFactory> factory =
|
||||||
|
manager->create_component_factory(resources[0]);
|
||||||
|
|
||||||
|
const rclcpp::NodeOptions options = rclcpp::NodeOptions().context(context);
|
||||||
|
|
||||||
|
for (auto _ : state) {
|
||||||
|
rclcpp_components::NodeInstanceWrapper node = factory->create_node_instance(options);
|
||||||
|
benchmark::DoNotOptimize(node);
|
||||||
|
benchmark::ClobberMemory();
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue