rustorch 0.6.29

Production-ready PyTorch-compatible deep learning library in Rust with special mathematical functions (gamma, Bessel, error functions), statistical distributions, Fourier transforms (FFT/RFFT), matrix decomposition (SVD/QR/LU/eigenvalue), automatic differentiation, neural networks, computer vision transforms, complete GPU acceleration (CUDA/Metal/OpenCL), SIMD optimizations, parallel processing, WebAssembly browser support, comprehensive distributed learning support, and performance validation
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
//! Performance Profiling System
//!
//! Advanced profiling system for performance monitoring, bottleneck detection,
//! and operation timing analysis with statistical reporting.

use std::collections::HashMap;
use std::fmt;
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};

use crate::error::{RusTorchError, RusTorchResult};

// Performance analysis constants
const MS_PER_SECOND: f64 = 1000.0;
const BOTTLENECK_THRESHOLD_MS: f64 = 50.0;

/// Performance profile entry
#[derive(Debug, Clone)]
pub struct ProfileEntry {
    pub operation_name: String,
    pub duration: Duration,
    pub timestamp: Instant,
    pub metadata: HashMap<String, String>,
    pub call_count: usize,
}

impl ProfileEntry {
    /// Create new profile entry
    pub fn new(operation_name: String, duration: Duration) -> Self {
        Self {
            operation_name,
            duration,
            timestamp: Instant::now(),
            metadata: HashMap::new(),
            call_count: 1,
        }
    }

    /// Create with metadata
    pub fn with_metadata(
        operation_name: String,
        duration: Duration,
        metadata: HashMap<String, String>,
    ) -> Self {
        Self {
            operation_name,
            duration,
            timestamp: Instant::now(),
            metadata,
            call_count: 1,
        }
    }

    /// Duration in milliseconds
    pub fn duration_ms(&self) -> f64 {
        self.duration.as_secs_f64() * MS_PER_SECOND
    }

    /// Duration in microseconds
    pub fn duration_us(&self) -> f64 {
        self.duration.as_secs_f64() * 1_000_000.0
    }
}

/// Aggregated performance metrics
#[derive(Debug, Clone)]
pub struct PerformanceMetrics {
    pub total_operations: usize,
    pub average_duration_ms: f64,
    pub median_duration_ms: f64,
    pub min_duration_ms: f64,
    pub max_duration_ms: f64,
    pub std_deviation_ms: f64,
    pub percentile_95_ms: f64,
    pub percentile_99_ms: f64,
    pub slowest_operation: String,
    pub fastest_operation: String,
    pub operations_per_second: f64,
    pub slow_operations_count: usize, // Operations > 100ms
    pub bottlenecks: Vec<String>,
}

impl Default for PerformanceMetrics {
    fn default() -> Self {
        Self {
            total_operations: 0,
            average_duration_ms: 0.0,
            median_duration_ms: 0.0,
            min_duration_ms: 0.0,
            max_duration_ms: 0.0,
            std_deviation_ms: 0.0,
            percentile_95_ms: 0.0,
            percentile_99_ms: 0.0,
            slowest_operation: String::new(),
            fastest_operation: String::new(),
            operations_per_second: 0.0,
            slow_operations_count: 0,
            bottlenecks: Vec::new(),
        }
    }
}

/// Performance profiler for operation timing
pub struct DebugProfiler {
    enabled: bool,
    max_entries: usize,
    entries: Vec<ProfileEntry>,
    operation_stats: HashMap<String, OperationStats>,
    total_operations: usize,
    session_start: Instant,
}

/// Statistics for a specific operation type
#[derive(Debug, Clone)]
pub struct OperationStats {
    pub count: usize,
    pub total_duration: Duration,
    pub min_duration: Duration,
    pub max_duration: Duration,
    pub durations: Vec<Duration>, // For percentile calculations
}

impl OperationStats {
    fn new() -> Self {
        Self {
            count: 0,
            total_duration: Duration::ZERO,
            min_duration: Duration::MAX,
            max_duration: Duration::ZERO,
            durations: Vec::new(),
        }
    }

    fn update(&mut self, duration: Duration) {
        self.count += 1;
        self.total_duration += duration;
        self.min_duration = self.min_duration.min(duration);
        self.max_duration = self.max_duration.max(duration);
        self.durations.push(duration);

        // Keep only recent durations to prevent memory growth
        if self.durations.len() > 1000 {
            self.durations.drain(0..100);
        }
    }

