Browse Source

feat: support benchmark

legacy
Dnomd343 2 years ago
parent
commit
96401bcfa7
  1. 10
      src/klotski/CMakeLists.txt
  2. 29
      src/klotski/benchmark/benchmark.cc
  3. 31
      src/klotski/benchmark/benchmark.h
  4. 93
      src/klotski/benchmark/chore.cc
  5. 26
      src/klotski/ffi/tmain.cc
  6. 4
      src/main.c

10
src/klotski/CMakeLists.txt

@ -17,6 +17,8 @@ include_directories(core)
include_directories(analyse) include_directories(analyse)
include_directories(fast_cal) include_directories(fast_cal)
include_directories(benchmark)
################################################################ ################################################################
add_subdirectory(utils) add_subdirectory(utils)
@ -30,15 +32,17 @@ add_subdirectory(core)
add_subdirectory(analyse) add_subdirectory(analyse)
add_subdirectory(fast_cal) add_subdirectory(fast_cal)
add_subdirectory(benchmark)
################################################################ ################################################################
add_library(klotski-ffi OBJECT ffi/codec.cc ffi/tmain.cc) add_library(klotski-ffi OBJECT ffi/codec.cc ffi/tmain.cc)
################################################################
add_library(klotski SHARED $<TARGET_OBJECTS:klotski-ffi>) add_library(klotski SHARED $<TARGET_OBJECTS:klotski-ffi>)
#add_library(klotski STATIC $<TARGET_OBJECTS:klotski-core>) #add_library(klotski STATIC $<TARGET_OBJECTS:klotski-core>)
################################################################
target_link_libraries(klotski PRIVATE utils) target_link_libraries(klotski PRIVATE utils)
target_link_libraries(klotski PRIVATE all_cases) 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 analyse)
target_link_libraries(klotski PRIVATE fast_cal) target_link_libraries(klotski PRIVATE fast_cal)
target_link_libraries(klotski PRIVATE benchmark)
################################################################ ################################################################

29
src/klotski/benchmark/benchmark.cc

@ -1,36 +1,23 @@
#include "benchmark.h" #include "benchmark.h"
#include "all_cases.h" #include "all_cases.h"
const char line[] = "------------------------"; using namespace klotski;
void Benchmark::basic_ranges(std::ostream &out) { float Benchmark::basic_ranges(Benchmark::TIME format) {
out << line << std::endl;
out << "Basic Ranges Benchmark" << std::endl;
if (BasicRanges::status() != BasicRanges::NO_INIT) { if (BasicRanges::status() != BasicRanges::NO_INIT) {
out << "already built -> " << color_red("skip") << std::endl; return -1; // data already built -> skip
out << line << std::endl;
return;
} }
/// start benchmark process
auto start = clock(); auto start = clock();
BasicRanges::build(); BasicRanges::build();
out << "time -> " << time_ms(start) << std::endl; return time_format(start, format);
out << line << std::endl;
} }
void Benchmark::all_cases(std::ostream &out) { float Benchmark::all_cases(Benchmark::TIME format) {
out << line << std::endl;
out << "All Cases Benchmark" << std::endl;
if (AllCases::status() != AllCases::NO_INIT) { if (AllCases::status() != AllCases::NO_INIT) {
out << "already built -> " << color_red("skip") << std::endl; return -1; // data already built -> skip
out << line << std::endl;
return;
} }
/// preparing benchmark data BasicRanges::build(); // preparing benchmark data
BasicRanges::build();
/// start benchmark process
auto start = clock(); auto start = clock();
AllCases::build(); AllCases::build();
out << "time -> " << time_ms(start) << std::endl; return time_format(start, format);
out << line << std::endl;
} }

31
src/klotski/benchmark/benchmark.h

