diff --git a/cetlvast/CMakeLists.txt b/cetlvast/CMakeLists.txt index 13f3ba7..c9af5a4 100644 --- a/cetlvast/CMakeLists.txt +++ b/cetlvast/CMakeLists.txt @@ -162,6 +162,7 @@ add_project_library( ) find_package(clangformat) +find_package(benchmark) if(clangformat_FOUND) # define a dry-run version that we always run. @@ -198,7 +199,9 @@ endif() add_subdirectory(${CMAKE_SOURCE_DIR}/suites/unittest) add_subdirectory(${CMAKE_SOURCE_DIR}/suites/docs) add_subdirectory(${CMAKE_SOURCE_DIR}/suites/compile) - +if(benchmark_FOUND) + add_subdirectory(${CMAKE_SOURCE_DIR}/suites/benchmark) +endif() # +---------------------------------------------------------------------------+ # | BUILD TARGET ALIASES # +---------------------------------------------------------------------------+ diff --git a/cetlvast/suites/benchmark/CMakeLists.txt b/cetlvast/suites/benchmark/CMakeLists.txt new file mode 100644 index 0000000..ee975b5 --- /dev/null +++ b/cetlvast/suites/benchmark/CMakeLists.txt @@ -0,0 +1,36 @@ +# +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +# SPDX-License-Identifier: MIT +# + +cmake_minimum_required(VERSION 3.22.0) + +project(cetlvast_benchmark CXX) + +find_package(benchmark REQUIRED) + +file(GLOB_RECURSE TEST_SOURCES + LIST_DIRECTORIES false + CONFIGURE_DEPENDS + RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} + benchmark_*.cpp **/benchmark_*.cpp +) + +set(ALL_TESTS_BUILD "") + +foreach(TEST_SOURCE ${TEST_SOURCES}) + + cmake_path(GET TEST_SOURCE STEM TEST_NAME) + + add_executable(${TEST_NAME} ${TEST_SOURCE}) + target_link_libraries(${TEST_NAME} cetl benchmark::benchmark) + + list(APPEND ALL_TESTS_BUILD ${TEST_NAME}) + +endforeach() + +add_custom_target( + build_benchmarks + DEPENDS + ${ALL_TESTS_BUILD} +) diff --git a/cetlvast/suites/benchmark/pmr/benchmark_pmr_function.cpp b/cetlvast/suites/benchmark/pmr/benchmark_pmr_function.cpp new file mode 100644 index 0000000..fd68275 --- /dev/null +++ b/cetlvast/suites/benchmark/pmr/benchmark_pmr_function.cpp @@ -0,0 +1,69 @@ +#include + +#include + +namespace +{ + +void BM_CetlFn_call(benchmark::State& state) +{ + cetl::pmr::function fn = [](int64_t i) { + benchmark::DoNotOptimize(i); + return i; + }; + for (auto _ : state) + { + int64_t res = 0; + const auto lim = state.range(); + for (int i = 0; i < lim; ++i) + { + res += fn(i); + } + benchmark::DoNotOptimize(res); + } +} + +void BM_StdFn_call(benchmark::State& state) +{ + std::function fn = [](int64_t i) { + benchmark::DoNotOptimize(i); + return i; + }; + for (auto _ : state) + { + int64_t res = 0; + const auto lim = state.range(); + for (int i = 0; i < lim; ++i) + { + res += fn(i); + } + benchmark::DoNotOptimize(res); + } +} + +void BM_Lambda_call(benchmark::State& state) +{ + auto fn = [](int64_t i) { + benchmark::DoNotOptimize(i); + return i; + }; + for (auto _ : state) + { + int64_t res = 0; + const auto lim = state.range(); + for (int i = 0; i < lim; ++i) + { + res += fn(i); + } + benchmark::DoNotOptimize(res); + } + (void) fn; +} + +} // namespace + +BENCHMARK(BM_CetlFn_call)->Arg(1000); +BENCHMARK(BM_StdFn_call)->Arg(1000); +BENCHMARK(BM_Lambda_call)->Arg(1000); + +BENCHMARK_MAIN();