use logwise::{Level, context::Context, declare_logging_domain, perfwarn_begin_if};
use std::thread;
use std::time::Duration;
declare_logging_domain!();
#[test]
fn test_perfwarn_if_logs_when_slow() {
Context::reset("test_perfwarn_if_slow".to_string());
let threshold = Duration::from_millis(10);
let interval = perfwarn_begin_if!(threshold, "slow_operation");
thread::sleep(Duration::from_millis(50));
drop(interval);
}
#[test]
fn test_perfwarn_if_skips_when_fast() {
Context::reset("test_perfwarn_if_fast".to_string());
let threshold = Duration::from_millis(100);
let interval = perfwarn_begin_if!(threshold, "fast_operation");
thread::sleep(Duration::from_millis(1));
drop(interval);
}
#[test]
fn test_perfwarn_if_statistics() {
Context::reset("test_perfwarn_if_stats".to_string());
let ctx = Context::new_task(
Some(Context::current()),
"stats_task".to_string(),
Level::Info,
true,
);
ctx.set_current();
{
let threshold = Duration::from_millis(100);
let interval = perfwarn_begin_if!(threshold, "fast_stat");
thread::sleep(Duration::from_millis(1));
drop(interval);
}
{
let threshold = Duration::from_millis(10);
let interval = perfwarn_begin_if!(threshold, "slow_stat");
thread::sleep(Duration::from_millis(50));
drop(interval);
}
}