Crates.io | slow_function_warning |
lib.rs | slow_function_warning |
version | |
source | src |
created_at | 2024-10-31 00:16:52.051448 |
updated_at | 2024-11-30 14:17:24.638266 |
description | A simple macro that prints a warning if a function takes longer than expected |
homepage | |
repository | https://github.com/ironpeak/slow_function_warning |
max_upload_size | |
id | 1429387 |
Cargo.toml error: | TOML parse error at line 18, column 1 | 18 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include` |
size | 0 |
This crate provides a procedural macro to inject timers into functions and print a warning if it takes longer than expected. It can be particularly useful for debugging performance issues during development.
This is not meant to be a benchmarking tool, but rather a way to detect potential performance issues in your code.
For example my use case was for developing a game in Bevy and I've added this to all my systems to detect if any game system function takes longer than a 1ms.
Add the following to your Cargo.toml
:
[dependencies]
slow_function_warning = "0.4.0"
# For wasm targets
[target.'cfg(target_family = "wasm")'.dependencies]
web-time = "1"
#[slow_function_warning(1000ms)] // Warn if the function takes longer than 1000 milliseconds
fn example_function() {
// Function implementation
}
The warning is not on by default and is only recommended for debugging purposes. To enable it use the slow_function_warning
feature.
cargo run --features slow_function_warning/enabled
// Warn if the function takes longer than a second with a custom message
#[slow_function_warning(1s, println!("Function {function} took too long!"))]
fn example_function() {
// Function implementation
}
You can also use the function parameters in your message:
// Warn if the function takes longer than a second with a custom message
#[debug_slow_function_warning(1s, println!("Function {function} took {millis} for {} values!", values.len()))]
fn sort(values: &Vec<u32>) {
// Function implementation
}
You can specify the duration using numeric literals followed by a suffix:
ns
for nanosecondsms
for millisecondss
for secondsm
for minutesh
for hoursd
for daysmodule: String
- The name of the modulefunction: String
- The name of the functionelapsed: Duration
- The elapsed timenanos: u64
- The elapsed time in nanosecondsns: u64
- The elapsed time in nanosecondsmillis: u64
- The elapsed time in millisecondsms: u64
- The elapsed time in millisecondssecs: u64
- The elapsed time in secondss: u64
- The elapsed time in secondsThis is a procedural macro that takes the content of a function and places it in a closure, executes it and times how long it took.
// Warn if the function takes longer than a second with a custom message
#[debug_slow_function_warning(1s, println!("Function {function} took too long!"))]
fn example_function() {
let x = 10;
}
Becomes:
fn example_function() {
let closure = || {
let x = 10;
};
#[cfg(not(target_family = "wasm"))]
let start = std::time::Instant::now();
#[cfg(target_family = "wasm")]
let start = web_time::Instant::now();
let result = closure();
if start.elapsed().as_nanos() > 1000000 {
let module = "module name";
let function = "example_function";
let elapsed = start.elapsed();
let ns = elapsed.as_nanos();
let nanos = ns;
let ms = elapsed.as_millis();
let millis = ms;
let s = elapsed.as_secs();
let secs = s;
println!("Function {function} took too long!")
}
result
}