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);
}

§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

§Requirements

  • The inventory crate must be in your dependency tree
  • Functions must have no parameters and return ()
  • The function should not panic during normal execution

§Example: Multiple Benchmarks

use mobench_sdk::benchmark;

#[benchmark]
fn benchmark_sorting() {
    let mut data: Vec<i32> = (0..1000).rev().collect();
    data.sort();
    std::hint::black_box(data);
}

#[benchmark]
fn benchmark_hashing() {
    use std::collections::hash_map::DefaultHasher;
    use std::hash::{Hash, Hasher};

    let mut hasher = DefaultHasher::new();
    "hello world".hash(&mut hasher);
    std::hint::black_box(hasher.finish());
}

Both functions will be registered with names like:

  • my_crate::benchmark_sorting
  • my_crate::benchmark_hashing

§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

Note: The mobench-runner crate has been consolidated into mobench-sdk as the timing module.

Attribute Macros§

benchmark
Marks a function as a benchmark for mobile execution.