#[cfg(feature = "time_profiling")]
use thag_profiler::{enable_profiling, end, file_stem_from_path_str, profile};
#[cfg(feature = "time_profiling")]
#[enable_profiling]
fn test_time_section() {
profile!(test_time_section, time);
let profile = test_time_section.as_ref().unwrap();
assert_eq!(profile.get_profile_type(), ProfileType::Time);
assert!(!profile.is_detailed_memory());
assert_eq!(
profile.section_name(),
Some("test_time_section".to_string())
);
let file_name = profile.file_name();
assert_eq!(file_name, "test_profile_section");
assert_eq!(file_name, file_stem_from_path_str(file!()));
assert_eq!(profile.start_line(), None);
assert_eq!(profile.end_line(), None);
let _ = (0..1000).sum::<i32>();
end!(test_time_section);
eprintln!("test_time_section passed");
}
#[cfg(feature = "full_profiling")]
#[enable_profiling(memory)]
fn test_memory_summary_section() {
profile!(test_memory_summary, mem_summary);
let profile = test_memory_summary.as_ref().unwrap();
assert_eq!(profile.get_profile_type(), ProfileType::Memory);
assert!(!profile.is_detailed_memory());
assert_eq!(
profile.section_name(),
Some("test_memory_summary".to_string())
);
assert!(profile.start_line().is_some());
let data = vec![0u8; 1024];
let _more_data = vec![0u8; 2048];
assert_eq!(data.len(), 1024);
end!(test_memory_summary);
eprintln!("test_memory_summary_section passed");
}
#[cfg(feature = "full_profiling")]
#[enable_profiling(memory)]
fn test_memory_detail_section() {
profile!(test_memory_detail, mem_detail);
let profile = test_memory_detail.as_ref().unwrap();
assert_eq!(profile.get_profile_type(), ProfileType::Memory);
assert!(profile.is_detailed_memory());
assert_eq!(
profile.section_name(),
Some("test_memory_detail".to_string())
);
let start_line = profile.start_line().unwrap();
assert!(start_line > 0);
let data1 = vec![1u8; 1024];
let data2 = vec![2u8; 2048];
let data3 = vec![3u8; 4096];
assert_eq!(data1.len() + data2.len() + data3.len(), 7168);
end!(test_memory_detail);
eprintln!("test_memory_detail_section passed");
}
#[cfg(feature = "full_profiling")]
#[enable_profiling]
#[allow(clippy::cast_sign_loss)]
fn test_both_profiling_section() {
profile!(test_both, time, mem_summary);
let profile = test_both.as_ref().unwrap();
assert_eq!(profile.get_profile_type(), ProfileType::Both);
assert!(!profile.is_detailed_memory());
let result = (0..1000).sum::<i32>();
let data = vec![0u8; result as usize % 10000];
assert!(data.capacity() > 0);
end!(test_both);
eprintln!("test_both_profiling_section passed");
}
#[cfg(feature = "full_profiling")]
#[enable_profiling]
#[allow(clippy::suspicious_map)]
fn test_both_detailed_section() {
profile!(test_both_detailed, time, mem_detail);
let profile = test_both_detailed.as_ref().unwrap();
assert_eq!(profile.get_profile_type(), ProfileType::Both);
assert!(profile.is_detailed_memory());
assert_eq!((0..1000).map(|i| i.to_string()).count(), 1000);
end!(test_both_detailed);
eprintln!("test_both_detailed_section passed");
}
#[cfg(feature = "time_profiling")]
#[cfg_attr(feature = "full_profiling", enable_profiling(memory))]
#[cfg_attr(not(feature = "full_profiling"), enable_profiling(time))]
fn test_global_profile_section() {
profile!(test_global, global);
let profile = test_global.as_ref().unwrap();
#[cfg(feature = "full_profiling")]
assert_eq!(profile.get_profile_type(), ProfileType::Memory);
#[cfg(not(feature = "full_profiling"))]
assert_eq!(profile.get_profile_type(), ProfileType::Time);
end!(test_global);
eprintln!("test_global_profile_section passed");
}
#[cfg(feature = "full_profiling")]
#[enable_profiling]
fn test_nested_sections() {
profile!(outer_section, time);
let outer_profile = outer_section.as_ref().unwrap();
assert_eq!(outer_profile.get_profile_type(), ProfileType::Time);
let _ = (0..500).sum::<i32>();
profile!(inner_section, mem_detail);
let inner_profile = inner_section.as_ref().unwrap();
assert_eq!(inner_profile.get_profile_type(), ProfileType::Memory);
assert!(inner_profile.is_detailed_memory());
let inner_data = vec![0u8; 4096];
assert_eq!(inner_data.len(), 4096);
end!(inner_section);
let outer_data = vec![1u8; 2048];
assert_eq!(outer_data.len(), 2048);
end!(outer_section);
eprintln!("test_nested_sections passed");
}
#[cfg(feature = "full_profiling")]
#[enable_profiling]
fn test_unbounded_section() {
{
profile!(unbounded_section, mem_detail, unbounded);
let profile = unbounded_section.as_ref().unwrap();
assert_eq!(profile.get_profile_type(), ProfileType::Memory);
assert!(profile.is_detailed_memory());
assert!(profile.start_line().is_some());
assert_eq!(profile.end_line(), None);
let data = vec![0u8; 8192];
assert_eq!(data.len(), 8192);
}
#[allow(unused_variables)]
let is_dropped = if cfg!(feature = "time_profiling") {
let unbounded_section = 42; unbounded_section == 42
} else {
true
};
assert!(is_dropped);
eprintln!("test_unbounded_section passed");
}
#[allow(dead_code)]
fn get_section_profile_name(section_name: &str) -> String {
format!("{}::{section_name}", module_path!())
}
#[test]
#[cfg(feature = "time_profiling")]
fn test_profile_section_behavior() {
use thag_profiler::{enable_profiling, profiling::set_global_profile_type, ProfileType};
#[cfg(feature = "full_profiling")]
enable_memory_profiling_for_test();
#[cfg(not(feature = "full_profiling"))]
enable_time_profiling_for_test();
#[cfg(feature = "full_profiling")]
#[enable_profiling(memory)]
fn enable_memory_profiling_for_test() {
eprintln!("In test_profile_section_behavior::enable_memory_profiling_for_test");
}
#[enable_profiling(time)]
fn enable_time_profiling_for_test() {}
test_time_section();
set_global_profile_type(ProfileType::None);
test_global_profile_section();
#[cfg(feature = "full_profiling")]
{
test_memory_summary_section();
test_memory_detail_section();
test_both_profiling_section();
test_both_detailed_section();
test_nested_sections();
test_unbounded_section();
}
println!("All profile section tests passed!");
}