From 96401bcfa745d375c1a967075493d27ccf1a91d0 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Fri, 27 Jan 2023 09:09:43 +0800 Subject: [PATCH] feat: support benchmark --- src/klotski/CMakeLists.txt | 10 +++- src/klotski/benchmark/benchmark.cc | 29 +++------- src/klotski/benchmark/benchmark.h | 31 +++++----- src/klotski/benchmark/chore.cc | 93 ++++++++++++++++++------------ src/klotski/ffi/tmain.cc | 26 ++++----- src/main.c | 4 +- 6 files changed, 101 insertions(+), 92 deletions(-) diff --git a/src/klotski/CMakeLists.txt b/src/klotski/CMakeLists.txt index d4609bc..dfb2779 100644 --- a/src/klotski/CMakeLists.txt +++ b/src/klotski/CMakeLists.txt @@ -17,6 +17,8 @@ include_directories(core) include_directories(analyse) include_directories(fast_cal) +include_directories(benchmark) + ################################################################ add_subdirectory(utils) @@ -30,15 +32,17 @@ add_subdirectory(core) add_subdirectory(analyse) add_subdirectory(fast_cal) +add_subdirectory(benchmark) + ################################################################ add_library(klotski-ffi OBJECT ffi/codec.cc ffi/tmain.cc) -################################################################ - add_library(klotski SHARED $) #add_library(klotski STATIC $) +################################################################ + target_link_libraries(klotski PRIVATE utils) target_link_libraries(klotski PRIVATE all_cases) @@ -50,4 +54,6 @@ target_link_libraries(klotski PRIVATE core) target_link_libraries(klotski PRIVATE analyse) target_link_libraries(klotski PRIVATE fast_cal) +target_link_libraries(klotski PRIVATE benchmark) + ################################################################ diff --git a/src/klotski/benchmark/benchmark.cc b/src/klotski/benchmark/benchmark.cc index 1099bd4..cecd9a1 100644 --- a/src/klotski/benchmark/benchmark.cc +++ b/src/klotski/benchmark/benchmark.cc @@ -1,36 +1,23 @@ #include "benchmark.h" #include "all_cases.h" -const char line[] = "------------------------"; +using namespace klotski; -void Benchmark::basic_ranges(std::ostream &out) { - out << line << std::endl; - out << "Basic Ranges Benchmark" << std::endl; +float Benchmark::basic_ranges(Benchmark::TIME format) { if (BasicRanges::status() != BasicRanges::NO_INIT) { - out << "already built -> " << color_red("skip") << std::endl; - out << line << std::endl; - return; + return -1; // data already built -> skip } - /// start benchmark process auto start = clock(); BasicRanges::build(); - out << "time -> " << time_ms(start) << std::endl; - out << line << std::endl; + return time_format(start, format); } -void Benchmark::all_cases(std::ostream &out) { - out << line << std::endl; - out << "All Cases Benchmark" << std::endl; +float Benchmark::all_cases(Benchmark::TIME format) { if (AllCases::status() != AllCases::NO_INIT) { - out << "already built -> " << color_red("skip") << std::endl; - out << line << std::endl; - return; + return -1; // data already built -> skip } - /// preparing benchmark data - BasicRanges::build(); - /// start benchmark process + BasicRanges::build(); // preparing benchmark data auto start = clock(); AllCases::build(); - out << "time -> " << time_ms(start) << std::endl; - out << line << std::endl; + return time_format(start, format); } diff --git a/src/klotski/benchmark/benchmark.h b/src/klotski/benchmark/benchmark.h index 5daa6b1..79315e2 100644 --- a/src/klotski/benchmark/benchmark.h +++ b/src/klotski/benchmark/benchmark.h @@ -2,19 +2,20 @@ #include -class Benchmark { -public: - static void all_cases(std::ostream &out); - static void basic_ranges(std::ostream &out); +namespace klotski { + class Benchmark { + public: + enum TIME { + S, MS, US, NS + }; + static float all_cases(enum TIME format); + static float basic_ranges(enum TIME format); -private: - static std::string time_s(clock_t start); - static std::string time_ms(clock_t start); - static std::string time_us(clock_t start); - static std::string time_ns(clock_t start); - - static std::string color_red(const std::string &str); - static std::string color_blue(const std::string &str); - static std::string color_green(const std::string &str); - static std::string color_yellow(const std::string &str); -}; + private: +// static std::string time_s(clock_t start); +// static std::string time_ms(clock_t start); +// static std::string time_us(clock_t start); +// static std::string time_ns(clock_t start); + static float time_format(clock_t start, enum TIME format); + }; +} diff --git a/src/klotski/benchmark/chore.cc b/src/klotski/benchmark/chore.cc index 9badc44..43cf7ee 100644 --- a/src/klotski/benchmark/chore.cc +++ b/src/klotski/benchmark/chore.cc @@ -1,45 +1,66 @@ #include "benchmark.h" +using namespace klotski; + // TODO: remove std::cout output -> only return benchmark status /// colorful string -std::string Benchmark::color_red(const std::string &str) { - return std::string("\033[31m") + str + "\033[0m"; -} - -std::string Benchmark::color_blue(const std::string &str) { - return std::string("\033[36m") + str + "\033[0m"; -} - -std::string Benchmark::color_green(const std::string &str) { - return std::string("\033[32m") + str + "\033[0m"; -} - -std::string Benchmark::color_yellow(const std::string &str) { - return std::string("\033[33m") + str + "\033[0m"; -} +//std::string Benchmark::color_red(const std::string &str) { +// return std::string("\033[31m") + str + "\033[0m"; +//} +// +//std::string Benchmark::color_blue(const std::string &str) { +// return std::string("\033[36m") + str + "\033[0m"; +//} +// +//std::string Benchmark::color_green(const std::string &str) { +// return std::string("\033[32m") + str + "\033[0m"; +//} +// +//std::string Benchmark::color_yellow(const std::string &str) { +// return std::string("\033[33m") + str + "\033[0m"; +//} /// used-time to green string -std::string Benchmark::time_s(clock_t start) { - return color_green( - std::to_string((clock() - start) / CLOCKS_PER_SEC) + "s" - ); -} - -std::string Benchmark::time_ms(clock_t start) { - return color_green( - std::to_string((clock() - start) * 1000 / CLOCKS_PER_SEC) + "ms" - ); -} - -std::string Benchmark::time_us(clock_t start) { - return color_green( - std::to_string((clock() - start) * 1000 * 1000 / CLOCKS_PER_SEC) + "us" - ); -} +//std::string Benchmark::time_s(clock_t start) { +// return color_green( +// std::to_string((clock() - start) / CLOCKS_PER_SEC) + "s" +// ); +//} +// +//std::string Benchmark::time_ms(clock_t start) { +// return color_green( +// std::to_string((clock() - start) * 1000 / CLOCKS_PER_SEC) + "ms" +// ); +//} +// +//std::string Benchmark::time_us(clock_t start) { +// return color_green( +// std::to_string((clock() - start) * 1000 * 1000 / CLOCKS_PER_SEC) + "us" +// ); +//} +// +//std::string Benchmark::time_ns(clock_t start) { +// return color_green( +// std::to_string((clock() - start) * 1000 * 1000 * 1000 / CLOCKS_PER_SEC) + "us" +// ); +//} -std::string Benchmark::time_ns(clock_t start) { - return color_green( - std::to_string((clock() - start) * 1000 * 1000 * 1000 / CLOCKS_PER_SEC) + "us" - ); +float Benchmark::time_format(clock_t start, Benchmark::TIME format) { + auto time = float(clock() - start); + switch (format) { + case S: + time *= float(1); + break; + case MS: + time *= float(1000); + break; + case US: + time *= float(1000 * 1000); + break; + case NS: + time *= float(1000 * 1000 * 1000); + break; + } + return time / CLOCKS_PER_SEC; } diff --git a/src/klotski/ffi/tmain.cc b/src/klotski/ffi/tmain.cc index 1b0b65e..fad95b3 100644 --- a/src/klotski/ffi/tmain.cc +++ b/src/klotski/ffi/tmain.cc @@ -1,30 +1,24 @@ -/// WARN: c-style lib should not using `iostream` -/// using `printf` for screen output in test process - #include #include +#include + #include "klotski.h" +//#include "core.h" #include "common.h" -//#include "core.h" +#include "benchmark.h" + +using namespace klotski; void tmain() { printf("tmain start\n"); - uint64_t common_code = 0x1A9BC0C00; - - klotski::Common::range_reverse(common_code); - -// uint64_t raw_code = 0x0603EDF5CAFFF5E2; - -// auto core = Core([](uint64_t code, uint64_t mask) { -// return; -// }); +// uint64_t common_code = 0x1A9BC0C00; +// klotski::Common::range_reverse(common_code); -// for (uint32_t i = 0; i < 1000000; ++i) { -// core.next_cases(raw_code, 0); -// } + std::cout << Benchmark::basic_ranges(Benchmark::MS) << std::endl; + std::cout << Benchmark::all_cases(Benchmark::MS) << std::endl; printf("tmain exit\n"); } diff --git a/src/main.c b/src/main.c index eaf10ef..afb1497 100644 --- a/src/main.c +++ b/src/main.c @@ -2,10 +2,10 @@ #include "klotski.h" int main() { - printf("cli boot\n"); +// printf("cli boot\n"); tmain(); - printf("cli exit\n"); +// printf("cli exit\n"); return 0; }