Skip to content

Commit

Permalink
Add benchmarks for components (#1476)
Browse files Browse the repository at this point in the history
Signed-off-by: Scott K Logan <logans@cottsay.net>
  • Loading branch information
cottsay committed Nov 25, 2020
1 parent f5e35bd commit 08963df
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 0 deletions.
12 changes: 12 additions & 0 deletions rclcpp_components/CMakeLists.txt
Expand Up @@ -65,6 +65,10 @@ endif()

if(BUILD_TESTING)
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()

set(components "")
Expand Down Expand Up @@ -109,6 +113,14 @@ if(BUILD_TESTING)
if(TARGET test_component_manager_api)
target_link_libraries(test_component_manager_api component_manager)
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()

install(
Expand Down
1 change: 1 addition & 0 deletions rclcpp_components/package.xml
Expand Up @@ -23,6 +23,7 @@
<exec_depend>composition_interfaces</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_lint_auto</test_depend>
<test_depend>ament_lint_common</test_depend>
Expand Down
121 changes: 121 additions & 0 deletions rclcpp_components/test/benchmark/benchmark_components.cpp
@@ -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();
}
}

0 comments on commit 08963df

Please sign in to comment.