From 851b79d2d72c24e39bf259d26f2954457536cf97 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Thu, 2 Mar 2023 20:36:40 +0800 Subject: [PATCH] feat: benchmark interface of klotski ffi --- src/klotski_core/CMakeLists.txt | 2 +- src/klotski_core/benchmark/benchmark.h | 4 +- src/klotski_core/ffi/benchmark.cc | 103 +++++++++++++++++++++++++ src/klotski_core/ffi/tmain.cc | 4 +- src/klotski_core/klotski.h | 46 ++++++++++- 5 files changed, 151 insertions(+), 8 deletions(-) create mode 100644 src/klotski_core/ffi/benchmark.cc diff --git a/src/klotski_core/CMakeLists.txt b/src/klotski_core/CMakeLists.txt index 269c95d..6b57505 100644 --- a/src/klotski_core/CMakeLists.txt +++ b/src/klotski_core/CMakeLists.txt @@ -118,7 +118,7 @@ add_subdirectory(benchmark) ################################################################################ include_directories(.) -set(FFI_SRC codec.cc all_cases.cc tmain.cc metadata.cc) +set(FFI_SRC codec.cc all_cases.cc tmain.cc metadata.cc benchmark.cc) list(TRANSFORM FFI_SRC PREPEND "ffi/") add_library(klotski-ffi OBJECT ${FFI_SRC}) diff --git a/src/klotski_core/benchmark/benchmark.h b/src/klotski_core/benchmark/benchmark.h index 9253c35..973c3e0 100644 --- a/src/klotski_core/benchmark/benchmark.h +++ b/src/klotski_core/benchmark/benchmark.h @@ -52,8 +52,8 @@ namespace klotski { static double common_code_to_raw_code(TIME format = NS) noexcept; static double raw_code_to_common_code(TIME format = NS) noexcept; - static double common_code_to_short_code(TIME format = NS) noexcept; - static double short_code_to_common_code(TIME format = NS) noexcept; + static double common_code_to_short_code(TIME format = US) noexcept; + static double short_code_to_common_code(TIME format = US) noexcept; static double common_code_to_short_code_fast(TIME format = NS) noexcept; static double short_code_to_common_code_fast(TIME format = NS) noexcept; diff --git a/src/klotski_core/ffi/benchmark.cc b/src/klotski_core/ffi/benchmark.cc new file mode 100644 index 0000000..d251fb2 --- /dev/null +++ b/src/klotski_core/ffi/benchmark.cc @@ -0,0 +1,103 @@ +#include "klotski.h" +#include "benchmark.h" + +using klotski::Benchmark; + +/////////////////////////////////////////////////////////////////////////////// + +void benchmark_preparation() { + Benchmark::data_preparation(); +} + +double benchmark_warm_up(uint64_t count) { + return Benchmark::warm_up(count); +} + +/////////////////////////////////////////////////////////////////////////////// + +double benchmark_range_flip_ns() { + return Benchmark::range_flip(Benchmark::NS); +} + +/////////////////////////////////////////////////////////////////////////////// + +double benchmark_all_cases_ms() { + return Benchmark::all_cases(Benchmark::MS); +} +double benchmark_basic_ranges_ms() { + return Benchmark::basic_ranges(Benchmark::MS); +} + +/////////////////////////////////////////////////////////////////////////////// + +double benchmark_raw_code_check_ns() { + return Benchmark::raw_code_check(Benchmark::NS); +} +double benchmark_short_code_check_ns() { + return Benchmark::short_code_check(Benchmark::NS); +} +double benchmark_common_code_check_ns() { + return Benchmark::common_code_check(Benchmark::NS); +} +double benchmark_raw_code_check_random_ns() { + return Benchmark::raw_code_check_random(Benchmark::NS); +} +double benchmark_short_code_check_random_ns() { + return Benchmark::short_code_check_random(Benchmark::NS); +} +double benchmark_common_code_check_random_ns() { + return Benchmark::common_code_check_random(Benchmark::NS); +} + +/////////////////////////////////////////////////////////////////////////////// + +double benchmark_short_code_to_string_ns() { + return Benchmark::short_code_to_string(Benchmark::NS); +} +double benchmark_short_code_from_string_ns() { + return Benchmark::short_code_from_string(Benchmark::NS); +} +double benchmark_common_code_to_string_ns() { + return Benchmark::common_code_to_string(Benchmark::NS); +} +double benchmark_common_code_from_string_ns() { + return Benchmark::common_code_from_string(Benchmark::NS); +} + +/////////////////////////////////////////////////////////////////////////////// + +double benchmark_common_code_to_raw_code_ns() { + return Benchmark::common_code_to_raw_code(Benchmark::NS); +} +double benchmark_raw_code_to_common_code_ns() { + return Benchmark::raw_code_to_common_code(Benchmark::NS); +} +double benchmark_common_code_to_short_code_us() { + return Benchmark::common_code_to_short_code(Benchmark::US); +} +double benchmark_short_code_to_common_code_us() { + return Benchmark::short_code_to_common_code(Benchmark::US); +} +double benchmark_common_code_to_short_code_fast_ns() { + return Benchmark::common_code_to_short_code_fast(Benchmark::NS); +} +double benchmark_short_code_to_common_code_fast_ns() { + return Benchmark::short_code_to_common_code_fast(Benchmark::NS); +} + +/////////////////////////////////////////////////////////////////////////////// + +double benchmark_vertical_mirror_check_ns() { + return Benchmark::vertical_mirror_check(Benchmark::NS); +} +double benchmark_horizontal_mirror_check_ns() { + return Benchmark::horizontal_mirror_check(Benchmark::NS); +} +double benchmark_vertical_mirror_convert_ns() { + return Benchmark::vertical_mirror_convert(Benchmark::NS); +} +double benchmark_horizontal_mirror_convert_ns() { + return Benchmark::horizontal_mirror_convert(Benchmark::NS); +} + +/////////////////////////////////////////////////////////////////////////////// diff --git a/src/klotski_core/ffi/tmain.cc b/src/klotski_core/ffi/tmain.cc index 96a0c03..adae2a8 100644 --- a/src/klotski_core/ffi/tmain.cc +++ b/src/klotski_core/ffi/tmain.cc @@ -58,9 +58,9 @@ void tmain() { Benchmark::raw_code_to_common_code() << "ns" << std::endl; std::cout << "common code to short code: " << - Benchmark::common_code_to_short_code() << "ns" << std::endl; + Benchmark::common_code_to_short_code() << "us" << std::endl; std::cout << "short code to common code: " << - Benchmark::short_code_to_common_code() << "ns" << std::endl; + Benchmark::short_code_to_common_code() << "us" << std::endl; std::cout << "common code to short code fast: " << Benchmark::common_code_to_short_code_fast() << "ns" << std::endl; diff --git a/src/klotski_core/klotski.h b/src/klotski_core/klotski.h index eb8a669..efada34 100644 --- a/src/klotski_core/klotski.h +++ b/src/klotski_core/klotski.h @@ -19,7 +19,7 @@ extern "C" { } #endif -/// -------------------------------- klotski metadata info ---------------------------------- +/// ---------------------------------- klotski metadata info ---------------------------------- #ifdef __cplusplus extern "C" { @@ -41,7 +41,7 @@ extern "C" { } #endif -/// ------------------------------- klotski all cases export -------------------------------- +/// -------------------------------- klotski all cases export --------------------------------- #ifdef __cplusplus extern "C" { @@ -60,7 +60,7 @@ extern "C" { } #endif -/// -------------------------------- klotski codec interface -------------------------------- +/// --------------------------------- klotski codec interface --------------------------------- #ifdef __cplusplus extern "C" { @@ -112,3 +112,43 @@ extern "C" { #ifdef __cplusplus } #endif + +/// ------------------------------------ klotski benchmark ------------------------------------ + +#ifdef __cplusplus +extern "C" { +#endif + EXTERN_FUNC void benchmark_preparation(); + EXTERN_FUNC double benchmark_warm_up(uint64_t count); + + EXTERN_FUNC double benchmark_range_flip_ns(); + + EXTERN_FUNC double benchmark_all_cases_ms(); + EXTERN_FUNC double benchmark_basic_ranges_ms(); + + EXTERN_FUNC double benchmark_raw_code_check_ns(); + EXTERN_FUNC double benchmark_short_code_check_ns(); + EXTERN_FUNC double benchmark_common_code_check_ns(); + EXTERN_FUNC double benchmark_raw_code_check_random_ns(); + EXTERN_FUNC double benchmark_short_code_check_random_ns(); + EXTERN_FUNC double benchmark_common_code_check_random_ns(); + + EXTERN_FUNC double benchmark_short_code_to_string_ns(); + EXTERN_FUNC double benchmark_short_code_from_string_ns(); + EXTERN_FUNC double benchmark_common_code_to_string_ns(); + EXTERN_FUNC double benchmark_common_code_from_string_ns(); + + EXTERN_FUNC double benchmark_common_code_to_raw_code_ns(); + EXTERN_FUNC double benchmark_raw_code_to_common_code_ns(); + EXTERN_FUNC double benchmark_common_code_to_short_code_us(); + EXTERN_FUNC double benchmark_short_code_to_common_code_us(); + EXTERN_FUNC double benchmark_common_code_to_short_code_fast_ns(); + EXTERN_FUNC double benchmark_short_code_to_common_code_fast_ns(); + + EXTERN_FUNC double benchmark_vertical_mirror_check_ns(); + EXTERN_FUNC double benchmark_horizontal_mirror_check_ns(); + EXTERN_FUNC double benchmark_vertical_mirror_convert_ns(); + EXTERN_FUNC double benchmark_horizontal_mirror_convert_ns(); +#ifdef __cplusplus +} +#endif