Expand description
§mobench-macros
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:
- Preserves the original function - The function remains callable as normal
- Registers with inventory - Creates a static registration that the SDK discovers at runtime
- Captures the fully-qualified name - Uses
module_path!()to generate unique names likemy_crate::my_module::my_benchmark - Handles setup/teardown - If specified, wraps the benchmark with setup/teardown that aren’t timed
§Requirements
- The
inventorycrate 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 toolmobench-macros(this crate) - Proc macros
Attribute Macros§
- benchmark
- Marks a function as a benchmark for mobile execution.