Skip to content

Commit 8fdf2ab

Browse files
committed
Compare performance of scan hint/alignment against Chromium
1 parent 96627c7 commit 8fdf2ab

4 files changed

Lines changed: 152 additions & 21 deletions

File tree

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ option(LIBHAT_DISABLE_AVX512 "Disables AVX512 scanning" OFF)
2222
option(LIBHAT_TESTING "Enable tests" ${PROJECT_IS_TOP_LEVEL})
2323
option(LIBHAT_TESTING_ASAN "Enable address sanitizer when testing" ON)
2424
option(LIBHAT_TESTING_SDE "Run tests using Intel Software Development Emulator" ON)
25+
option(LIBHAT_TESTING_SAMPLE_BIN "Enables tests that require downloading a large sample binary" ON)
2526
option(LIBHAT_MODULE_TARGET "Create target for the module interface" OFF)
2627
option(LIBHAT_USE_STD_MODULE "Compile the module target using the std module" OFF)
2728
option(LIBHAT_EXAMPLES "Include example targets" ${PROJECT_IS_TOP_LEVEL})

test/CMakeLists.txt

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
project(libhat_tests)
22

3-
include(FetchContent)
3+
if (NOT CPM_SOURCE_CACHE AND NOT ENV{CPM_SOURCE_CACHE})
4+
set(CPM_SOURCE_CACHE ${PROJECT_SOURCE_DIR}/.cpmcache)
5+
endif()
6+
7+
include(cmake/get_cpm.cmake)
48

59
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
610
set(BENCHMARK_ENABLE_TESTING OFF CACHE BOOL "" FORCE)
@@ -9,41 +13,37 @@ if(POLICY CMP0135)
913
cmake_policy(SET CMP0135 NEW)
1014
endif()
1115

12-
FetchContent_Declare(
13-
googletest
14-
GIT_REPOSITORY https://github.com/google/googletest.git
15-
GIT_TAG 96cd50c082d880a9aab6455dcc5817cfbf0ea45f
16-
)
17-
FetchContent_Declare(
18-
benchmark
19-
GIT_REPOSITORY https://github.com/google/benchmark.git
20-
GIT_TAG 4682db08bc7bb7e547e0a1056e32392998f8101f
21-
)
22-
FetchContent_MakeAvailable(googletest benchmark)
16+
CPMAddPackage("gh:google/googletest#v1.17.0")
17+
CPMAddPackage("gh:google/benchmark#v1.9.4")
18+
19+
if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang" AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 21)
20+
target_compile_options(gtest PRIVATE "-Wno-character-conversion")
21+
endif()
2322

