Expand description
A library for automatically benchmarking function call count and execution time in Rust.
Minimal setup is required to use this library, a macro attribute is provided to enable automatic benchmarking of functions, and a function to print the results.
- Enable the
disablefeature to disable the library, which will make the macros and functions to do nothing, allowing to keep the code in production without performance overhead. - Enable the
disable_hyfeature to disable only the call stack hierarchy tracking, which will make the macros#[auto_bench_fct_hy]behave like#[auto_bench_fct]. - Enable the
logfeature to log the results using thelogcrate with the info level.
This crate supports multithreaded programs, and uses wall time to measure the execution time of functions, which is suitable for most use cases.
§Usage
§Define which functions to benchmark
Add the #[auto_bench_fct] macro attribute to a function to track its execution time and calls
count, or addd the #[auto_bench_fct_hy] macro attribute to track its execution time
and calls count both globally and in the call stack hierarchy.
§Print the results
- Print the benchmark of functions with the macro attribute
#[auto_bench_fct]or#[auto_bench_fct_hy]grouped by function usingprint_bench_fct_results. - Print the benchmark of functions with the macro attribute
#[auto_bench_fct_hy]grouped by function and call stack context usingprint_bench_fct_hy_results.
§Usage recommendations
Use the #[auto_bench_fct_hy] macro attribute for functions you want to benchmark, and
use #[auto_bench_fct] instead only if you want to exclude the function from the call stack
hierarchy.
When not wanting to debug the call stack hierarchy due to the performance overhead,
enable the feature disable_hy.
When not wanting to debug the function calls at all, enable the feature disable.
§Note about recursive functions
Recursive functions are supported and will be tracked correctly, tracking only the root function call execution time. Though, mutual recursion is not supported, meaning that if two functions call each other recursively, it may create a huge number of entries in the metrics.
§Note about performance
This library is designed to be used in development and testing environments.
It may have a large performance overhead, mostly if used on recursive functions
or in functions with a large number of calls and a small execution time.
The macro #[auto_bench_fct_hy] has a larger performance overhead than #[auto_bench_fct]
and should not be used in non-heavy functions.
Structs§
- FUNCTION_
METRICS - Stores the function call count and total duration for each function. The function index is a unique u32 identifier for the function. Mapping: (Function name, Function index) -> (Call count, Total duration)
- FUNCTION_
METRICS_ HIERARCHY - Stores the function call count and total duration for each function in a specific call stack. The function index is a unique u32 identifier for the function. Mapping: (function index stack) -> (Function name, Function index) -> (Call count, Total duration)
Constants§
- CALL_
STACK - Stores the current call stack of function indices. The function index is a unique u32 identifier for the function.
Functions§
- get_
bench_ fct_ hy_ results - Gets the raw function metrics grouped by call stack hierarchy.
The function index is a unique u32 identifier for the function.
Mapping: (function index stack) -> (Function name, Function index) -> (Call count, Total duration)
This map is filled by execution of functions with the macro attribute
#[auto_bench_fct_hy]. - get_
bench_ fct_ results - Gets the raw function metrics.
The function index is a unique u32 identifier for the function.
Mapping: (Function name, Function index) -> (Call count, Total duration)
This map is filled by execution of functions with the macro attribute
#[auto_bench_fct]or#[auto_bench_fct_hy]. - print_
bench_ fct_ hy_ results - Prints the function hierarchy benchmark results in stdout.
If the
logfeature is enabled, it will log the results usinglog::info!. If thedisable_hyfeature is enabled, this function does nothing. If thedisablefeature is enabled, this function does nothing. - print_
bench_ fct_ results - Prints the function benchmark results in stdout.
If the
logfeature is enabled, it will log the results usinglog::info!. If thedisablefeature is enabled, this function does nothing.
Attribute Macros§
- auto_
bench_ fct - Add the
#[auto_bench_fct]macro attribute to a function to enable automatic benchmarking. It will store in a map the number of calls and the total duration of the function execution. - auto_
bench_ fct_ hy - Add the
#[auto_bench_fct_hy]macro attribute to a function to enable automatic benchmarking with hierarchy computation. It will do the same as#[auto_bench_fct], but it will also track he call stack, allowing to display the hierarchy of function calls.