From c4e50492624fd29dba1b3312c0ecd19d84bd8e61 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Sun, 8 Dec 2024 18:09:17 +0800 Subject: [PATCH] feat: benchmark suites of RawCode --- src/core/CMakeLists.txt | 4 ++ src/core/benchmark/raw_code.cc | 122 +++++++++++++++++++++++++++++++++ 2 files changed, 126 insertions(+) create mode 100644 src/core/benchmark/raw_code.cc diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index 54d9a0b..be0c551 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -53,6 +53,10 @@ if (KLSK_ENABLE_BENCHMARK) target_compile_options(bm_klsk_common_code PRIVATE ${KLSK_BENCHMARK_OPTS}) target_link_libraries(bm_klsk_common_code PRIVATE ${KLSK_BENCHMARK_LIBS}) + add_executable(bm_klsk_raw_code benchmark/raw_code.cc) + target_compile_options(bm_klsk_raw_code PRIVATE ${KLSK_BENCHMARK_OPTS}) + target_link_libraries(bm_klsk_raw_code PRIVATE ${KLSK_BENCHMARK_LIBS}) + add_executable(bm_klsk_group benchmark/group.cc) target_compile_options(bm_klsk_group PRIVATE ${KLSK_BENCHMARK_OPTS}) target_link_libraries(bm_klsk_group PRIVATE ${KLSK_BENCHMARK_LIBS}) diff --git a/src/core/benchmark/raw_code.cc b/src/core/benchmark/raw_code.cc new file mode 100644 index 0000000..62e2ac4 --- /dev/null +++ b/src/core/benchmark/raw_code.cc @@ -0,0 +1,122 @@ +#include + +#include "raw_code/raw_code.h" + +#include "all_cases/all_cases.h" + +using klotski::codec::RawCode; +using klotski::cases::AllCases; + +/// Build all valid CommonCodes. +static std::vector all_common_codes() { + std::vector codes; + for (uint64_t head = 0; head < 16; ++head) { + for (const auto range : AllCases::instance().fetch().ranges(head)) { + codes.emplace_back(head << 32 | range); + } + } + // std::cout << "do cal complete" << std::endl; + return codes; +} + +std::vector common_code_samples(uint64_t num) { + + static auto codes = all_common_codes(); + + uint64_t part_size = codes.size() / num; + + // uint64_t offset = 0; + uint64_t offset = part_size / 2; + + std::vector result; + + for (uint64_t i = 0; i < num; ++i) { + uint64_t index = i * part_size + offset; + result.emplace_back(codes[index]); + } + + return result; +} + +std::vector raw_code_samples(size_t num) { + std::vector raw_codes; + raw_codes.reserve(num); + + for (auto common_code : common_code_samples(num)) { + raw_codes.emplace_back(klotski::codec::CommonCode::unsafe_create(common_code).to_raw_code()); + } + + return raw_codes; +} + +static void RawCodeCheck(benchmark::State &state) { + const auto samples = raw_code_samples(state.range(0)); + for (auto _ : state) { + for (const auto code : samples) { + benchmark::DoNotOptimize(RawCode::check(code)); + } + } + state.SetItemsProcessed(state.iterations() * state.range(0)); +} + +static void RawCodeCompact(benchmark::State &state) { + const auto samples = raw_code_samples(state.range(0)); + for (auto _ : state) { + for (const auto code : samples) { + benchmark::DoNotOptimize(RawCode::compact(code)); + } + } + state.SetItemsProcessed(state.iterations() * state.range(0)); +} + +static void RawCodeExtract(benchmark::State &state) { + const auto samples = common_code_samples(state.range(0)); + for (auto _ : state) { + for (const auto code : samples) { + benchmark::DoNotOptimize(RawCode::extract(code)); + } + } + state.SetItemsProcessed(state.iterations() * state.range(0)); +} + +static void RawCodeCheckMirror(benchmark::State &state) { + const auto samples = raw_code_samples(state.range(0)); + for (auto _ : state) { + for (const auto code : samples) { + benchmark::DoNotOptimize(RawCode::check_mirror(code)); + } + } + state.SetItemsProcessed(state.iterations() * state.range(0)); +} + +static void RawCodeVerticalMirror(benchmark::State &state) { + const auto samples = raw_code_samples(state.range(0)); + for (auto _ : state) { + for (const auto code : samples) { + benchmark::DoNotOptimize(RawCode::get_vertical_mirror(code)); + } + } + state.SetItemsProcessed(state.iterations() * state.range(0)); +} + +static void RawCodeHorizontalMirror(benchmark::State &state) { + const auto samples = raw_code_samples(state.range(0)); + for (auto _ : state) { + for (const auto code : samples) { + benchmark::DoNotOptimize(RawCode::get_horizontal_mirror(code)); + } + } + state.SetItemsProcessed(state.iterations() * state.range(0)); +} + +// BENCHMARK(RawCodeCheck)->Range(8, 256); + +// BENCHMARK(RawCodeCompact)->RangeMultiplier(4)->Range(16, 1024); +// BENCHMARK(RawCodeExtract)->RangeMultiplier(4)->Range(16, 1024); + +// BENCHMARK(RawCodeCheckMirror)->RangeMultiplier(4)->Range(16, 1024); + +// BENCHMARK(RawCodeVerticalMirror)->RangeMultiplier(4)->Range(16, 1024); +BENCHMARK(RawCodeHorizontalMirror)->RangeMultiplier(4)->Range(16, 1024); + +BENCHMARK_MAIN();