集成性能分析

This commit is contained in:
yaha 2023-05-18 17:36:40 +08:00
parent 5df953460e
commit 47ae4cce3c
5 changed files with 111 additions and 10 deletions

View File

@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.25)
cmake_minimum_required(VERSION 3.24)
project(leetcode)
set(CMAKE_CXX_STANDARD 17)
@ -10,4 +10,9 @@ file(GLOB_RECURSE SOURCES "src/*/*.cpp")
add_library(${APP_LIB_NAME} ${SOURCES})
include(cmake/googletest.cmake)
option(WITH_PROFILE ON)
if (${WITH_PROFILE})
add_definitions(-DENABLE_PROFILING)
include(cmake/gperftools.cmake)
endif ()
include(cmake/tests.cmake)

View File

@ -1,9 +1,13 @@
message(STATUS "======= GoogleTest start ... =======")
include(FetchContent)
FetchContent_Declare(
googletest
URL https://github.com/google/googletest/archive/03597a01ee50ed33e9dfd640b249b4be3799d395.zip
)
# For Windows: Prevent overriding the parent project's compiler/linker settings
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(googletest)
include(GoogleTest)
include(GoogleTest)
message(STATUS "======= GoogleTest end =======")

42
cmake/gperftools.cmake Normal file
View File

@ -0,0 +1,42 @@
message(STATUS "======= gperftools starting ... =======")
##
#set(tcmalloc_BUILD_STATIC ON CACHE BOOL "" FORCE)
#set(gperftools_enable_libunwind OFF CACHE BOOL "" FORCE)
#set(gperftools_enable_frame_pointers ON CACHE BOOL "" FORCE)
#
include(FetchContent)
FetchContent_Declare(
gperftools
GIT_REPOSITORY https://github.com/gperftools/gperftools.git
GIT_TAG gperftools-2.10
# GIT_TAG master
)
FetchContent_GetProperties(gperftools)
if(NOT gperftools_POPULATED)
FetchContent_Populate(gperftools)
endif()
set(GPERFTOOLS_SOURCE_DIR ${gperftools_SOURCE_DIR})
set(GPERFTOOLS_INSTALL_DIR ${gperftools_BINARY_DIR})
execute_process(
COMMAND ${GPERFTOOLS_SOURCE_DIR}/autogen.sh
WORKING_DIRECTORY ${GPERFTOOLS_SOURCE_DIR}
)
execute_process(
COMMAND ${GPERFTOOLS_SOURCE_DIR}/configure --prefix=${GPERFTOOLS_INSTALL_DIR}
WORKING_DIRECTORY ${GPERFTOOLS_SOURCE_DIR}
)
execute_process(
COMMAND make -j6
WORKING_DIRECTORY ${GPERFTOOLS_SOURCE_DIR}
)
execute_process(
COMMAND make install
WORKING_DIRECTORY ${GPERFTOOLS_SOURCE_DIR}
)
message(STATUS "============ gperftools source: ${gperftools_SOURCE_DIR}")
message(STATUS "============ gperftools binary: ${gperftools_BINARY_DIR}")
message(STATUS "======= gperftools end =======")

View File

@ -1,9 +1,11 @@
set(APP_TEST_NAME leetcode_test)
message(STATUS =========== start testing APP_TEST_NAME: ${APP_TEST_NAME} ==========)
message(STATUS "=========== start testing APP_TEST_NAME: ${APP_TEST_NAME} ==========")
set(TEST_SOURCES)
file(GLOB_RECURSE TEST_SOURCES "tests/*.cpp")
message(STATUS =========== test_file: ${TEST_SOURCES} ==========)
message(STATUS "=========== test_file: ${TEST_SOURCES}")
include(GoogleTest)
enable_testing()
add_executable(
@ -12,11 +14,29 @@ add_executable(
${TEST_SOURCES}
)
target_link_libraries(
${APP_TEST_NAME}
${APP_TEST_NAME} PRIVATE
GTest::gtest_main
${APP_LIB_NAME}
)
include(GoogleTest)
if(${WITH_PROFILE})
message(STATUS "==== WITH_PROFILE: ON")
message(STATUS "==== gperftools_SOURCE_DIR: ${gperftools_SOURCE_DIR}/src")
# gperftools
# target_include_directories(
# ${APP_TEST_NAME} PRIVATE
# ${gperftools_SOURCE_DIR}/src
# )
include_directories(${gperftools_BINARY_DIR}/include)
file(GLOB GPERFTOOLS_LIBRARIES "${gperftools_BINARY_DIR}/lib/*.a")
message("Found libraries:")
foreach(LIBRARY ${GPERFTOOLS_LIBRARIES})
message(" ${LIBRARY}")
endforeach()
target_link_libraries(${APP_LIB_NAME} ${GPERFTOOLS_LIBRARIES})
endif()
gtest_discover_tests(${APP_TEST_NAME})
message(STATUS "========= end testing ========")

View File

@ -1,6 +1,36 @@
#include <gtest/gtest.h>
#ifdef ENABLE_PROFILING
#include "gperftools/profiler.h"
#include "gperftools/heap-profiler.h"
#endif
int main(int argc, char* argv[]) {
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
std::string get_timestamp() {
std::time_t now = std::time(nullptr);
std::tm timeInfo;
localtime_r(&now, &timeInfo);
char buffer[64];
std::strftime(buffer, sizeof(buffer), "./profile/feature_pipe_%Y%m%d", &timeInfo);
return std::string(buffer);
}
int main(int argc, char *argv[]) {
testing::InitGoogleTest(&argc, argv);
#ifdef ENABLE_PROFILING
std::string prof_name = get_timestamp() + ".prof";
std::string heap_name = get_timestamp() + ".heap";
ProfilerStart(prof_name.c_str());
HeapProfilerStart(heap_name.c_str());
#endif
int result = RUN_ALL_TESTS();
#ifdef ENABLE_PROFILING
ProfilerStop();
HeapProfilerStop();
#endif
return result;
}