mobench-macros
Procedural macros for the mobench mobile benchmarking framework.
This crate provides the #[benchmark] attribute macro that automatically registers functions for mobile benchmarking. It uses compile-time registration via the inventory crate to build a registry of benchmark functions.
In mobench 0.1.22, benchmarks annotated with these macros are discovered through the CLI's config-first resolver, so non-legacy crate layouts work with mobench.toml, --project-root, and --crate-path.
Features
#[benchmark]attribute: Mark functions as benchmarks- Automatic registration: No manual registry maintenance required
- Type safety: Compile-time validation of benchmark functions
- Zero runtime overhead: Registration happens at compile time
Usage
Add this to your Cargo.toml:
[]
= "0.1.22"
= "0.1.22" # For the runtime
Basic Example
use benchmark;
With mobench-sdk
The macros work seamlessly with mobench-sdk:
use benchmark;
use ;
How It Works
The #[benchmark] macro:
- Preserves your function: The original function remains unchanged
- Generates registration code: Creates an
inventory::submit!call - Wraps in closure: Converts your function into a callable closure
- Registers at compile time: Adds to the global benchmark registry
Macro Expansion
When you write:
The macro expands to something like:
submit!
Setup and Teardown
For benchmarks that need expensive setup that shouldn't be measured:
use benchmark;
Per-Iteration Setup
For benchmarks that mutate their input (e.g., sorting):
Setup and Teardown
Requirements
- Functions must be regular functions (not async)
- Without setup: no parameters allowed
- With setup: exactly one parameter (reference to setup result, or owned for per_iteration)
- Functions should use
std::hint::black_box()to prevent optimization of results
Best Practices
Prevent Compiler Optimization
Always use black_box for benchmark results:
use benchmark;
Benchmark Naming
Use descriptive names that indicate what's being measured:
Isolate Benchmarks
Keep benchmarks focused on one operation:
// Good: Measures one thing
// Bad: Measures multiple things
Part of mobench
This crate is part of the mobench ecosystem for mobile benchmarking:
- mobench - CLI tool for running benchmarks
- mobench-sdk - Core SDK with timing harness, build automation, and codegen
- mobench-macros - This crate (proc macros)
See Also
- mobench-sdk for the complete SDK
- mobench for the CLI tool
- inventory for the registration mechanism
License
Licensed under the MIT License. See LICENSE.md for details.
Copyright (c) 2026 World Foundation