From 24bf4ea2af20ad1bec23a7d0f7eaab2b4bf78719 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Fri, 3 Mar 2023 00:09:11 +0800 Subject: [PATCH] update: codec checker benchmark of rust ffi --- src/rust_ffi/src/benchmark/chore.rs | 98 ++++++++++++++++++++--------- src/rust_ffi/src/benchmark/ffi.rs | 48 ++++++++++++++ src/rust_ffi/src/benchmark/mod.rs | 31 ++++++--- 3 files changed, 141 insertions(+), 36 deletions(-) diff --git a/src/rust_ffi/src/benchmark/chore.rs b/src/rust_ffi/src/benchmark/chore.rs index 3034eb5..a910cbb 100644 --- a/src/rust_ffi/src/benchmark/chore.rs +++ b/src/rust_ffi/src/benchmark/chore.rs @@ -1,9 +1,64 @@ +use std::fmt; + #[derive(Debug)] pub(crate) struct Duration { picos: u64 } impl Duration { + #[inline] + fn to_ps(&self) -> f64 { + self.picos as f64 / (1 as f64) + } + + #[inline] + fn to_ns(&self) -> f64 { + self.picos as f64 / (1_000 as f64) + } + + #[inline] + fn to_us(&self) -> f64 { + self.picos as f64 / (1_000_000 as f64) + } + + #[inline] + fn to_ms(&self) -> f64 { + self.picos as f64 / (1_000_000_000 as f64) + } +} + +impl Duration { + #[inline] + fn to_ps_str(&self) -> String { + format!("{:.03}ps", self.to_ps()) + } + + #[inline] + fn to_ns_str(&self) -> String { + format!("{:.03}ns", self.to_ns()) + } + + #[inline] + fn to_us_str(&self) -> String { + format!("{:.03}us", self.to_us()) + } + + #[inline] + fn to_ms_str(&self) -> String { + format!("{:.03}ms", self.to_ms()) + } +} + +impl Duration { + #[inline] + #[allow(dead_code)] + pub(crate) fn from_ps(ps: f64) -> Duration { + Duration { + picos: (ps * (1 as f64)) as u64 + } + } + + #[inline] #[allow(dead_code)] pub(crate) fn from_ns(ns: f64) -> Duration { Duration { @@ -11,6 +66,7 @@ impl Duration { } } + #[inline] #[allow(dead_code)] pub(crate) fn from_us(us: f64) -> Duration { Duration { @@ -18,41 +74,25 @@ impl Duration { } } + #[inline] #[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) +impl fmt::Display for Duration { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{}", if self.picos < 1000 { + self.to_ps_str() + } else if self.picos < 1_000_000 { + self.to_ns_str() + } else if self.picos < 1_000_000_000 { + self.to_us_str() + } else { + self.to_ms_str() + }) } } diff --git a/src/rust_ffi/src/benchmark/ffi.rs b/src/rust_ffi/src/benchmark/ffi.rs index 4e09ef8..181c13b 100644 --- a/src/rust_ffi/src/benchmark/ffi.rs +++ b/src/rust_ffi/src/benchmark/ffi.rs @@ -39,3 +39,51 @@ pub(crate) fn all_cases() -> Result { } } } + +pub(crate) fn raw_code_check() -> Result { + unsafe { + let time = Core::benchmark_raw_code_check_ns(); + match time.total_cmp(&(0 as f64)) { + Ordering::Greater => Ok(Duration::from_ns(time)), + _ => Err("data not ready"), + } + } +} + +pub(crate) fn short_code_check() -> Result { + unsafe { + let time = Core::benchmark_short_code_check_ns(); + match time.total_cmp(&(0 as f64)) { + Ordering::Greater => Ok(Duration::from_ns(time)), + _ => Err("data not ready"), + } + } +} + +pub(crate) fn common_code_check() -> Result { + unsafe { + let time = Core::benchmark_common_code_check_ns(); + match time.total_cmp(&(0 as f64)) { + Ordering::Greater => Ok(Duration::from_ns(time)), + _ => Err("data not ready"), + } + } +} + +pub(crate) fn raw_code_check_random() -> Duration { + unsafe { + Duration::from_ns(Core::benchmark_raw_code_check_random_ns()) + } +} + +pub(crate) fn short_code_check_random() -> Duration { + unsafe { + Duration::from_ns(Core::benchmark_short_code_check_random_ns()) + } +} + +pub(crate) fn common_code_check_random() -> Duration { + unsafe { + Duration::from_ns(Core::benchmark_common_code_check_random_ns()) + } +} diff --git a/src/rust_ffi/src/benchmark/mod.rs b/src/rust_ffi/src/benchmark/mod.rs index f90e1b1..988c7d1 100644 --- a/src/rust_ffi/src/benchmark/mod.rs +++ b/src/rust_ffi/src/benchmark/mod.rs @@ -1,18 +1,35 @@ mod ffi; - mod chore; use chore::Duration; -use ffi::*; - pub fn demo() { - println!("warm up: {}", warm_up(0x100_0000).to_ms()); + use ffi::*; + + // println!("demo: {}", Duration::from_ps(233 as f64).to_string()); + // println!("demo: {}", Duration::from_ns(233 as f64).to_string()); + // println!("demo: {}", Duration::from_us(233 as f64).to_string()); + // println!("demo: {}", Duration::from_ms(233 as f64).to_string()); + // println!("demo: {}", Duration::from_ms(233000 as f64).to_string()); + + println!("warm up: {}", warm_up(0x100_0000)); + + println!("range flip: {}", range_flip()); + + println!("basic ranges: {}", basic_ranges().unwrap()); + println!("all cases: {}", all_cases().unwrap()); + + preparation(); + + println!("raw code check: {}", raw_code_check().unwrap()); + println!("short code check: {}", short_code_check().unwrap()); + println!("common code check: {}", common_code_check().unwrap()); - println!("range flip: {}", range_flip().to_ns()); + println!("raw code check random: {}", raw_code_check_random()); + println!("short code check random: {}", short_code_check_random()); + println!("common code check random: {}", common_code_check_random()); - println!("basic ranges: {}", basic_ranges().unwrap().to_ms()); - println!("all cases: {}", all_cases().unwrap().to_ms()); + println!("benchmark complete"); }