Skip to content

Commit ad7ef7d

Browse files
committed
added benchmarking project.
1 parent 620b312 commit ad7ef7d

File tree

4 files changed

+135
-21
lines changed

4 files changed

+135
-21
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/bin")
99
add_subdirectory(CxxTestDesignPatternsUsingRTL)
1010
add_subdirectory(CxxRTLTypeRegistration)
1111
add_subdirectory(CxxRTLTestApplication)
12+
add_subdirectory(CxxRTLPerfTestApplication)
1213
add_subdirectory(CxxTestProps)
1314
add_subdirectory(CxxTestUtils)
1415
add_subdirectory(ReflectionTemplateLib)
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
cmake_minimum_required(VERSION 3.20)
2+
project(CxxPerfRTLTestApplication LANGUAGES CXX)
3+
4+
set(CMAKE_CXX_STANDARD 20)
5+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
6+
7+
# ===============================
8+
# Dependencies (Google Benchmark)
9+
# ===============================
10+
include(FetchContent)
11+
12+
FetchContent_Declare(
13+
benchmark
14+
GIT_REPOSITORY https://github.com/google/benchmark.git
15+
GIT_TAG v1.8.3
16+
)
17+
18+
# Prevent benchmark from adding extra projects
19+
set(BENCHMARK_ENABLE_TESTING OFF CACHE BOOL "" FORCE)
20+
set(BENCHMARK_ENABLE_INSTALL OFF CACHE BOOL "" FORCE)
21+
22+
FetchContent_GetProperties(benchmark)
23+
if(NOT benchmark_POPULATED)
24+
FetchContent_Populate(benchmark)
25+
add_subdirectory(${benchmark_SOURCE_DIR} ${benchmark_BINARY_DIR} EXCLUDE_FROM_ALL)
26+
endif()
27+
28+
# ===============================
29+
# Common Include Paths
30+
# ===============================
31+
set(RTL_INCLUDE_DIRS
32+
inc
33+
"${CMAKE_SOURCE_DIR}/CxxTestUtils/inc"
34+
"${CMAKE_SOURCE_DIR}/CxxRTLTypeRegistration/inc"
35+
"${CMAKE_SOURCE_DIR}/ReflectionTemplateLib/common"
36+
"${CMAKE_SOURCE_DIR}/ReflectionTemplateLib/detail/inc"
37+
"${CMAKE_SOURCE_DIR}/ReflectionTemplateLib/access/inc"
38+
"${CMAKE_SOURCE_DIR}/ReflectionTemplateLib/builder/inc"
39+
)
40+
41+
# ===============================
42+
# Benchmarks (only target)
43+
# ===============================
44+
add_executable(CxxPerfRTLTestApplication src/main_bench.cpp)
45+
46+
target_include_directories(CxxPerfRTLTestApplication PRIVATE ${RTL_INCLUDE_DIRS})
47+
48+
target_link_libraries(CxxPerfRTLTestApplication
49+
PRIVATE
50+
benchmark
51+
CxxTestUtils
52+
ReflectionTemplateLib
53+
CxxRTLTypeRegistration
54+
)
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
2+
#include <benchmark/benchmark.h>
3+
4+
#include "TestMirrorProvider.h"
5+
#include "GlobalTestUtils.h"
6+
#include "../../CxxTestProps/inc/Person.h"
7+
#include "../../CxxTestProps/inc/Complex.h"
8+
9+
// Direct call vs. Reflected call
10+
// ------------------------------------------------------------
11+
static void DirectCall(benchmark::State& state)
12+
{
13+
Person obj;
14+
for (auto _ : state) {
15+
benchmark::DoNotOptimize(complex::getMagnitude());
16+
}
17+
}
18+
19+
static void ReflectedCall(benchmark::State& state)
20+
{
21+
rtl::Function getMagnitude = test_mirror::cxx().mirror().getFunction(test_utils::str_complex,test_utils::str_getMagnitude).value();
22+
for (auto _ : state) {;
23+
benchmark::DoNotOptimize(getMagnitude.bind().call());
24+
}
25+
}
26+
27+
28+
// ------------------------------------------------------------
29+
// Register benchmarks
30+
// ------------------------------------------------------------
31+
BENCHMARK(DirectCall);
32+
BENCHMARK(ReflectedCall);
33+
34+
BENCHMARK_MAIN();
Lines changed: 46 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,63 @@
1-
# CMakeLists.txt for CxxReflectionTests
21
cmake_minimum_required(VERSION 3.20)
2+
project(CxxRTLTestApplication LANGUAGES CXX)
33

