|
| 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(); |
0 commit comments