Browse Source

refactor: benchmark suites of CommonCode

master
Dnomd343 3 weeks ago
parent
commit
dcfe37dc6d
  1. 4
      src/core/CMakeLists.txt
  2. 80
      src/core/benchmark/codec.cc
  3. 118
      src/core/benchmark/common_code.cc

4
src/core/CMakeLists.txt

@ -49,6 +49,10 @@ if (KLSK_ENABLE_BENCHMARK)
target_compile_options(bm_klsk_codec PRIVATE ${KLSK_BENCHMARK_OPTS}) target_compile_options(bm_klsk_codec PRIVATE ${KLSK_BENCHMARK_OPTS})
target_link_libraries(bm_klsk_codec PRIVATE ${KLSK_BENCHMARK_LIBS}) 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) add_executable(bm_klsk_group benchmark/group.cc)
target_compile_options(bm_klsk_group PRIVATE ${KLSK_BENCHMARK_OPTS}) target_compile_options(bm_klsk_group PRIVATE ${KLSK_BENCHMARK_OPTS})
target_link_libraries(bm_klsk_group PRIVATE ${KLSK_BENCHMARK_LIBS}) target_link_libraries(bm_klsk_group PRIVATE ${KLSK_BENCHMARK_LIBS})

80
src/core/benchmark/codec.cc

@ -2,11 +2,9 @@
#include <benchmark/benchmark.h> #include <benchmark/benchmark.h>
// #define private public
#include "group/group.h" #include "group/group.h"
#include "all_cases/all_cases.h" #include "all_cases/all_cases.h"
#include "common_code/common_code.h" #include "common_code/common_code.h"
// #undef private
using klotski::codec::ShortCode; using klotski::codec::ShortCode;
using klotski::codec::CommonCode; using klotski::codec::CommonCode;
@ -102,64 +100,6 @@ std::vector<std::string> str_short_codes(uint64_t num) {
return codes; 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<std::string_view> 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<std::string_view> 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) { static void ShortCodeSerialize(benchmark::State &state) {
std::vector<uint32_t> samples = short_code_samples(state.range(0)); std::vector<uint32_t> 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<uint64_t> 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(ShortCodeSerialize)->Range(8, 256);
// BENCHMARK(ShortCodeDeserialize)->Range(8, 256); // BENCHMARK(ShortCodeDeserialize)->Range(8, 256);

118
src/core/benchmark/common_code.cc

@ -0,0 +1,118 @@
#include <benchmark/benchmark.h>
#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<uint64_t> all_common_codes() {
std::vector<uint64_t> 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<uint64_t> 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<uint64_t> 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<std::string> str_common_codes(uint64_t num, bool shorten) {
// auto src = select_codes(num);
auto src = common_code_samples(num);
std::vector<std::string> 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<std::string_view> 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<std::string_view> 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();
Loading…
Cancel
Save