44
set(CMAKE_CXX_STANDARD 20)
5+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
56

6-
project(CxxRTLTestApplication)
7-
8-
set(CXX_EXE_NAME CxxRTLTestApplication)
9-
add_executable(${CXX_EXE_NAME} "")
10-
11-
7+
# ===============================
8+
# Dependencies (GoogleTest)
9+
# ===============================
1210
include(FetchContent)
11+
1312
FetchContent_Declare(
1413
googletest
1514
URL https://github.com/google/googletest/archive/refs/tags/v1.14.0.zip
1615
)
17-
# For Windows: Prevent overriding the parent project's compiler/linker settings
16+
17+
# Prevent GTest from overriding CRT settings on Windows
1818
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
19+
20+
# Avoid GTest adding its own tests into the solution
21+
set(INSTALL_GTEST OFF CACHE BOOL "" FORCE)
22+
set(BUILD_GMOCK OFF CACHE BOOL "" FORCE)
23+
set(BUILD_GTEST ON CACHE BOOL "" FORCE)
24+
1925
FetchContent_MakeAvailable(googletest)
2026

21-
include_directories(inc)
22-
include_directories("${CMAKE_SOURCE_DIR}/CxxTestUtils/inc")
23-
include_directories("${CMAKE_SOURCE_DIR}/CxxRTLTypeRegistration/inc")
24-
include_directories("${CMAKE_SOURCE_DIR}/ReflectionTemplateLib/common")
25-
INCLUDE_DIRECTORIES("${CMAKE_SOURCE_DIR}/ReflectionTemplateLib/detail/inc")
26-
include_directories("${CMAKE_SOURCE_DIR}/ReflectionTemplateLib/access/inc")
27-
include_directories("${CMAKE_SOURCE_DIR}/ReflectionTemplateLib/builder/inc")
27+
# ===============================
28+
# Common Include Paths
29+
# ===============================
30+
set(RTL_INCLUDE_DIRS
31+
inc
32+
"${CMAKE_SOURCE_DIR}/CxxTestUtils/inc"
33+
"${CMAKE_SOURCE_DIR}/CxxRTLTypeRegistration/inc"
34+
"${CMAKE_SOURCE_DIR}/ReflectionTemplateLib/common"
35+
"${CMAKE_SOURCE_DIR}/ReflectionTemplateLib/detail/inc"
36+
"${CMAKE_SOURCE_DIR}/ReflectionTemplateLib/access/inc"
37+
"${CMAKE_SOURCE_DIR}/ReflectionTemplateLib/builder/inc"
38+
)
39+
40+
# ===============================
41+
# Test Executable
42+
# ===============================
43+
set(CXX_EXE_NAME CxxRTLTestApplication)
44+
45+
# Add all test sources (either glob or include your src/CMakeLists.txt)
46+
file(GLOB_RECURSE TEST_SOURCES CONFIGURE_DEPENDS src/*.cpp)
47+
add_executable(${CXX_EXE_NAME} ${TEST_SOURCES})
2848

29-
target_link_libraries(${CXX_EXE_NAME} CxxTestUtils)
30-
target_link_libraries(${CXX_EXE_NAME} GTest::gtest_main)
31-
target_link_libraries(${CXX_EXE_NAME} ReflectionTemplateLib)
32-
target_link_libraries(${CXX_EXE_NAME} CxxRTLTypeRegistration)
49+
target_include_directories(${CXX_EXE_NAME} PRIVATE ${RTL_INCLUDE_DIRS})
3350

34-
# Add the source directory
35-
include(src/CMakeLists.txt)
51+
target_link_libraries(${CXX_EXE_NAME}
52+
PRIVATE
53+
CxxTestUtils
54+
ReflectionTemplateLib
55+
CxxRTLTypeRegistration
56+
GTest::gtest_main
57+
)
3658

59+
# ===============================
60+
# GoogleTest Integration
61+
# ===============================
3762
include(GoogleTest)
3863
gtest_discover_tests(${CXX_EXE_NAME})

0 commit comments

Comments
 (0)