mobench-macros-0.1.2 has been yanked.
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.
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"
= "0.1" # 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!
Requirements
- Functions must be regular functions (not async)
- Functions should not take parameters
- 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 - SDK for integrating benchmarks
- mobench-macros - This crate (proc macros)
- mobench-runner - Timing harness
See Also
- mobench-sdk for the complete SDK
- mobench for the CLI tool
- inventory for the registration mechanism
License
Licensed under either of:
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
at your option.