    fn average_duration(&self) -> Duration {
        if self.count > 0 {
            self.total_duration / self.count as u32
        } else {
            Duration::ZERO
        }
    }

    fn median_duration(&self) -> Duration {
        if self.durations.is_empty() {
            return Duration::ZERO;
        }

        let mut sorted = self.durations.clone();
        sorted.sort();

        let mid = sorted.len() / 2;
        if sorted.len() % 2 == 0 {
            (sorted[mid - 1] + sorted[mid]) / 2
        } else {
            sorted[mid]
        }
    }

    fn percentile(&self, p: f64) -> Duration {
        if self.durations.is_empty() {
            return Duration::ZERO;
        }

        let mut sorted = self.durations.clone();
        sorted.sort();

        let index = ((sorted.len() as f64 - 1.0) * p / 100.0).round() as usize;
        sorted[index.min(sorted.len() - 1)]
    }
}

impl fmt::Debug for DebugProfiler {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("DebugProfiler")
            .field("enabled", &self.enabled)
            .field("max_entries", &self.max_entries)
            .field("total_operations", &self.total_operations)
            .field(
                "operation_types",
                &self.operation_stats.keys().collect::<Vec<_>>(),
            )
            .finish()
    }
}

impl DebugProfiler {
    /// Create new profiler
    pub fn new(enabled: bool, max_entries: usize) -> Self {
        Self {
            enabled,
            max_entries,
            entries: Vec::new(),
            operation_stats: HashMap::new(),
            total_operations: 0,
            session_start: Instant::now(),
        }
    }

    /// Record operation timing
    pub fn record_operation(
        &mut self,
        operation_name: &str,
        duration: Duration,
    ) -> RusTorchResult<()> {
        if !self.enabled {
            return Ok(());
        }

        // Create profile entry
        let entry = ProfileEntry::new(operation_name.to_string(), duration);

        // Update operation statistics
        let stats = self
            .operation_stats
            .entry(operation_name.to_string())
            .or_insert_with(OperationStats::new);
        stats.update(duration);

        // Add to entries
        self.entries.push(entry);
        self.total_operations += 1;

        // Maintain max entries limit
        if self.entries.len() > self.max_entries {
            self.entries.drain(0..self.max_entries / 10);
        }

        Ok(())
    }

    /// Record operation with metadata
    pub fn record_operation_with_metadata(
        &mut self,
        operation_name: &str,
        duration: Duration,
        metadata: HashMap<String, String>,
    ) -> RusTorchResult<()> {
        if !self.enabled {
            return Ok(());
        }

        // Create profile entry with metadata
        let entry = ProfileEntry::with_metadata(operation_name.to_string(), duration, metadata);

        // Update operation statistics
        let stats = self
            .operation_stats
            .entry(operation_name.to_string())
            .or_insert_with(OperationStats::new);
        stats.update(duration);

        // Add to entries
        self.entries.push(entry);
        self.total_operations += 1;

        // Maintain max entries limit
        if self.entries.len() > self.max_entries {
            self.entries.drain(0..self.max_entries / 10);
        }

        Ok(())
    }

