pub struct TimeUse { /* private fields */ }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>:
Noneoperates on the global timer.Some("foo")operates on a per-tag span tracked in aHashMap.
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
impl TimeUse
Sourcepub fn new() -> Self
pub fn new() -> Self
Construct a new profiler. The global timer starts immediately.
Examples found in repository?
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}Sourcepub fn start(&mut self, tag: Option<&str>)
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?
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}Sourcepub fn stop(&mut self, tag: Option<&str>) -> Duration
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
tagwas never started, the global start is used as the origin and “now” is recorded as the end.
Examples found in repository?
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}Sourcepub fn elapsed(&self, tag: Option<&str>) -> Duration
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.