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.
Installation
Run
or add the following to your Cargo.toml:
[]
= "1"
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 using [print_bench_fct_results]. - Print the benchmark of functions with the macro attribute
#[auto_bench_fct_hy]grouped by function and call stack context using [print_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.
License
This library is licensed under the GNU General Public License v3.0 (GPL-3.0).