    /// Get comprehensive performance metrics
    pub fn get_performance_metrics(&self) -> PerformanceMetrics {
        if self.entries.is_empty() {
            return PerformanceMetrics::default();
        }

        // Collect all durations
        let durations: Vec<Duration> = self.entries.iter().map(|e| e.duration).collect();

        let mut sorted_durations = durations.clone();
        sorted_durations.sort();

        // Calculate basic statistics
        let total_duration: Duration = durations.iter().sum();
        let average_duration = total_duration / durations.len() as u32;

        let min_duration = sorted_durations.first().cloned().unwrap_or(Duration::ZERO);
        let max_duration = sorted_durations.last().cloned().unwrap_or(Duration::ZERO);

        // Calculate median
        let median_duration = if sorted_durations.len() % 2 == 0 {
            let mid = sorted_durations.len() / 2;
            (sorted_durations[mid - 1] + sorted_durations[mid]) / 2
        } else {
            sorted_durations[sorted_durations.len() / 2]
        };

        // Calculate standard deviation
        let mean_ms = average_duration.as_secs_f64() * MS_PER_SECOND;
        let variance = durations
            .iter()
            .map(|d| {
                let diff = (d.as_secs_f64() * MS_PER_SECOND) - mean_ms;
                diff * diff
            })
            .sum::<f64>()
            / durations.len() as f64;
        let std_deviation_ms = variance.sqrt();

        // Calculate percentiles
        let p95_index = ((sorted_durations.len() as f64 - 1.0) * 0.95).round() as usize;
        let p99_index = ((sorted_durations.len() as f64 - 1.0) * 0.99).round() as usize;

        let percentile_95_ms = sorted_durations[p95_index.min(sorted_durations.len() - 1)]
            .as_secs_f64()
            * MS_PER_SECOND;
        let percentile_99_ms = sorted_durations[p99_index.min(sorted_durations.len() - 1)]
            .as_secs_f64()
            * MS_PER_SECOND;

        // Find slowest and fastest operations
        let slowest_entry = self
            .entries
            .iter()
            .max_by_key(|e| e.duration)
            .map(|e| e.operation_name.clone())
            .unwrap_or_default();

        let fastest_entry = self
            .entries
            .iter()
            .min_by_key(|e| e.duration)
            .map(|e| e.operation_name.clone())
            .unwrap_or_default();

        // Calculate operations per second
        let session_duration = self.session_start.elapsed().as_secs_f64();
        let operations_per_second = if session_duration > 0.0 {
            self.total_operations as f64 / session_duration
        } else {
            0.0
        };

        // Count slow operations (> 100ms)
        let slow_operations_count = durations.iter().filter(|d| d.as_millis() > 100).count();

        // Identify bottlenecks (operations with high average duration)
        let mut bottlenecks: Vec<(String, f64)> = self
            .operation_stats
            .iter()
            .map(|(name, stats)| {
                (
                    name.clone(),
                    stats.average_duration().as_secs_f64() * 1000.0,
                )
            })
            .filter(|(_, avg_ms)| *avg_ms > BOTTLENECK_THRESHOLD_MS)
            .collect();

        bottlenecks.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap_or(std::cmp::Ordering::Equal));
        let bottlenecks: Vec<String> = bottlenecks
            .into_iter()
            .take(5) // Top 5 bottlenecks
            .map(|(name, _)| name)
            .collect();

        PerformanceMetrics {
            total_operations: self.total_operations,
            average_duration_ms: mean_ms,
            median_duration_ms: median_duration.as_secs_f64() * 1000.0,
            min_duration_ms: min_duration.as_secs_f64() * 1000.0,
            max_duration_ms: max_duration.as_secs_f64() * 1000.0,
            std_deviation_ms,
            percentile_95_ms,
            percentile_99_ms,
            slowest_operation: slowest_entry,
            fastest_operation: fastest_entry,
            operations_per_second,
            slow_operations_count,
            bottlenecks,
        }
    }

    /// Get statistics for specific operation
    pub fn get_operation_stats(&self, operation_name: &str) -> Option<OperationStats> {
        self.operation_stats.get(operation_name).cloned()
    }

    /// Get all operation names
    pub fn get_operation_names(&self) -> Vec<String> {
        self.operation_stats.keys().cloned().collect()
    }

    /// Get recent entries (last n entries)
    pub fn get_recent_entries(&self, n: usize) -> &[ProfileEntry] {
        let start = if self.entries.len() > n {
            self.entries.len() - n
        } else {
            0
        };

        &self.entries[start..]
    }

    /// Get entries for specific operation
    pub fn get_entries_for_operation(&self, operation_name: &str) -> Vec<&ProfileEntry> {
        self.entries
            .iter()
            .filter(|e| e.operation_name == operation_name)
            .collect()
    }

    /// Get total number of recorded operations
    pub fn get_total_profiles(&self) -> usize {
        self.total_operations
    }

    /// Clear all recorded data
    pub fn clear(&mut self) {
        self.entries.clear();
        self.operation_stats.clear();
        self.total_operations = 0;
        self.session_start = Instant::now();
    }

    /// Enable/disable profiling
    pub fn set_enabled(&mut self, enabled: bool) {
        self.enabled = enabled;
    }

    /// Check if profiling is enabled
    pub fn is_enabled(&self) -> bool {
        self.enabled
    }

    /// Generate performance report
    pub fn generate_performance_report(&self) -> String {
        let metrics = self.get_performance_metrics();

        let mut report = String::new();
        report.push_str("🚀 Performance Profile Report\n");
        report.push_str("==============================\n\n");

        report.push_str(&format!("📊 Overall Statistics:\n"));
        report.push_str(&format!(
            "  Total Operations: {}\n",
            metrics.total_operations
        ));
        report.push_str(&format!(
            "  Operations/Second: {:.2}\n",
            metrics.operations_per_second
        ));
        report.push_str(&format!(
            "  Average Duration: {:.3}ms\n",
            metrics.average_duration_ms
        ));
        report.push_str(&format!(
            "  Median Duration: {:.3}ms\n",
            metrics.median_duration_ms
        ));
        report.push_str(&format!(
            "  Std Deviation: {:.3}ms\n\n",
            metrics.std_deviation_ms
        ));

        report.push_str(&format!("⚡ Performance Range:\n"));
        report.push_str(&format!(
            "  Fastest: {:.3}ms ({})\n",
            metrics.min_duration_ms, metrics.fastest_operation
        ));
        report.push_str(&format!(
            "  Slowest: {:.3}ms ({})\n",
            metrics.max_duration_ms, metrics.slowest_operation
        ));
        report.push_str(&format!(
            "  95th Percentile: {:.3}ms\n",
            metrics.percentile_95_ms
        ));
        report.push_str(&format!(
            "  99th Percentile: {:.3}ms\n\n",
            metrics.percentile_99_ms
        ));

        report.push_str(&format!("🐌 Performance Issues:\n"));
        report.push_str(&format!(
            "  Slow Operations (>100ms): {}\n",
            metrics.slow_operations_count
        ));

        if !metrics.bottlenecks.is_empty() {
            report.push_str("  Top Bottlenecks:\n");
            for (i, bottleneck) in metrics.bottlenecks.iter().enumerate() {
                report.push_str(&format!("    {}. {}\n", i + 1, bottleneck));
            }
        } else {
            report.push_str("  No significant bottlenecks detected\n");
        }

        report
    }
}

