Browse Source

feat: support benchmark

master
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(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 $<TARGET_OBJECTS:klotski-ffi>)
#add_library(klotski STATIC $<TARGET_OBJECTS:klotski-core>)
################################################################
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)
################################################################

29
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);
}

31
src/klotski/benchmark/benchmark.h

@ -2,19 +2,20 @@
#include <ostream>
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);
};
}

93
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;
}

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 <cstdint>
#include <iostream>
#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");
}

4
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;
}

Loading…
Cancel
Save