Browse Source

feat: benchmark suites of RawCode

master
Dnomd343 3 weeks ago
parent
commit
c4e5049262
  1. 4
      src/core/CMakeLists.txt
  2. 122
      src/core/benchmark/raw_code.cc

4
src/core/CMakeLists.txt

@ -53,6 +53,10 @@ if (KLSK_ENABLE_BENCHMARK)
target_compile_options(bm_klsk_common_code PRIVATE ${KLSK_BENCHMARK_OPTS}) target_compile_options(bm_klsk_common_code PRIVATE ${KLSK_BENCHMARK_OPTS})
target_link_libraries(bm_klsk_common_code PRIVATE ${KLSK_BENCHMARK_LIBS}) 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) 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})

122
src/core/benchmark/raw_code.cc

@ -0,0 +1,122 @@
#include <benchmark/benchmark.h>
#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<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<uint64_t> raw_code_samples(size_t num) {
std::vector<uint64_t> 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();
Loading…
Cancel
Save