2423
if(LIBHAT_TESTING_SDE)
2524
set(LIBHAT_SETUP_SDE OFF)
2625
if(WIN32)
27-
FetchContent_Declare(
28-
intel_sde
29-
URL https://downloadmirror.intel.com/831748/sde-external-9.44.0-2024-08-22-win.tar.xz
30-
URL_HASH SHA256=ED1562840510ABEB958DFE4DFBFF5BEA9A960A0075839222E984393425E15FBF
26+
CPMAddPackage(
27+
NAME intel_sde
28+
URL https://downloadmirror.intel.com/831748/sde-external-9.44.0-2024-08-22-win.tar.xz
29+
URL_HASH SHA256=ED1562840510ABEB958DFE4DFBFF5BEA9A960A0075839222E984393425E15FBF
30+
DOWNLOAD_ONLY TRUE
3131
)
3232
set(LIBHAT_SETUP_SDE ON)
3333
set(LIBHAT_SDE_EXECUTABLE sde.exe)
3434
elseif(UNIX)
35-
FetchContent_Declare(
36-
intel_sde
37-
URL https://downloadmirror.intel.com/831748/sde-external-9.44.0-2024-08-22-lin.tar.xz
38-
URL_HASH SHA256=C6BC16FC6D1855049EA22DAF214A8C00331713BC6A7B0C4D6955D6ED84148B9B
35+
CPMAddPackage(
36+
NAME intel_sde
37+
URL https://downloadmirror.intel.com/831748/sde-external-9.44.0-2024-08-22-lin.tar.xz
38+
URL_HASH SHA256=C6BC16FC6D1855049EA22DAF214A8C00331713BC6A7B0C4D6955D6ED84148B9B
39+
DOWNLOAD_ONLY TRUE
3940
)
4041
set(LIBHAT_SETUP_SDE ON)
4142
set(LIBHAT_SDE_EXECUTABLE sde64)
4243
else()
4344
message("Intel SDE not supported by host platform, testing support may be limited" WARNING)
4445
endif()
4546
if(LIBHAT_SETUP_SDE)
46-
FetchContent_MakeAvailable(intel_sde)
4747
set(LIBHAT_TEST_COMMAND_PREFIX
4848
${intel_sde_SOURCE_DIR}/${LIBHAT_SDE_EXECUTABLE}
4949
-gnr # Supports AVX512 emulation
@@ -61,5 +61,17 @@ endfunction()
6161
register_test(libhat_benchmark_compare benchmark/Compare.cpp)
6262
register_test(libhat_test_scanner tests/Scanner.cpp)
6363

64+
if(LIBHAT_TESTING_SAMPLE_BIN)
65+
CPMAddPackage(
66+
NAME chromium_win_x64
67+
URL https://www.googleapis.com/download/storage/v1/b/chromium-browser-snapshots/o/Win_x64%2F1533392%2Fchrome-win.zip?generation=1761109878458576&alt=media
68+
URL_HASH SHA256=C53AC5F55C8EC6CD61E90D3D50DB7AF05D17708EBCD3141E0008CBE2F82DF5BF
69+
DOWNLOAD_ONLY TRUE
70+
)
71+
register_test(libhat_benchmark_chromium benchmark/Chromium.cpp)
72+
set(CHROME_DLL_PATH "${chromium_win_x64_SOURCE_DIR}/chrome.dll")
73+
target_compile_definitions(libhat_benchmark_chromium PRIVATE CHROME_DLL_PATH=${CHROME_DLL_PATH})
74+
endif()
75+
6476
add_executable(libhat_hwinfo info/HardwareInfo.cpp)
6577
target_link_libraries(libhat_hwinfo PRIVATE libhat)

test/benchmark/Chromium.cpp

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
#include <fstream>
2+
3+
#include <benchmark/benchmark.h>
4+
#include <libhat/scanner.hpp>
5+
6+
#include <format>
7+
8+
#define WIDE_STR_(x) L ## #x
9+
#define WIDE_STR(x) WIDE_STR_(x)
10+
11+
static constexpr auto DllMainSignature = hat::compile_signature<"48 89 5C 24 08 48 89 74 24 10 57 48 83 EC ?? 49 8B F8">();
12+
13+
static std::span<const std::byte> get_file_data() {
14+
static std::vector<std::byte> data = []{
15+
std::ifstream file(WIDE_STR(CHROME_DLL_PATH), std::ios::binary);
16+
if (!file.is_open()) {
17+
std::terminate();
18+
}
19+
20+
file.seekg(0, std::ios::end);
21+
const std::streampos fileSize = file.tellg();
22+
file.seekg(0, std::ios::beg);
23+
24+
std::vector<std::byte> contents(static_cast<size_t>(fileSize));
25+
file.read(reinterpret_cast<char*>(contents.data()), fileSize);
26+
return contents;
27+
}();
28+
return data;
29+
}
30+
31+
static void BM_find(benchmark::State& state) {
32+
const auto buf = get_file_data();
33+
34+
hat::const_scan_result result;
35+
for (auto _ : state) {
36+
benchmark::DoNotOptimize(result = hat::find_pattern(buf, DllMainSignature));
37+
}
38+
if (!result.has_result()) {
39+
std::terminate();
40+
}
41+
state.SetBytesProcessed(state.iterations() * (result.get() - buf.data()));
42+
}
43+
44+
static void BM_find_align(benchmark::State& state) {
45+
const auto buf = get_file_data();
46+
47+
hat::const_scan_result result;
48+
for (auto _ : state) {
49+
benchmark::DoNotOptimize(result = hat::find_pattern(buf, DllMainSignature, hat::scan_alignment::X16));
50+
}
51+
if (!result.has_result()) {
52+
std::terminate();
53+
}
54+
state.SetBytesProcessed(state.iterations() * (result.get() - buf.data()));
55+
}
56+
57+
static void BM_find_hint(benchmark::State& state) {
58+
const auto buf = get_file_data();
59+
60+
hat::const_scan_result result;
61+
for (auto _ : state) {
62+
benchmark::DoNotOptimize(result = hat::find_pattern(buf, DllMainSignature, hat::scan_alignment::X1, hat::scan_hint::x86_64));
63+
}
64+
if (!result.has_result()) {
65+
std::terminate();
66+
}
67+
state.SetBytesProcessed(state.iterations() * (result.get() - buf.data()));
68+
}
69+
70+
static void BM_find_align_hint(benchmark::State& state) {
71+
const auto buf = get_file_data();
72+
73+
hat::const_scan_result result;
74+
for (auto _ : state) {
75+
benchmark::DoNotOptimize(result = hat::find_pattern(buf, DllMainSignature, hat::scan_alignment::X16, hat::scan_hint::x86_64));
76+
}
77+
if (!result.has_result()) {
78+
std::terminate();
79+
}
80+
state.SetBytesProcessed(state.iterations() * (result.get() - buf.data()));
81+
}
82+
83+
#define LIBHAT_BENCHMARK(...) BENCHMARK(__VA_ARGS__) \
84+
->Threads(1) \
85+
->MinWarmUpTime(2) \
86+
->MinTime(4) \
87+
->UseRealTime();
88+
89+
LIBHAT_BENCHMARK(BM_find);
90+
LIBHAT_BENCHMARK(BM_find_align);
91+
LIBHAT_BENCHMARK(BM_find_hint);
92+
LIBHAT_BENCHMARK(BM_find_align_hint);
93+
94+
BENCHMARK_MAIN();

test/cmake/get_cpm.cmake

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# SPDX-License-Identifier: MIT
2+
#
3+
# SPDX-FileCopyrightText: Copyright (c) 2019-2023 Lars Melchior and contributors
4+
5+
set(CPM_DOWNLOAD_VERSION 0.42.0)
6+
set(CPM_HASH_SUM "2020b4fc42dba44817983e06342e682ecfc3d2f484a581f11cc5731fbe4dce8a")
7+
8+
if(CPM_SOURCE_CACHE)
9+
set(CPM_DOWNLOAD_LOCATION "${CPM_SOURCE_CACHE}/cpm/CPM_${CPM_DOWNLOAD_VERSION}.cmake")
10+
elseif(DEFINED ENV{CPM_SOURCE_CACHE})
11+
set(CPM_DOWNLOAD_LOCATION "$ENV{CPM_SOURCE_CACHE}/cpm/CPM_${CPM_DOWNLOAD_VERSION}.cmake")
12+
else()
13+
set(CPM_DOWNLOAD_LOCATION "${CMAKE_BINARY_DIR}/cmake/CPM_${CPM_DOWNLOAD_VERSION}.cmake")
14+
endif()
15+
16+
# Expand relative path. This is important if the provided path contains a tilde (~)
17+
get_filename_component(CPM_DOWNLOAD_LOCATION ${CPM_DOWNLOAD_LOCATION} ABSOLUTE)
18+
19+
file(DOWNLOAD
20+
https://github.com/cpm-cmake/CPM.cmake/releases/download/v${CPM_DOWNLOAD_VERSION}/CPM.cmake
21+
${CPM_DOWNLOAD_LOCATION} EXPECTED_HASH SHA256=${CPM_HASH_SUM}
22+
)
23+
24+
include(${CPM_DOWNLOAD_LOCATION})

0 commit comments

Comments
 (0)