#!/usr/bin/env herodo
// Profiling Module - Stress Testing
// Run with: herodo rhaiexamples/profiling/03_stress_test.rhai

print("=== System Stress Testing ===");
print("");

// Get system info
let profile = SystemProfile();
print("Testing system: " + profile.hostname);
print("CPU cores: " + profile.cpu_cores);
print("Memory: " + (profile.memory_total_bytes / 1024 / 1024) + " MB");
print("");

// ========================
// CPU Stress Test
// ========================
print("--- CPU Stress Test ---");

print("Running CPU stress test for 10 seconds...");
let cpu_stress = run_cpu_benchmark(10); // 10 seconds
print("CPU stress test completed:");
print("  Operations completed: " + cpu_stress.total_operations);
print("  Average score: " + to_string(cpu_stress.average_score));
print("");

// ========================
// Memory Stress Test  
// ========================
print("--- Memory Stress Test ---");

// Use 25% of available memory for stress testing
let stress_memory = profile.memory_total_bytes / 4;
let stress_mb = stress_memory / 1024 / 1024;
print("Running memory stress test with " + to_string(stress_mb) + " MB...");

let mem_stress = run_memory_stress_test(10, stress_memory); // 10 seconds
print("Memory stress test completed:");
print("  Peak read bandwidth: " + to_string(mem_stress.peak_read_bandwidth_gbps) + " GB/s");
print("  Peak write bandwidth: " + to_string(mem_stress.peak_write_bandwidth_gbps) + " GB/s");
print("  Errors detected: " + mem_stress.error_count);
print("");

// ========================
// Disk Stress Test
// ========================
print("--- Disk Stress Test ---");

print("Running disk stress test...");
let disk_stress = run_disk_benchmark("/tmp", 10); // 10 seconds
print("Disk stress test completed:");
if disk_stress.sequential_read != null {
    print("  Sequential read: " + to_string(disk_stress.sequential_read.throughput_mbps) + " MB/s");
}
if disk_stress.random_write != null {
    print("  Random write: " + to_string(disk_stress.random_write.throughput_mbps) + " MB/s");
}
if disk_stress.iops != null {
    print("  Read IOPS: " + disk_stress.iops.read_iops);
    print("  Write IOPS: " + disk_stress.iops.write_iops);
}
print("");

// ========================
// Thermal Check
// ========================
print("--- Thermal Check ---");

// Check if system is thermal throttling
let throttling = detect_thermal_throttling();
if throttling {
    print("⚠️  WARNING: System is thermal throttling!");
    print("Performance may be limited by temperature.");
} else {
    print("✅ No thermal throttling detected.");
}
print("");

// ========================
// Resource Usage Summary
// ========================
print("--- Resource Usage Summary ---");

let cpu_usage = sample_cpu_utilization();
let mem_usage = sample_memory_usage();
let io_stats = sample_io_statistics();

print("Current system status:");
print("  CPU usage: " + to_string(cpu_usage * 100) + "%");
print("  Memory usage: " + to_string(mem_usage.usage_percent * 100) + "%");
print("  Available memory: " + to_string(mem_usage.available_bytes / 1024 / 1024) + " MB");
print("  Disk read IOPS: " + to_string(io_stats.read_iops));
print("  Disk write IOPS: " + to_string(io_stats.write_iops));

print("");
print("Stress testing completed! 🚀");
print("Review the results above for any performance issues.");
