gravel_core/
performance.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
use log::Level;
use std::fmt::{self, Arguments, Display, Formatter};
use std::time::Instant;

#[derive(Debug)]
pub struct Stopwatch {
	begin: Instant,
}

impl Stopwatch {
	pub fn start() -> Self {
		Self { begin: Instant::now() }
	}
}

impl Display for Stopwatch {
	fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
		// convert micros -> millis so we get decimal values
		let micros = self.begin.elapsed().as_micros() as f32;
		let millis = micros / 1000.0;
		write!(f, "{millis}ms")
	}
}

#[derive(Debug)]
pub struct Timed {
	message: String,
	level: Level,
	stopwatch: Stopwatch,
}

impl Timed {
	pub fn new(level: Level, args: Arguments<'_>) -> Option<Self> {
		if level > log::STATIC_MAX_LEVEL || level > log::max_level() {
			return None;
		}

		Some(Self {
			message: args.to_string(),
			level,
			stopwatch: Stopwatch::start(),
		})
	}
}

impl Drop for Timed {
	fn drop(&mut self) {
		log::log!(self.level, "{} {}", self.message, self.stopwatch);
	}
}

#[macro_export]
macro_rules! timed {
	( ($($arg:tt)*), $body:block ) => { timed!(::log::Level::Trace, ::std::format_args!($($arg)*), $body) };
	( $msg:literal, $body:block ) => { timed!(::log::Level::Trace, ::std::format_args!($msg), $body) };
	( $level:expr, ($($arg:tt)*), $body:block ) => { timed!($level, ::std::format_args!($($arg)*), $body) };
	( $level:expr, $msg:literal, $body:block ) => { timed!($level, ::std::format_args!($msg), $body) };

	( $level:expr, $msg:expr, $body:block ) => {{
		let __time = $crate::performance::Timed::new($level, $msg);

		$body
	}};
}