Skip to main content

Crate mobench_macros

Crate mobench_macros 

Source
Expand description

§mobench-macros

Crates.io Documentation MIT License

Procedural macros for the mobench mobile benchmarking SDK.

This crate provides the #[benchmark] attribute macro that marks functions for mobile benchmarking. Functions annotated with this macro are automatically registered in a global registry and can be discovered and executed at runtime.

§Usage

Most users should import the macro via mobench-sdk rather than using this crate directly:

use mobench_sdk::benchmark;

#[benchmark]
fn my_benchmark() {
    // Your benchmark code here
    let result = expensive_computation();
    std::hint::black_box(result);
}

§Setup and Teardown

For benchmarks that need expensive setup that shouldn’t be measured:

use mobench_sdk::benchmark;

fn setup_data() -> Vec<u8> {
    vec![0u8; 1_000_000]  // Not measured
}

#[benchmark(setup = setup_data)]
fn hash_benchmark(data: &Vec<u8>) {
    std::hint::black_box(compute_hash(data));  // Only this is measured
}

§How It Works

The #[benchmark] macro:

  1. Preserves the original function - The function remains callable as normal
  2. Registers with inventory - Creates a static registration that the SDK discovers at runtime
  3. Captures the fully-qualified name - Uses module_path!() to generate unique names like my_crate::my_module::my_benchmark
  4. Handles setup/teardown - If specified, wraps the benchmark with setup/teardown that aren’t timed

§Requirements

  • The inventory crate must be in your dependency tree
  • Simple benchmarks: no parameters, returns ()
  • With setup: exactly one parameter (reference to setup result), returns ()
  • The function should not panic during normal execution

§Crate Ecosystem

This crate is part of the mobench ecosystem:

  • mobench-sdk - Core SDK with timing harness (re-exports this macro)
  • mobench - CLI tool
  • mobench-macros (this crate) - Proc macros

Attribute Macros§

benchmark
Marks a function as a benchmark for mobile execution.