Browse Source

feat: benchmark module of rust ffi

legacy
Dnomd343 2 years ago
parent
commit
68a5272975
  1. 22
      src/cli/src/main.rs
  2. 2
      src/klotski_core/ffi/benchmark.cc
  3. 2
      src/klotski_core/klotski.h
  4. 58
      src/rust_ffi/src/benchmark/chore.rs
  5. 41
      src/rust_ffi/src/benchmark/ffi.rs
  6. 18
      src/rust_ffi/src/benchmark/mod.rs
  7. 3
      src/rust_ffi/src/lib.rs

22
src/cli/src/main.rs

@ -1,6 +1,6 @@
extern crate klotski_ffi; extern crate klotski_ffi;
use klotski_ffi::AllCases; // use klotski_ffi::AllCases;
// use std::thread::sleep; // use std::thread::sleep;
// use std::time::Duration; // use std::time::Duration;
@ -50,18 +50,20 @@ fn main() {
// println!("{:09X}", code); // println!("{:09X}", code);
// } // }
for raw_code in &AllCases::raw_codes()[..8] { // for raw_code in &AllCases::raw_codes()[..8] {
println!("{}", raw_code); // println!("{}", raw_code);
} // }
for short_code in &AllCases::short_codes()[..8] { // for short_code in &AllCases::short_codes()[..8] {
println!("{}", short_code); // println!("{}", short_code);
} // }
for common_code in &AllCases::common_codes()[..8] { // for common_code in &AllCases::common_codes()[..8] {
println!("{}", common_code); // println!("{}", common_code);
} // }
// loop { // loop {
// sleep(Duration::from_secs(1)); // sleep(Duration::from_secs(1));
// } // }
klotski_ffi::demo();
} }

2
src/klotski_core/ffi/benchmark.cc

@ -9,7 +9,7 @@ void benchmark_preparation() {
Benchmark::data_preparation(); Benchmark::data_preparation();
} }
double benchmark_warm_up(uint64_t count) { double benchmark_warm_up_us(uint64_t count) {
return Benchmark::warm_up(count); return Benchmark::warm_up(count);
} }

2
src/klotski_core/klotski.h

@ -119,7 +119,7 @@ extern "C" {
extern "C" { extern "C" {
#endif #endif
EXTERN_FUNC void benchmark_preparation(); EXTERN_FUNC void benchmark_preparation();
EXTERN_FUNC double benchmark_warm_up(uint64_t count); EXTERN_FUNC double benchmark_warm_up_us(uint64_t count);
EXTERN_FUNC double benchmark_range_flip_ns(); EXTERN_FUNC double benchmark_range_flip_ns();

58
src/rust_ffi/src/benchmark/chore.rs

@ -0,0 +1,58 @@
#[derive(Debug)]
pub(crate) struct Duration {
picos: u64
}
impl Duration {
#[allow(dead_code)]
pub(crate) fn from_ns(ns: f64) -> Duration {
Duration {
picos: (ns * (1_000 as f64)) as u64
}
}
#[allow(dead_code)]
pub(crate) fn from_us(us: f64) -> Duration {
Duration {
picos: (us * (1_000_000 as f64)) as u64
}
}
#[allow(dead_code)]
pub(crate) fn from_ms(ms: f64) -> Duration {
Duration {
picos: (ms * (1_000_000_000 as f64)) as u64
}
}
#[allow(dead_code)]
pub(crate) fn from_s(s: f64) -> Duration {
Duration {
picos: (s * (1_000_000_000_000 as i64 as f64)) as u64
}
}
#[allow(dead_code)]
pub(crate) fn to_ns(&self) -> String {
let tmp = self.picos as f64 / (1_000 as f64);
format!("{:.03}ns", tmp)
}
#[allow(dead_code)]
pub(crate) fn to_us(&self) -> String {
let tmp = self.picos as f64 / (1_000_000 as f64);
format!("{:.03}us", tmp)
}
#[allow(dead_code)]
pub(crate) fn to_ms(&self) -> String {
let tmp = self.picos as f64 / (1_000_000_000 as f64);
format!("{:.03}ms", tmp)
}
#[allow(dead_code)]
pub(crate) fn to_s(&self) -> String {
let tmp = self.picos as f64 / (1_000_000_000_000 as i64 as f64);
format!("{:.03}s", tmp)
}
}

41
src/rust_ffi/src/benchmark/ffi.rs

@ -0,0 +1,41 @@
use super::Duration;
use crate::core::Core;
use std::cmp::Ordering;
pub(crate) fn preparation() {
unsafe {
Core::benchmark_preparation();
}
}
pub(crate) fn warm_up(count: u64) -> Duration {
unsafe {
Duration::from_us(Core::benchmark_warm_up_us(count))
}
}
pub(crate) fn range_flip() -> Duration {
unsafe {
Duration::from_ns(Core::benchmark_range_flip_ns())
}
}
pub(crate) fn basic_ranges() -> Result<Duration, &'static str> {
unsafe {
let time = Core::benchmark_basic_ranges_ms();
match time.total_cmp(&(0 as f64)) {
Ordering::Greater => Ok(Duration::from_ms(time)),
_ => Err("data already built"),
}
}
}
pub(crate) fn all_cases() -> Result<Duration, &'static str> {
unsafe {
let time = Core::benchmark_all_cases_ms();
match time.total_cmp(&(0 as f64)) {
Ordering::Greater => Ok(Duration::from_ms(time)),
_ => Err("data already built"),
}
}
}

18
src/rust_ffi/src/benchmark/mod.rs

@ -0,0 +1,18 @@
mod ffi;
mod chore;
use chore::Duration;
use ffi::*;
pub fn demo() {
println!("warm up: {}", warm_up(0x100_0000).to_ms());
println!("range flip: {}", range_flip().to_ns());
println!("basic ranges: {}", basic_ranges().unwrap().to_ms());
println!("all cases: {}", all_cases().unwrap().to_ms());
}

3
src/rust_ffi/src/lib.rs

@ -5,6 +5,7 @@ mod core;
mod codec; mod codec;
mod metadata; mod metadata;
mod all_cases; mod all_cases;
mod benchmark;
pub use codec::RawCode; pub use codec::RawCode;
pub use codec::ShortCode; pub use codec::ShortCode;
@ -13,3 +14,5 @@ pub use codec::CommonCode;
pub use all_cases::AllCases; pub use all_cases::AllCases;
pub use metadata::load_metadata as metadata; pub use metadata::load_metadata as metadata;
pub use benchmark::demo;

Loading…
Cancel
Save