/// RAII profiling scope guard
pub struct ProfileScope {
    operation_name: String,
    start_time: Instant,
    profiler: Arc<Mutex<DebugProfiler>>,
    metadata: HashMap<String, String>,
}

impl ProfileScope {
    /// Create new profiling scope
    pub fn new(operation_name: String, profiler: Arc<Mutex<DebugProfiler>>) -> Self {
        Self {
            operation_name,
            start_time: Instant::now(),
            profiler,
            metadata: HashMap::new(),
        }
    }

    /// Create profiling scope with metadata
    pub fn with_metadata(
        operation_name: String,
        profiler: Arc<Mutex<DebugProfiler>>,
        metadata: HashMap<String, String>,
    ) -> Self {
        Self {
            operation_name,
            start_time: Instant::now(),
            profiler,
            metadata,
        }
    }

    /// Add metadata to the profile
    pub fn add_metadata(&mut self, key: &str, value: &str) {
        self.metadata.insert(key.to_string(), value.to_string());
    }
}

impl Drop for ProfileScope {
    fn drop(&mut self) {
        let duration = self.start_time.elapsed();

        if let Ok(mut profiler) = self.profiler.lock() {
            if self.metadata.is_empty() {
                let _ = profiler.record_operation(&self.operation_name, duration);
            } else {
                let _ = profiler.record_operation_with_metadata(
                    &self.operation_name,
                    duration,
                    self.metadata.clone(),
                );
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::thread;
    use std::time::Duration as StdDuration;

    #[test]
    fn test_profile_entry_creation() {
        let duration = Duration::from_millis(100);
        let entry = ProfileEntry::new("test_operation".to_string(), duration);

        assert_eq!(entry.operation_name, "test_operation");
        assert_eq!(entry.duration, duration);
        assert_eq!(entry.duration_ms(), 100.0);
        assert_eq!(entry.call_count, 1);
    }

    #[test]
    fn test_profiler_creation() {
        let profiler = DebugProfiler::new(true, 1000);
        assert!(profiler.is_enabled());
        assert_eq!(profiler.get_total_profiles(), 0);
    }

    #[test]
    fn test_profiler_recording() {
        let mut profiler = DebugProfiler::new(true, 1000);

        let duration = Duration::from_millis(50);
        assert!(profiler.record_operation("test_op", duration).is_ok());

        assert_eq!(profiler.get_total_profiles(), 1);

        let metrics = profiler.get_performance_metrics();
        assert_eq!(metrics.total_operations, 1);
        assert_eq!(metrics.average_duration_ms, 50.0);
    }

    #[test]
    fn test_profiler_disabled() {
        let mut profiler = DebugProfiler::new(false, 1000);

        let duration = Duration::from_millis(50);
        assert!(profiler.record_operation("test_op", duration).is_ok());

        assert_eq!(profiler.get_total_profiles(), 0);
    }

    #[test]
    fn test_performance_metrics_calculation() {
        let mut profiler = DebugProfiler::new(true, 1000);

        // Record various operations
        let durations = [10, 20, 30, 40, 50, 100, 200, 500];
        for (i, &duration_ms) in durations.iter().enumerate() {
            let duration = Duration::from_millis(duration_ms);
            profiler
                .record_operation(&format!("op_{}", i), duration)
                .unwrap();
        }

        let metrics = profiler.get_performance_metrics();

        assert_eq!(metrics.total_operations, 8);
        assert!(metrics.average_duration_ms > 0.0);
        assert!(metrics.median_duration_ms > 0.0);
        assert_eq!(metrics.min_duration_ms, 10.0);
        assert_eq!(metrics.max_duration_ms, 500.0);
        assert!(metrics.std_deviation_ms > 0.0);
        assert!(metrics.slow_operations_count > 0); // 200ms and 500ms are > 100ms
    }

    #[test]
    fn test_operation_stats() {
        let mut profiler = DebugProfiler::new(true, 1000);

        // Record multiple instances of same operation
        for i in 0..5 {
            let duration = Duration::from_millis(100 + i * 10);
            profiler.record_operation("repeated_op", duration).unwrap();
        }

        let stats = profiler.get_operation_stats("repeated_op").unwrap();
        assert_eq!(stats.count, 5);
        assert_eq!(stats.min_duration, Duration::from_millis(100));
        assert_eq!(stats.max_duration, Duration::from_millis(140));
    }

    #[test]
    fn test_profile_scope() {
        let profiler = Arc::new(Mutex::new(DebugProfiler::new(true, 1000)));

        {
            let _scope = ProfileScope::new("scoped_operation".to_string(), Arc::clone(&profiler));
            thread::sleep(StdDuration::from_millis(10));
        } // Scope drops here, recording the profile

        let prof = profiler.lock().unwrap();
        assert_eq!(prof.get_total_profiles(), 1);

        let entries = prof.get_entries_for_operation("scoped_operation");
        assert_eq!(entries.len(), 1);
        assert!(entries[0].duration_ms() >= 10.0);
    }

    #[test]
    fn test_profile_scope_with_metadata() {
        let profiler = Arc::new(Mutex::new(DebugProfiler::new(true, 1000)));

        {
            let mut metadata = HashMap::new();
            metadata.insert("component".to_string(), "tensor".to_string());
            metadata.insert("operation_type".to_string(), "multiplication".to_string());

            let _scope = ProfileScope::with_metadata(
                "tensor_multiply".to_string(),
                Arc::clone(&profiler),
                metadata,
            );
            thread::sleep(StdDuration::from_millis(5));
        }

        let prof = profiler.lock().unwrap();
        let entries = prof.get_entries_for_operation("tensor_multiply");
        assert_eq!(entries.len(), 1);
        assert_eq!(
            entries[0].metadata.get("component"),
            Some(&"tensor".to_string())
        );
        assert_eq!(
            entries[0].metadata.get("operation_type"),
            Some(&"multiplication".to_string())
        );
    }

    #[test]
    fn test_bottleneck_detection() {
        let mut profiler = DebugProfiler::new(true, 1000);

        // Create a clear bottleneck
        for _ in 0..10 {
            profiler
                .record_operation("fast_op", Duration::from_millis(5))
                .unwrap();
            profiler
                .record_operation("slow_op", Duration::from_millis(200))
                .unwrap();
        }

        let metrics = profiler.get_performance_metrics();
        assert!(!metrics.bottlenecks.is_empty());
        assert!(metrics.bottlenecks.contains(&"slow_op".to_string()));
    }

    #[test]
    fn test_profiler_clear() {
        let mut profiler = DebugProfiler::new(true, 1000);

        profiler
            .record_operation("test_op", Duration::from_millis(50))
            .unwrap();
        assert_eq!(profiler.get_total_profiles(), 1);

        profiler.clear();
        assert_eq!(profiler.get_total_profiles(), 0);

        let metrics = profiler.get_performance_metrics();
        assert_eq!(metrics.total_operations, 0);
    }
}