gglib_core/utils/timing.rs
1//! Timing utility shared across crates that measure tool execution latency.
2
3use std::time::Instant;
4
5/// Convert `Instant::elapsed()` to whole milliseconds, clamping to `u64::MAX`.
6///
7/// Used wherever `wait_ms` / `duration_ms` fields are populated so that the
8/// same `u64::try_from(…).unwrap_or(u64::MAX)` boilerplate is not repeated.
9#[inline]
10pub fn elapsed_ms(start: Instant) -> u64 {
11 u64::try_from(start.elapsed().as_millis()).unwrap_or(u64::MAX)
12}
13
14#[cfg(test)]
15mod tests {
16 use super::*;
17
18 #[test]
19 fn elapsed_ms_returns_small_value_immediately() {
20 let start = Instant::now();
21 let ms = elapsed_ms(start);
22 assert!(
23 ms < 1000,
24 "elapsed_ms should be near zero for an immediate call"
25 );
26 }
27}