From dcfe37dc6df72be275800b45770a7a25c1d5d9a0 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Sun, 8 Dec 2024 16:47:33 +0800 Subject: [PATCH] refactor: benchmark suites of CommonCode --- src/core/CMakeLists.txt | 4 + src/core/benchmark/codec.cc | 80 -------------------- src/core/benchmark/common_code.cc | 118 ++++++++++++++++++++++++++++++ 3 files changed, 122 insertions(+), 80 deletions(-) create mode 100644 src/core/benchmark/common_code.cc diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index 16429c3..54d9a0b 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -49,6 +49,10 @@ if (KLSK_ENABLE_BENCHMARK) target_compile_options(bm_klsk_codec PRIVATE ${KLSK_BENCHMARK_OPTS}) target_link_libraries(bm_klsk_codec PRIVATE ${KLSK_BENCHMARK_LIBS}) + add_executable(bm_klsk_common_code benchmark/common_code.cc) + 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_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/codec.cc b/src/core/benchmark/codec.cc index b7422bc..69b6b13 100644 --- a/src/core/benchmark/codec.cc +++ b/src/core/benchmark/codec.cc @@ -2,11 +2,9 @@ #include -// #define private public #include "group/group.h" #include "all_cases/all_cases.h" #include "common_code/common_code.h" -// #undef private using klotski::codec::ShortCode; using klotski::codec::CommonCode; @@ -102,64 +100,6 @@ std::vector str_short_codes(uint64_t num) { return codes; } -static void CommonCodeSerialize(benchmark::State &state) { - - // common_code_samples(8); - - auto samples = common_code_samples(state.range(0)); - - for (auto _ : state) { - - for (auto code : samples) { - - volatile auto ret = klotski::codec::CommonCode::string_encode(code); - } - - } - - state.SetItemsProcessed(state.iterations() * state.range(0)); -} - -static void CommonCodeDeserialize(benchmark::State &state) { - const auto tmp = str_common_codes(state.range(0), false); - const std::vector samples {tmp.begin(), tmp.end()}; - - for (auto _ : state) { - for (const auto code : samples) { - benchmark::DoNotOptimize(CommonCode::string_decode(code)); - } - } - state.SetItemsProcessed(state.iterations() * state.range(0)); - -} - -static void CommonCodeSerializeShorten(benchmark::State &state) { - - // auto samples = select_codes(state.range(0)); - auto samples = common_code_samples(state.range(0)); - - for (auto _ : state) { - for (auto code : samples) { - volatile auto ret = klotski::codec::CommonCode::string_encode_shorten(code); - } - } - state.SetItemsProcessed(state.iterations() * state.range(0)); - -} - -static void CommonCodeDeserializeShorten(benchmark::State &state) { - const auto tmp = str_common_codes(state.range(0), true); - const std::vector samples {tmp.begin(), tmp.end()}; - - for (auto _ : state) { - for (const auto code : samples) { - benchmark::DoNotOptimize(CommonCode::string_decode(code)); - } - } - state.SetItemsProcessed(state.iterations() * state.range(0)); - -} - static void ShortCodeSerialize(benchmark::State &state) { std::vector samples = short_code_samples(state.range(0)); @@ -260,26 +200,6 @@ static void IsMirrorCompare(benchmark::State &state) { } } -static void CommonCodeCheck(benchmark::State &state) { - - std::vector samples = common_code_samples(state.range(0)); - - for (auto _ : state) { - for (auto code : samples) { - volatile auto tmp = CommonCode::check(code); - } - } - - state.SetItemsProcessed(state.iterations() * state.range(0)); -} - -BENCHMARK(CommonCodeCheck)->Range(64, 1024); - -// BENCHMARK(CommonCodeSerialize)->Range(8, 256); -//BENCHMARK(CommonCodeDeserialize)->Range(8, 256); -// BENCHMARK(CommonCodeSerializeShorten)->Range(8, 256); -// BENCHMARK(CommonCodeDeserializeShorten)->Range(8, 256); - //BENCHMARK(ShortCodeSerialize)->Range(8, 256); // BENCHMARK(ShortCodeDeserialize)->Range(8, 256); diff --git a/src/core/benchmark/common_code.cc b/src/core/benchmark/common_code.cc new file mode 100644 index 0000000..8c54c8e --- /dev/null +++ b/src/core/benchmark/common_code.cc @@ -0,0 +1,118 @@ +#include + +#include "common_code/common_code.h" + +#include "all_cases/all_cases.h" + +using klotski::cases::AllCases; +using klotski::codec::CommonCode; + +/// 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 str_common_codes(uint64_t num, bool shorten) { + // auto src = select_codes(num); + auto src = common_code_samples(num); + + std::vector codes; + + codes.reserve(src.size()); + for (auto x : src) { + codes.emplace_back(klotski::codec::CommonCode::unsafe_create(x).to_string(shorten)); + } + + return codes; +} + +static void CommonCodeCheck(benchmark::State &state) { + const auto samples = common_code_samples(state.range(0)); + for (auto _ : state) { + for (const auto code : samples) { + volatile auto tmp = CommonCode::check(code); + } + } + state.SetItemsProcessed(state.iterations() * state.range(0)); +} + +static void CommonCodeSerialize(benchmark::State &state) { + const auto samples = common_code_samples(state.range(0)); + for (auto _ : state) { + for (const auto code : samples) { + // volatile auto ret = CommonCode::string_encode(code); + benchmark::DoNotOptimize(CommonCode::string_encode(code)); + } + } + state.SetItemsProcessed(state.iterations() * state.range(0)); +} + +static void CommonCodeDeserialize(benchmark::State &state) { + const auto tmp = str_common_codes(state.range(0), false); + const std::vector samples {tmp.begin(), tmp.end()}; + + for (auto _ : state) { + for (const auto code : samples) { + benchmark::DoNotOptimize(CommonCode::string_decode(code)); + } + } + state.SetItemsProcessed(state.iterations() * state.range(0)); +} + +static void CommonCodeSerializeShorten(benchmark::State &state) { + const auto samples = common_code_samples(state.range(0)); + for (auto _ : state) { + for (const auto code : samples) { + // volatile auto ret = CommonCode::string_encode_shorten(code); + benchmark::DoNotOptimize(CommonCode::string_encode_shorten(code)); + } + } + state.SetItemsProcessed(state.iterations() * state.range(0)); +} + +static void CommonCodeDeserializeShorten(benchmark::State &state) { + const auto tmp = str_common_codes(state.range(0), true); + const std::vector samples {tmp.begin(), tmp.end()}; + + for (auto _ : state) { + for (const auto code : samples) { + benchmark::DoNotOptimize(CommonCode::string_decode(code)); + } + } + state.SetItemsProcessed(state.iterations() * state.range(0)); +} + +// BENCHMARK(CommonCodeCheck)->Range(64, 1024); + +// BENCHMARK(CommonCodeSerialize)->Range(8, 256); +// BENCHMARK(CommonCodeDeserialize)->Range(8, 256); +// BENCHMARK(CommonCodeSerializeShorten)->Range(8, 256); +BENCHMARK(CommonCodeDeserializeShorten)->Range(8, 256); + +BENCHMARK_MAIN();