From fd6549a6352633cee7b5b747448129df7a0532ca Mon Sep 17 00:00:00 2001 From: Jiayu Ye Date: Thu, 6 Aug 2020 22:37:43 +0800 Subject: [PATCH] fix(apm): Return the correct time (#400) * fix(apm): Return the correct time * fix lint * add test * fix lint * fix ci * fix lint --- common/apm/src/lib.rs | 3 +++ common/apm/src/metrics.rs | 16 +++++++++++++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/common/apm/src/lib.rs b/common/apm/src/lib.rs index 4279d73ef..35e1b39e8 100644 --- a/common/apm/src/lib.rs +++ b/common/apm/src/lib.rs @@ -1,3 +1,6 @@ +// https://rust-lang.github.io/rust-clippy/master/index.html#float_cmp +#![allow(clippy::float_cmp)] + pub mod metrics; pub use muta_apm; diff --git a/common/apm/src/metrics.rs b/common/apm/src/metrics.rs index 70f281eea..a3b37d7c9 100644 --- a/common/apm/src/metrics.rs +++ b/common/apm/src/metrics.rs @@ -40,7 +40,7 @@ impl From for ProtocolError { impl std::error::Error for Error {} pub fn duration_to_sec(d: Duration) -> f64 { - d.as_secs_f64() + (f64::from(d.subsec_nanos()) / 1e9) + d.as_secs_f64() } pub fn all_metrics() -> ProtocolResult> { @@ -54,3 +54,17 @@ pub fn all_metrics() -> ProtocolResult> { Ok(encoded_metrics) } + +#[cfg(test)] +mod tests { + use super::duration_to_sec; + use std::time::Duration; + + #[test] + fn test_duration_to_sec() { + let d = Duration::from_millis(1110); + let sec = duration_to_sec(d); + + assert_eq!(sec, 1.11 as f64); + } +}