exec_time 0.1.7

Attribute macro for timing sync and async Rust functions with labels, units, and slow-call thresholds
Documentation

exec_time

Attribute macro for printing sync and async function execution time.

License: MIT Crates.io Build Status

Install

[dependencies]
exec_time = "0.1.7"

MSRV: Rust 1.88.

Default

use exec_time::exec_time;

#[exec_time]
fn login() {
    std::thread::sleep(std::time::Duration::from_millis(100));
}
[exec_time] login took 102 ms

Slow Calls

use exec_time::exec_time;

#[exec_time(name = "cache.lookup", unit = "us", log_over = "500us")]
fn lookup() {}

#[exec_time(name = "db.query", warn_over = "250ms")]
fn query_db() {}
[exec_time] cache.lookup took 734 us
[exec_time][warn] db.query took 312 ms

Options

  • print = "always" | "debug" | "never": controls whether timing is emitted. Default: always.
  • name = "...": replaces the generated label.
  • prefix = "...", suffix = "...": build the label as <prefix>::<function>::<suffix>. Ignored when name is set.
  • unit = "ns" | "us" | "ms" | "s": output unit. Default: ms.
  • log_over = "...": print only when execution time meets or exceeds the threshold.
  • warn_over = "...": write a warning to stderr only when execution time meets or exceeds the threshold.

Threshold values support ns, us, ms, and s, for example 500us, 5ms, or 0.5s.

Rules

  • Default output format: [exec_time] <label> took <value> <unit>
  • warn_over takes precedence over log_over when both thresholds match
  • print = "never" disables all output