Skip to content
This repository has been archived by the owner on Jan 13, 2025. It is now read-only.

Add Measure::as_duration() #25942

Merged
merged 1 commit into from
Jun 14, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions measure/src/measure.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use {
solana_sdk::timing::duration_as_ns,
std::{fmt, time::Instant},
std::{
fmt,
time::{Duration, Instant},
},
};

#[derive(Debug)]
Expand Down Expand Up @@ -38,6 +41,10 @@ impl Measure {
pub fn as_s(&self) -> f32 {
self.duration as f32 / (1000.0f32 * 1000.0f32 * 1000.0f32)
}

pub fn as_duration(&self) -> Duration {
Duration::from_nanos(self.as_ns())
}
}

impl fmt::Display for Measure {
Expand All @@ -58,10 +65,7 @@ impl fmt::Display for Measure {

#[cfg(test)]
mod tests {
use {
super::*,
std::{thread::sleep, time::Duration},
};
use {super::*, std::thread::sleep};

#[test]
fn test_measure() {
Expand All @@ -71,6 +75,10 @@ mod tests {
assert!(measure.as_s() >= 0.99f32 && measure.as_s() <= 1.01f32);
assert!(measure.as_ms() >= 990 && measure.as_ms() <= 1_010);
assert!(measure.as_us() >= 999_000 && measure.as_us() <= 1_010_000);
assert!(
measure.as_duration() >= Duration::from_millis(990)
&& measure.as_duration() <= Duration::from_millis(1_010)
);
}

#[test]
Expand Down