use std::thread;
use std::time::Duration;
#[cfg(feature = "logging")]
use scirs2_core::logging::{LogLevel, Logger, ProgressTracker};
#[cfg(feature = "profiling")]
use scirs2_core::profiling::{Profiler, Timer};
#[cfg(feature = "random")]
use scirs2_core::random::{CoreRandom, DistributionExt, Random};
#[cfg(feature = "memory_management")]
use scirs2_core::memory::{BufferPool, ZeroCopyView};
#[cfg(feature = "types")]
use scirs2_core::types::NumericConversion;
#[allow(dead_code)]
fn main() {
println!("Integrated Features Example");
#[cfg(all(
feature = "logging",
feature = "profiling",
feature = "random",
feature = "memory_management",
feature = "types"
))]
run_integrated_example();
#[cfg(not(all(
feature = "logging",
feature = "profiling",
feature = "random",
feature = "memory_management",
feature = "types"
)))]
println!("Not all required features are enabled. Please run with --features=\"logging profiling random memory_management types\"");
}
#[cfg(all(
feature = "logging",
feature = "profiling",
feature = "random",
feature = "memory_management",
feature = "types"
))]
#[allow(dead_code)]
fn run_integrated_example() {
use scirs2_core::ndarray::IxDyn;
use scirs2_core::random::Normal;
scirs2_core::logging::set_level(LogLevel::Debug);
let logger = Logger::new("integrated_example");
logger.info("Starting integrated example");
Profiler::global().lock().expect("Operation failed").start();
logger.debug("Profiling started");
let mut progress = ProgressTracker::new("Processing Pipeline", 4);
let timer_step1 = Timer::start("step1_generate_data");
logger.debug("Generating random data");
let mut rng = CoreRandom::default();
let normal_distribution = Normal::new(0.0, 1.0).expect("Operation failed");
let mut buffer_pool = BufferPool::<f64>::new();
let mut large_buffer = buffer_pool.acquire_vec(10000);
for elem in large_buffer.iter_mut() {
*elem = rng.sample(normal_distribution);
}
let array_data = normal_distribution.random_array(&mut rng, IxDyn(&[100, 100]));
progress.update(1);
timer_step1.stop();
logger.info("Random data generation completed");
let timer_step2 = Timer::start("step2_process_data");
logger.debug("Processing data");
let array_view = ZeroCopyView::new(&array_data);
let squared_data = Timer::time_function("transform_data", || array_view.transform(|&x| x * x));
progress.update(2);
timer_step2.stop();
logger.info("Data processing completed");
let timer_step3 = Timer::start("step3_convert_types");
logger.debug("Converting data types");
let conversion_result = Timer::time_function("type_conversion", || {
let mut success_count = 0;
let mut clamped_count = 0;
let mut error_count = 0;
for &value in large_buffer.iter().take(100) {
match value.to_numeric::<i16>() {
Ok(_) => success_count += 1,
Err(_) => {
let _ = value.to_numeric_clamped::<i16>();
clamped_count += 1;
error_count += 1;
}
}
}
(success_count, clamped_count, error_count)
});
progress.update(3);
timer_step3.stop();
logger.info(&format!(
"Type conversion completed: {} successful, {} clamped, {} errors",
conversion_result.0, conversion_result.1, conversion_result.2
));
let timer_step4 = Timer::start("step4_cleanup");
logger.debug("Cleaning up resources");
buffer_pool.release_vec(large_buffer);
thread::sleep(Duration::from_millis(200));
progress.update(4);
timer_step4.stop();
logger.info("Resource cleanup completed");
progress.complete();
println!("\n--- Performance Profile ---");
Profiler::global()
.lock()
.expect("Operation failed")
.print_report();
if let Some((calls, total, avg, max)) = Profiler::global()
.lock()
.expect("Operation failed")
.get_timing_stats("transform_data")
{
logger.info(&format!(
"Data transformation details: {} calls, {:.2}ms total, {:.2}ms average, {:.2}ms max",
calls,
total.as_secs_f64() * 1000.0,
avg.as_secs_f64() * 1000.0,
max.as_secs_f64() * 1000.0
));
}
logger.info("Integrated example completed successfully");
Profiler::global().lock().expect("Operation failed").stop();
}