Micrometer

Crates.io Documentation

Profiling for fast, high frequency events in multithreaded applications with low overhead

Important: enabling data collection

By default every measure is a no-op, to measure and consume measures, enable the enable feature. This is done to allow libs to instrument their code without runitme costs if no one is using the measures.

Definitions

Examples

Measuring the duration of a loop

``rs for _ in 0..100 { // Define aTrackPointnamed "loop_duration", get (or init) theTrackfor the // current thread, then create a span and assign it to a new local variable called //loopduration` micrometer::span!(loopduration);

std::hint::black_box("do something");
// `loop_duration` is automatically dropped recording the measure

} // Print a summary of the measurements micrometer::summary(); ```

Measuring the duration of a loop, threaded

rs std::thread::scope(|s| { for t in 1..=4 { s.spawn(move || { for _ in 0..(10 * t) { micrometer::span!(); // Name automatically assigned to source file and line std::hint::black_box("do something"); } }); } }); // Print a summary of the measurements micrometer::summary(); // Print a summary of the measurements, aggregating measures for the same location micrometer::summary_grouped();

Measuring the duration of an expression

rs // This works like the `dbg!` macro, allowing you to transparently wrap an expression: // a span is created, the expression is executed, then the span is closed and the result // of the expression is passed along let a = micrometer::span!(5 * 5, "fives_sq"); let b = micrometer::span!(a * a); // Name automatically assigned to source file and line assert_eq!(a, 25); assert_eq!(b, 25 * 25);

Measuring a code segment

rs let a = 5; micrometer::span!(guard, "a_sq"); let b = a * a; drop(guard); // Measurement stops here let c = b * a;