Skip to main content

TimeUse

Struct TimeUse 

Source
pub struct TimeUse { /* private fields */ }
Available on crate feature time only.
Expand description

A multi-stage profiler with named tags.

Mirrors Node’s XTimeUse. On construction it records a “global” start time. Every method takes a tag: Option<&str>:

  • None operates on the global timer.
  • Some("foo") operates on a per-tag span tracked in a HashMap.

Idempotent stop: once a span has been stopped, subsequent TimeUse::stop / TimeUse::elapsed calls return the cached duration.

Fallback: calling TimeUse::stop / TimeUse::elapsed with a tag that was never started uses the global start as the origin, so you can ad-hoc query “how long since the profiler was created” under any label.

use bento_kit::time::TimeUse;
let mut t = TimeUse::new();

t.start(Some("connect"));
// ... establish connection ...
let connect_ms = t.stop(Some("connect")).as_millis();

t.start(Some("query"));
// ... run query ...
let query_ms = t.stop(Some("query")).as_millis();

let total_ms = t.stop(None).as_millis();

Implementations§

Source§

impl TimeUse

Source

pub fn new() -> Self

Construct a new profiler. The global timer starts immediately.

Examples found in repository?
examples/time_demo.rs (line 13)
4fn main() {
5    use bento_kit::time::{format_now_local, format_now_utc, now_millis, TimeUse};
6    use std::thread::sleep;
7    use std::time::Duration;
8
9    println!("now (utc)   = {}", format_now_utc("%Y-%m-%d %H:%M:%S"));
10    println!("now (local) = {}", format_now_local("%Y-%m-%d %H:%M:%S %Z"));
11    println!("now millis  = {}", now_millis());
12
13    let mut t = TimeUse::new();
14
15    t.start(Some("connect"));
16    sleep(Duration::from_millis(20));
17    println!("connect = {:?}", t.stop(Some("connect")));
18
19    t.start(Some("query"));
20    sleep(Duration::from_millis(40));
21    println!("query   = {:?}", t.stop(Some("query")));
22
23    sleep(Duration::from_millis(10));
24    println!("total   = {:?}", t.stop(None));
25}
Source

pub fn start(&mut self, tag: Option<&str>)

Reset the timer for the given tag (or the global timer if None) to “now”. Other tags are unaffected.

Examples found in repository?
examples/time_demo.rs (line 15)
4fn main() {
5    use bento_kit::time::{format_now_local, format_now_utc, now_millis, TimeUse};
6    use std::thread::sleep;
7    use std::time::Duration;
8
9    println!("now (utc)   = {}", format_now_utc("%Y-%m-%d %H:%M:%S"));
10    println!("now (local) = {}", format_now_local("%Y-%m-%d %H:%M:%S %Z"));
11    println!("now millis  = {}", now_millis());
12
13    let mut t = TimeUse::new();
14
15    t.start(Some("connect"));
16    sleep(Duration::from_millis(20));
17    println!("connect = {:?}", t.stop(Some("connect")));
18
19    t.start(Some("query"));
20    sleep(Duration::from_millis(40));
21    println!("query   = {:?}", t.stop(Some("query")));
22
23    sleep(Duration::from_millis(10));
24    println!("total   = {:?}", t.stop(None));
25}
Source

pub fn stop(&mut self, tag: Option<&str>) -> Duration

Stop the timer for the given tag (or the global timer if None) and return its elapsed duration.

  • Idempotent: subsequent calls return the cached duration.
  • Tag fallback: if tag was never started, the global start is used as the origin and “now” is recorded as the end.
Examples found in repository?
examples/time_demo.rs (line 17)
4fn main() {
5    use bento_kit::time::{format_now_local, format_now_utc, now_millis, TimeUse};
6    use std::thread::sleep;
7    use std::time::Duration;
8
9    println!("now (utc)   = {}", format_now_utc("%Y-%m-%d %H:%M:%S"));
10    println!("now (local) = {}", format_now_local("%Y-%m-%d %H:%M:%S %Z"));
11    println!("now millis  = {}", now_millis());
12
13    let mut t = TimeUse::new();
14
15    t.start(Some("connect"));
16    sleep(Duration::from_millis(20));
17    println!("connect = {:?}", t.stop(Some("connect")));
18
19    t.start(Some("query"));
20    sleep(Duration::from_millis(40));
21    println!("query   = {:?}", t.stop(Some("query")));
22
23    sleep(Duration::from_millis(10));
24    println!("total   = {:?}", t.stop(None));
25}
Source

pub fn elapsed(&self, tag: Option<&str>) -> Duration

Peek the elapsed duration without recording an end time.

Returns the cached duration if the timer has already been stopped. Falls back to the global start if the tag was never started.

Source

pub fn restart(&mut self, tag: Option<&str>) -> Duration

Stop and immediately restart the timer for the given tag (or the global timer if None). Returns the duration of the just-finished span.

Source

pub fn tag_count(&self) -> usize

Number of tags currently tracked.

Trait Implementations§

Source§

impl Debug for TimeUse

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for TimeUse

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V