@ -2,19 +2,20 @@
#include <ostream> #include <ostream>
class Benchmark { namespace klotski {
public: class Benchmark {
static void all_cases(std::ostream &out); public:
static void basic_ranges(std::ostream &out); enum TIME {
S, MS, US, NS
};
static float all_cases(enum TIME format);
static float basic_ranges(enum TIME format);
private: private:
static std::string time_s(clock_t start); // static std::string time_s(clock_t start);
static std::string time_ms(clock_t start); // static std::string time_ms(clock_t start);
static std::string time_us(clock_t start); // static std::string time_us(clock_t start);
static std::string time_ns(clock_t start); // static std::string time_ns(clock_t start);
static float time_format(clock_t start, enum TIME format);
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);
};

93
src/klotski/benchmark/chore.cc

@ -1,45 +1,66 @@
#include "benchmark.h" #include "benchmark.h"
using namespace klotski;
// TODO: remove std::cout output -> only return benchmark status // TODO: remove std::cout output -> only return benchmark status
/// colorful string /// colorful string
std::string Benchmark::color_red(const std::string &str) { //std::string Benchmark::color_red(const std::string &str) {
return std::string("\033[31m") + str + "\033[0m"; // return std::string("\033[31m") + str + "\033[0m";
} //}
//
std::string Benchmark::color_blue(const std::string &str) { //std::string Benchmark::color_blue(const std::string &str) {
return std::string("\033[36m") + str + "\033[0m"; // return std::string("\033[36m") + str + "\033[0m";
} //}
//
std::string Benchmark::color_green(const std::string &str) { //std::string Benchmark::color_green(const std::string &str) {
return std::string("\033[32m") + str + "\033[0m"; // return std::string("\033[32m") + str + "\033[0m";
} //}
//
std::string Benchmark::color_yellow(const std::string &str) { //std::string Benchmark::color_yellow(const std::string &str) {
return std::string("\033[33m") + str + "\033[0m"; // return std::string("\033[33m") + str + "\033[0m";
} //}
/// used-time to green string /// used-time to green string
std::string Benchmark::time_s(clock_t start) { //std::string Benchmark::time_s(clock_t start) {
return color_green( // return color_green(
std::to_string((clock() - start) / CLOCKS_PER_SEC) + "s" // std::to_string((clock() - start) / CLOCKS_PER_SEC) + "s"
); // );
} //}
//
std::string Benchmark::time_ms(clock_t start) { //std::string Benchmark::time_ms(clock_t start) {
return color_green( // return color_green(
std::to_string((clock() - start) * 1000 / CLOCKS_PER_SEC) + "ms" // std::to_string((clock() - start) * 1000 / CLOCKS_PER_SEC) + "ms"
); // );
} //}
//
std::string Benchmark::time_us(clock_t start) { //std::string Benchmark::time_us(clock_t start) {
return color_green( // return color_green(
std::to_string((clock() - start) * 1000 * 1000 / CLOCKS_PER_SEC) + "us" // 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) { float Benchmark::time_format(clock_t start, Benchmark::TIME format) {
return color_green( auto time = float(clock() - start);
std::to_string((clock() - start) * 1000 * 1000 * 1000 / CLOCKS_PER_SEC) + "us" 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;
} }

26
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 <cstdio> #include <cstdio>
#include <cstdint> #include <cstdint>
#include <iostream>
#include "klotski.h" #include "klotski.h"
//#include "core.h"
#include "common.h" #include "common.h"
//#include "core.h" #include "benchmark.h"
using namespace klotski;
void tmain() { void tmain() {
printf("tmain start\n"); printf("tmain start\n");
uint64_t common_code = 0x1A9BC0C00; // uint64_t common_code = 0x1A9BC0C00;
// klotski::Common::range_reverse(common_code);
klotski::Common::range_reverse(common_code);
// uint64_t raw_code = 0x0603EDF5CAFFF5E2;
// auto core = Core([](uint64_t code, uint64_t mask) {
// return;
// });
// for (uint32_t i = 0; i < 1000000; ++i) { std::cout << Benchmark::basic_ranges(Benchmark::MS) << std::endl;
// core.next_cases(raw_code, 0); std::cout << Benchmark::all_cases(Benchmark::MS) << std::endl;
// }
printf("tmain exit\n"); printf("tmain exit\n");
} }

4
src/main.c

@ -2,10 +2,10 @@
#include "klotski.h" #include "klotski.h"
int main() { int main() {
printf("cli boot\n"); // printf("cli boot\n");
tmain(); tmain();
printf("cli exit\n"); // printf("cli exit\n");
return 0; return 0;
} }

Loading…
Cancel
Save