From 341b06abf16a1091999846e6b4b4db46903c61d8 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Sun, 8 Dec 2024 19:44:52 +0800 Subject: [PATCH] refactor: benchmark suites of ShortCode --- src/core/CMakeLists.txt | 4 + src/core/benchmark/codec.cc | 83 ---------------- src/core/benchmark/short_code.cc | 161 +++++++++++++++++++++++++++++++ 3 files changed, 165 insertions(+), 83 deletions(-) create mode 100644 src/core/benchmark/short_code.cc diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index c6c4e22..3790ce2 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -57,6 +57,10 @@ if (KLSK_ENABLE_BENCHMARK) 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_short_code benchmark/short_code.cc) + target_compile_options(bm_klsk_short_code PRIVATE ${KLSK_BENCHMARK_OPTS}) + target_link_libraries(bm_klsk_short_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 69b6b13..b7c8553 100644 --- a/src/core/benchmark/codec.cc +++ b/src/core/benchmark/codec.cc @@ -100,89 +100,6 @@ std::vector str_short_codes(uint64_t num) { return codes; } -static void ShortCodeSerialize(benchmark::State &state) { - std::vector samples = short_code_samples(state.range(0)); - - for (auto _ : state) { - - for (const auto code : samples) { - volatile auto ret = ShortCode::string_encode(code); - } - - } - - state.SetItemsProcessed(state.iterations() * state.range(0)); - -} - -static void ShortCodeDeserialize(benchmark::State &state) { - - const auto tmp = str_short_codes(state.range(0)); - - const std::vector samples {tmp.begin(), tmp.end()}; - - for (auto _ : state) { - - for (const auto code : samples) { - - volatile auto ret = ShortCode::string_decode(code); - - } - - } - - state.SetItemsProcessed(state.iterations() * state.range(0)); - -} - -static void ShortCodeToCommonCode(benchmark::State &state) { - - // ShortCode::speed_up(true); - ShortCode::speed_up(false); - - // ShortCode::fast_decode(4091296); - - auto short_code = CommonCode::unsafe_create(0x1A9BF0C00).to_short_code(); - - for (auto _ : state) { - - // volatile auto kk = short_code.to_common_code(); - - benchmark::DoNotOptimize(short_code.to_common_code()); - - // if (AllCases::instance().is_available()) { - // if (ShortCode::stage_ == ShortCode::Stage::FAST) { - // volatile auto pp = ShortCode::fast_decode(4091296); - // } - // } - - } - -} - -static void CommonCodeToShortCode(benchmark::State &state) { - // ShortCode::speed_up(true); - ShortCode::speed_up(false); - - auto common_code = CommonCode::unsafe_create(0x1A9BF0C00); - - // std::vector samples; - // for (auto code : common_code_samples(256)) { - // samples.emplace_back(CommonCode::unsafe_create(code)); - // } - - for (auto _ : state) { - - // for (auto common_code : samples) { - // volatile auto kk = ShortCode(common_code); - - benchmark::DoNotOptimize(ShortCode(common_code)); - - // } - - } -} - static void IsMirrorCompare(benchmark::State &state) { std::vector samples; diff --git a/src/core/benchmark/short_code.cc b/src/core/benchmark/short_code.cc new file mode 100644 index 0000000..743d745 --- /dev/null +++ b/src/core/benchmark/short_code.cc @@ -0,0 +1,161 @@ +#include + +#include "all_cases/all_cases.h" + +#include "short_code/short_code.h" + +using klotski::cases::AllCases; +using klotski::codec::ShortCode; + +/// 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 = 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 short_code_samples(uint64_t num) { + + uint32_t part_size = klotski::codec::SHORT_CODE_LIMIT / num; + + uint32_t offset = part_size / 2; + + std::vector result; + + for (uint32_t i = 0; i < num; ++i) { + uint32_t index = i * part_size + offset; + + result.emplace_back(index); + } + + return result; + +} + +std::vector str_short_codes(uint64_t num) { + auto src = short_code_samples(num); + + std::vector codes; + codes.reserve(src.size()); + for (auto x : src) { + codes.emplace_back(klotski::codec::ShortCode::unsafe_create(x).to_string()); + } + + return codes; +} + +static void ShortCodeCheck(benchmark::State &state) { + const auto samples = short_code_samples(state.range(0)); + for (auto _ : state) { + for (const auto code : samples) { + benchmark::DoNotOptimize(ShortCode::check(code)); + } + } + state.SetItemsProcessed(state.iterations() * state.range(0)); +} + +static void ShortCodeSerialize(benchmark::State &state) { + const auto samples = short_code_samples(state.range(0)); + for (auto _ : state) { + for (const auto code : samples) { + // volatile auto ret = ShortCode::string_encode(code); + benchmark::DoNotOptimize(ShortCode::string_encode(code)); + } + } + state.SetItemsProcessed(state.iterations() * state.range(0)); +} + +static void ShortCodeDeserialize(benchmark::State &state) { + const auto tmp = str_short_codes(state.range(0)); + const std::vector samples {tmp.begin(), tmp.end()}; + + for (auto _ : state) { + for (const auto code : samples) { + benchmark::DoNotOptimize(ShortCode::string_decode(code)); + } + } + state.SetItemsProcessed(state.iterations() * state.range(0)); +} + +static void ShortCodeFastDecode(benchmark::State &state) { + ShortCode::speed_up(true); + const auto samples = short_code_samples(state.range(0)); + for (auto _ : state) { + for (const auto code : samples) { + benchmark::DoNotOptimize(ShortCode::fast_decode(code)); + } + } + state.SetItemsProcessed(state.iterations() * state.range(0)); +} + +static void ShortCodeFastEncode(benchmark::State &state) { + ShortCode::speed_up(true); + const auto samples = common_code_samples(state.range(0)); + for (auto _ : state) { + for (const auto code : samples) { + benchmark::DoNotOptimize(ShortCode::fast_encode(code)); + } + } + state.SetItemsProcessed(state.iterations() * state.range(0)); +} + +static void ShortCodeTinyDecode(benchmark::State &state) { + ShortCode::fast_ = false; + ShortCode::speed_up(false); + const auto samples = short_code_samples(state.range(0)); + for (auto _ : state) { + for (auto code : samples) { + benchmark::DoNotOptimize(ShortCode::tiny_decode(code)); + } + } + state.SetItemsProcessed(state.iterations() * state.range(0)); +} + +static void ShortCodeTinyEncode(benchmark::State &state) { + ShortCode::fast_ = false; + ShortCode::speed_up(false); + const auto samples = common_code_samples(state.range(0)); + for (auto _ : state) { + for (const auto code : samples) { + benchmark::DoNotOptimize(ShortCode::tiny_encode(code)); + } + } + state.SetItemsProcessed(state.iterations() * state.range(0)); +} + +// BENCHMARK(ShortCodeCheck)->Range(8, 256); + +// BENCHMARK(ShortCodeSerialize)->Range(8, 256); +// BENCHMARK(ShortCodeDeserialize)->Range(8, 256); + +BENCHMARK(ShortCodeFastDecode)->Range(8, 256); +BENCHMARK(ShortCodeFastEncode)->Range(8, 256); + +// BENCHMARK(ShortCodeTinyDecode)->Range(8, 256)->Unit(benchmark::kMicrosecond); +// BENCHMARK(ShortCodeTinyEncode)->Range(8, 256)->Unit(benchmark::kMicrosecond); + +BENCHMARK_MAIN();