solana_measure/macros.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 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
/// Measure this expression
///
/// Use `measure_time!()` when you have an expression that you want to measure. `measure_time!()` will start
/// a new [`Measure`], evaluate your expression, stop the [`Measure`], and then return the
/// [`Measure`] object along with your expression's return value.
///
/// Use `measure_us!()` when you want to measure an expression in microseconds.
///
/// Use `meas_dur!()` when you want to measure an expression and get the Duration.
///
/// [`Measure`]: crate::measure::Measure
///
/// # Examples
///
/// ```
/// // Measure functions
/// # use solana_measure::{measure_time, measure_us, meas_dur};
/// # fn foo() {}
/// # fn bar(x: i32) {}
/// # fn add(x: i32, y: i32) -> i32 {x + y}
/// let (result, measure) = measure_time!(foo(), "foo takes no parameters");
/// let (result, measure) = measure_time!(bar(42), "bar takes one parameter");
/// let (result, measure) = measure_time!(add(1, 2), "add takes two parameters and returns a value");
/// let (result, measure_us) = measure_us!(add(1, 2));
/// let (result, duration) = meas_dur!(add(1, 2));
/// # assert_eq!(result, 1 + 2);
/// ```
///
/// ```
/// // Measure methods
/// # use solana_measure::{measure_time, measure_us, meas_dur};
/// # struct Foo {
/// # f: i32,
/// # }
/// # impl Foo {
/// # fn frobnicate(&self, bar: i32) -> i32 {
/// # self.f * bar
/// # }
/// # }
/// let foo = Foo { f: 42 };
/// let (result, measure) = measure_time!(foo.frobnicate(2), "measure methods");
/// let (result, measure_us) = measure_us!(foo.frobnicate(2));
/// let (result, duration) = meas_dur!(foo.frobnicate(2));
/// # assert_eq!(result, 42 * 2);
/// ```
///
/// ```
/// // Measure expression blocks
/// # use solana_measure::measure_time;
/// # fn complex_calculation() -> i32 { 42 }
/// # fn complex_transform(x: i32) -> i32 { x + 3 }
/// # fn record_result(y: i32) {}
/// let (result, measure) = measure_time!(
/// {
/// let x = complex_calculation();
/// # assert_eq!(x, 42);
/// let y = complex_transform(x);
/// # assert_eq!(y, 42 + 3);
/// record_result(y);
/// y
/// },
/// "measure a block of many operations",
/// );
/// # assert_eq!(result, 42 + 3);
/// ```
///
/// ```
/// // The `name` parameter is optional
/// # use solana_measure::{measure_time, measure_us};
/// # fn meow() {};
/// let (result, measure) = measure_time!(meow());
/// let (result, measure_us) = measure_us!(meow());
/// ```
#[macro_export]
macro_rules! measure_time {
($val:expr, $name:tt $(,)?) => {{
let mut measure = $crate::measure::Measure::start($name);
let result = $val;
measure.stop();
(result, measure)
}};
($val:expr) => {
measure_time!($val, "")
};
}
#[macro_export]
macro_rules! measure_us {
($expr:expr) => {{
let (result, duration) = $crate::meas_dur!($expr);
(result, duration.as_micros() as u64)
}};
}
/// Measures how long it takes to execute an expression, and returns a Duration
///
/// # Examples
///
/// ```
/// # use solana_measure::meas_dur;
/// # fn meow(x: i32, y: i32) -> i32 {x + y}
/// let (result, duration) = meas_dur!(meow(1, 2) + 3);
/// # assert_eq!(result, 1 + 2 + 3);
/// ```
//
// The macro name, `meas_dur`, is "measure" + "duration".
// When said aloud, the pronunciation is close to "measure".
#[macro_export]
macro_rules! meas_dur {
($expr:expr) => {{
let start = std::time::Instant::now();
let result = $expr;
(result, start.elapsed())
}};
}
#[cfg(test)]
mod tests {
use std::{thread::sleep, time::Duration};
fn my_multiply(x: i32, y: i32) -> i32 {
x * y
}
fn square(x: i32) -> i32 {
my_multiply(x, x)
}
struct SomeStruct {
x: i32,
}
impl SomeStruct {
fn add_to(&self, x: i32) -> i32 {
x + self.x
}
}
#[test]
fn test_measure_macro() {
// Ensure that the measurement side actually works
{
let (_result, measure) = measure_time!(sleep(Duration::from_secs(1)), "test");
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);
}
// Ensure that the macro can be called with functions
{
let (result, _measure) = measure_time!(my_multiply(3, 4), "test");
assert_eq!(result, 3 * 4);
let (result, _measure) = measure_time!(square(5), "test");
assert_eq!(result, 5 * 5)
}
// Ensure that the macro can be called with methods
{
let some_struct = SomeStruct { x: 42 };
let (result, _measure) = measure_time!(some_struct.add_to(4), "test");
assert_eq!(result, 42 + 4);
}
// Ensure that the macro can be called with blocks
{
let (result, _measure) = measure_time!({ 1 + 2 }, "test");
assert_eq!(result, 3);
}
// Ensure that the macro can be called with a trailing comma
{
let (result, _measure) = measure_time!(square(5), "test",);
assert_eq!(result, 5 * 5)
}
// Ensure that the macro can be called without a name
{
let (result, _measure) = measure_time!(square(5));
assert_eq!(result, 5 * 5)
}
}
#[test]
fn test_measure_us_macro() {
// Ensure that the measurement side actually works
{
let (_result, measure) = measure_us!(sleep(Duration::from_secs(1)));
assert!((999_000..=1_010_000).contains(&measure));
}
// Ensure that the macro can be called with functions
{
let (result, _measure) = measure_us!(my_multiply(3, 4));
assert_eq!(result, 3 * 4);
let (result, _measure) = measure_us!(square(5));
assert_eq!(result, 5 * 5)
}
// Ensure that the macro can be called with methods
{
let some_struct = SomeStruct { x: 42 };
let (result, _measure) = measure_us!(some_struct.add_to(4));
assert_eq!(result, 42 + 4);
}
// Ensure that the macro can be called with blocks
{
let (result, _measure) = measure_us!({ 1 + 2 });
assert_eq!(result, 3);
}
}
#[test]
fn test_meas_dur_macro() {
// Ensure that the macro can be called with functions
{
let (result, _duration) = meas_dur!(my_multiply(3, 4));
assert_eq!(result, 3 * 4);
let (result, _duration) = meas_dur!(square(5));
assert_eq!(result, 5 * 5)
}
// Ensure that the macro can be called with methods
{
let some_struct = SomeStruct { x: 42 };
let (result, _duration) = meas_dur!(some_struct.add_to(4));
assert_eq!(result, 42 + 4);
}
// Ensure that the macro can be called with blocks
{
let (result, _duration) = meas_dur!({ 1 + 2 });
assert_eq!(result, 3);
}
}
}