revue 2.71.1

A Vue-style TUI framework for Rust with CSS styling
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
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
//! Performance profiling utilities
//!
//! Provides tools for measuring and optimizing TUI application performance.
//!
//! # Example
//!
//! ```rust,ignore
//! use revue::app::profiler::{Profiler, profiler};
//!
//! let mut profiler = Profiler::new();
//!
//! // Time a render operation
//! profiler.start("render");
//! // ... render widgets ...
//! profiler.end("render");
//!
//! // Get statistics
//! if let Some(stats) = profiler.stats("render") {
//!     println!("Avg render: {:?}", stats.avg);
//!     println!("Max render: {:?}", stats.max);
//! }
//!
//! // Print report
//! println!("{}", profiler.report());
//! ```

use std::collections::HashMap;
use std::time::{Duration, Instant};

/// Performance measurement sample
#[derive(Clone, Debug)]
pub struct Sample {
    /// Duration of the sample
    pub duration: Duration,
    /// Timestamp when sample was taken
    pub timestamp: Instant,
}

impl Sample {
    /// Create new sample
    pub fn new(duration: Duration) -> Self {
        Self {
            duration,
            timestamp: Instant::now(),
        }
    }
}

/// Statistics for a metric
#[derive(Clone, Debug)]
pub struct Stats {
    /// Number of samples
    pub count: usize,
    /// Total duration
    pub total: Duration,
    /// Minimum duration
    pub min: Duration,
    /// Maximum duration
    pub max: Duration,
    /// Average duration
    pub avg: Duration,
    /// Standard deviation (approximate)
    pub std_dev: Duration,
    /// Last sample
    pub last: Duration,
    /// Samples per second (throughput)
    pub throughput: f64,
}

impl Stats {
    /// Create stats from samples
    pub fn from_samples(samples: &[Sample]) -> Option<Self> {
        if samples.is_empty() {
            return None;
        }

        let count = samples.len();
        let total: Duration = samples.iter().map(|s| s.duration).sum();
        // samples is guaranteed non-empty by early return above
        let min = samples.iter().map(|s| s.duration).min().unwrap_or_default();
        let max = samples.iter().map(|s| s.duration).max().unwrap_or_default();
        let avg = total / count as u32;
        let last = samples.last().map(|s| s.duration).unwrap_or_default();

        // Calculate throughput based on time window
        let throughput = if let (Some(first), Some(last_sample)) = (samples.first(), samples.last())
        {
            let window = last_sample.timestamp.duration_since(first.timestamp);
            if window.as_secs_f64() > 0.0 {
                count as f64 / window.as_secs_f64()
            } else {
                0.0
            }
        } else {
            0.0
        };

        // Calculate standard deviation
        let avg_nanos = avg.as_nanos() as f64;
        let variance: f64 = samples
            .iter()
            .map(|s| {
                let diff = s.duration.as_nanos() as f64 - avg_nanos;
                diff * diff
            })
            .sum::<f64>()
            / count as f64;
        let std_dev = Duration::from_nanos(variance.sqrt() as u64);

        Some(Self {
            count,
            total,
            min,
            max,
            avg,
            std_dev,
            last,
            throughput,
        })
    }

    /// Format duration for display
    fn format_duration(d: Duration) -> String {
        let micros = d.as_micros();
        if micros < 1000 {
            format!("{}μs", micros)
        } else if micros < 1_000_000 {
            format!("{:.2}ms", micros as f64 / 1000.0)
        } else {
            format!("{:.2}s", d.as_secs_f64())
        }
    }
}

/// Metric type for categorization
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum MetricType {
    /// Render timing
    Render,
    /// Event handling timing
    Event,
    /// Layout calculation timing
    Layout,
    /// Custom timing
    Custom,
}

/// Metric entry with samples
#[derive(Clone, Debug)]
pub struct Metric {
    /// Metric name
    pub name: String,
    /// Metric type
    pub metric_type: MetricType,
    /// Samples
    samples: Vec<Sample>,
    /// Maximum samples to keep
    max_samples: usize,
    /// Active start time
    start_time: Option<Instant>,
}

impl Metric {
    /// Create new metric
    pub fn new(name: impl Into<String>, metric_type: MetricType) -> Self {
        Self {
            name: name.into(),
            metric_type,
            samples: Vec::new(),
            max_samples: 1000,
            start_time: None,
        }
    }

    /// Set max samples to keep
    pub fn max_samples(mut self, max: usize) -> Self {
        self.max_samples = max;
        self
    }

    /// Start timing
    pub fn start(&mut self) {
        self.start_time = Some(Instant::now());
    }

    /// End timing and record sample
    pub fn end(&mut self) -> Option<Duration> {
        if let Some(start) = self.start_time.take() {
            let duration = start.elapsed();
            self.add_sample(duration);
            Some(duration)
        } else {
            None
        }
    }

    /// Add a sample directly
    pub fn add_sample(&mut self, duration: Duration) {
        self.samples.push(Sample::new(duration));
        if self.samples.len() > self.max_samples {
            self.samples.remove(0);
        }
    }

    /// Get statistics
    pub fn stats(&self) -> Option<Stats> {
        Stats::from_samples(&self.samples)
    }

    /// Clear all samples
    pub fn clear(&mut self) {
        self.samples.clear();
        self.start_time = None;
    }

    /// Get sample count
    pub fn sample_count(&self) -> usize {
        self.samples.len()
    }

    /// Get samples
    pub fn samples(&self) -> &[Sample] {
        &self.samples
    }
}

/// Frame rate counter
#[derive(Clone, Debug)]
pub struct FpsCounter {
    /// Frame timestamps
    frames: Vec<Instant>,
    /// Window size for FPS calculation
    window: Duration,
}

impl FpsCounter {
    /// Create new FPS counter
    pub fn new() -> Self {
        Self {
            frames: Vec::new(),
            window: Duration::from_secs(1),
        }
    }

    /// Set window size
    pub fn window(mut self, duration: Duration) -> Self {
        self.window = duration;
        self
    }

    /// Record a frame
    pub fn frame(&mut self) {
        let now = Instant::now();
        self.frames.push(now);

        // Remove old frames outside window
        let cutoff = now - self.window;
        self.frames.retain(|&t| t >= cutoff);
    }

    /// Get current FPS
    pub fn fps(&self) -> f64 {
        if self.frames.len() < 2 {
            return 0.0;
        }

        let count = self.frames.len();
        let window_secs = self.window.as_secs_f64();
        count as f64 / window_secs
    }

    /// Get frame time (average)
    pub fn frame_time(&self) -> Option<Duration> {
        let fps = self.fps();
        if fps > 0.0 {
            Some(Duration::from_secs_f64(1.0 / fps))
        } else {
            None
        }
    }

    /// Reset counter
    pub fn reset(&mut self) {
        self.frames.clear();
    }
}

impl Default for FpsCounter {
    fn default() -> Self {
        Self::new()
    }
}

/// Performance profiler
pub struct Profiler {
    /// Named metrics
    metrics: HashMap<String, Metric>,
    /// FPS counter
    fps: FpsCounter,
    /// Profiling enabled
    enabled: bool,
    /// Start time
    start_time: Instant,
}

impl Profiler {
    /// Create new profiler
    pub fn new() -> Self {
        Self {
            metrics: HashMap::new(),
            fps: FpsCounter::new(),
            enabled: true,
            start_time: Instant::now(),
        }
    }

    /// Enable or 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
    }

    /// Register a metric
    pub fn register(&mut self, name: impl Into<String>, metric_type: MetricType) {
        let name = name.into();
        self.metrics
            .insert(name.clone(), Metric::new(name, metric_type));
    }

    /// Start timing a metric
    pub fn start(&mut self, name: &str) {
        if !self.enabled {
            return;
        }

        if !self.metrics.contains_key(name) {
            self.metrics
                .insert(name.to_string(), Metric::new(name, MetricType::Custom));
        }

        if let Some(metric) = self.metrics.get_mut(name) {
            metric.start();
        }
    }

    /// End timing a metric
    pub fn end(&mut self, name: &str) -> Option<Duration> {
        if !self.enabled {
            return None;
        }

        self.metrics.get_mut(name).and_then(|m| m.end())
    }

    /// Record a sample directly
    pub fn record(&mut self, name: &str, duration: Duration) {
        if !self.enabled {
            return;
        }

        if !self.metrics.contains_key(name) {
            self.metrics
                .insert(name.to_string(), Metric::new(name, MetricType::Custom));
        }

        if let Some(metric) = self.metrics.get_mut(name) {
            metric.add_sample(duration);
        }
    }

    /// Time a closure
    pub fn time<F, T>(&mut self, name: &str, f: F) -> T
    where
        F: FnOnce() -> T,
    {
        if !self.enabled {
            return f();
        }

        self.start(name);
        let result = f();
        self.end(name);
        result
    }

    /// Record a frame
    pub fn frame(&mut self) {
        if self.enabled {
            self.fps.frame();
        }
    }

    /// Get current FPS
    pub fn fps(&self) -> f64 {
        self.fps.fps()
    }

    /// Get statistics for a metric
    pub fn stats(&self, name: &str) -> Option<Stats> {
        self.metrics.get(name).and_then(|m| m.stats())
    }

    /// Get all metrics
    pub fn metrics(&self) -> impl Iterator<Item = (&str, &Metric)> {
        self.metrics.iter().map(|(k, v)| (k.as_str(), v))
    }

    /// Clear all metrics
    pub fn clear(&mut self) {
        for metric in self.metrics.values_mut() {
            metric.clear();
        }
        self.fps.reset();
    }

    /// Get total runtime
    pub fn runtime(&self) -> Duration {
        self.start_time.elapsed()
    }

    /// Generate a text report
    pub fn report(&self) -> String {
        let mut lines = Vec::new();
        lines.push("=== Performance Report ===".to_string());
        lines.push(format!("Runtime: {:.2}s", self.runtime().as_secs_f64()));
        lines.push(format!("FPS: {:.1}", self.fps()));
        lines.push(String::new());

        // Sort metrics by name
        let mut metric_names: Vec<_> = self.metrics.keys().collect();
        metric_names.sort();

        for name in metric_names {
            if let Some(stats) = self.stats(name) {
                lines.push(format!("[{}]", name));
                lines.push(format!("  Samples: {}", stats.count));
                lines.push(format!("  Avg: {}", Stats::format_duration(stats.avg)));
                lines.push(format!("  Min: {}", Stats::format_duration(stats.min)));
                lines.push(format!("  Max: {}", Stats::format_duration(stats.max)));
                lines.push(format!(
                    "  Std Dev: {}",
                    Stats::format_duration(stats.std_dev)
                ));
                lines.push(format!("  Throughput: {:.1}/s", stats.throughput));
                lines.push(String::new());
            }
        }

        lines.join(
            "
",
        )
    }

    /// Generate a summary (shorter report)
    pub fn summary(&self) -> String {
        let mut parts = Vec::new();
        parts.push(format!("FPS: {:.1}", self.fps()));

        for (name, metric) in &self.metrics {
            if let Some(stats) = metric.stats() {
                parts.push(format!("{}: {}", name, Stats::format_duration(stats.avg)));
            }
        }

        parts.join(" | ")
    }
}

impl Default for Profiler {
    fn default() -> Self {
        Self::new()
    }
}

/// Scoped timing guard
pub struct TimingGuard<'a> {
    profiler: &'a mut Profiler,
    name: String,
}

impl<'a> TimingGuard<'a> {
    /// Create new timing guard
    pub fn new(profiler: &'a mut Profiler, name: impl Into<String>) -> Self {
        let name = name.into();
        profiler.start(&name);
        Self { profiler, name }
    }
}

impl Drop for TimingGuard<'_> {
    fn drop(&mut self) {
        self.profiler.end(&self.name);
    }
}

/// Create a new profiler
pub fn profiler() -> Profiler {
    Profiler::new()
}

/// Create a new FPS counter
pub fn fps_counter() -> FpsCounter {
    FpsCounter::new()
}

/// Performance snapshot for comparison
#[derive(Clone, Debug)]
pub struct Snapshot {
    /// Metric stats
    pub stats: HashMap<String, Stats>,
    /// FPS at snapshot time
    pub fps: f64,
    /// Runtime at snapshot
    pub runtime: Duration,
    /// Timestamp
    pub timestamp: Instant,
}

impl Snapshot {
    /// Take a snapshot from profiler
    pub fn from_profiler(profiler: &Profiler) -> Self {
        let stats = profiler
            .metrics
            .iter()
            .filter_map(|(name, metric)| metric.stats().map(|s| (name.clone(), s)))
            .collect();

        Self {
            stats,
            fps: profiler.fps(),
            runtime: profiler.runtime(),
            timestamp: Instant::now(),
        }
    }

    /// Compare with another snapshot
    pub fn compare(&self, other: &Snapshot) -> SnapshotDiff {
        let mut diffs = HashMap::new();

        for (name, stats) in &self.stats {
            if let Some(other_stats) = other.stats.get(name) {
                let avg_diff = stats.avg.as_nanos() as i128 - other_stats.avg.as_nanos() as i128;
                diffs.insert(
                    name.clone(),
                    MetricDiff {
                        avg_change_nanos: avg_diff,
                        count_change: stats.count as i64 - other_stats.count as i64,
                    },
                );
            }
        }

        SnapshotDiff {
            fps_change: self.fps - other.fps,
            metric_diffs: diffs,
        }
    }
}

/// Difference between snapshots
#[derive(Clone, Debug)]
pub struct SnapshotDiff {
    /// FPS change
    pub fps_change: f64,
    /// Metric differences
    pub metric_diffs: HashMap<String, MetricDiff>,
}

/// Metric difference
#[derive(Clone, Debug)]
pub struct MetricDiff {
    /// Average change in nanoseconds
    pub avg_change_nanos: i128,
    /// Sample count change
    pub count_change: i64,
}
// KEEP HERE - Private implementation tests (accesses private fields)

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

    #[test]
    fn test_profiler_basic() {
        let mut profiler = Profiler::new();

        profiler.start("test");
        thread::sleep(Duration::from_millis(10));
        let duration = profiler.end("test");

        assert!(duration.is_some());
        assert!(duration.unwrap() >= Duration::from_millis(10));
    }

    #[test]
    fn test_profiler_stats() {
        let mut profiler = Profiler::new();

        for _ in 0..10 {
            profiler.record("test", Duration::from_millis(10));
        }

        let stats = profiler.stats("test").unwrap();
        assert_eq!(stats.count, 10);
        assert_eq!(stats.avg, Duration::from_millis(10));
    }

    #[test]
    fn test_profiler_time() {
        let mut profiler = Profiler::new();

        let result = profiler.time("compute", || {
            thread::sleep(Duration::from_millis(5));
            42
        });

        assert_eq!(result, 42);
        assert!(profiler.stats("compute").is_some());
    }

    #[test]
    fn test_profiler_disabled() {
        let mut profiler = Profiler::new();
        profiler.set_enabled(false);

        profiler.start("test");
        thread::sleep(Duration::from_millis(5));
        let duration = profiler.end("test");

        assert!(duration.is_none());
    }

    #[test]
    fn test_fps_counter() {
        let mut fps = FpsCounter::new().window(Duration::from_millis(500));

        // Record frames without sleep - just testing frame counting logic
        for _ in 0..10 {
            fps.frame();
        }

        // FPS should be calculated (may be very high without sleep)
        let measured = fps.fps();
        // Just verify it returns a positive number (logic works)
        assert!(measured >= 0.0);
    }

    #[test]
    fn test_profiler_report() {
        let mut profiler = Profiler::new();

        profiler.record("render", Duration::from_millis(16));
        profiler.record("render", Duration::from_millis(17));
        profiler.record("event", Duration::from_micros(100));

        let report = profiler.report();
        assert!(report.contains("render"));
        assert!(report.contains("event"));
    }

    #[test]
    fn test_stats_format_duration() {
        assert_eq!(Stats::format_duration(Duration::from_micros(500)), "500μs");
        assert_eq!(
            Stats::format_duration(Duration::from_micros(1500)),
            "1.50ms"
        );
        assert_eq!(Stats::format_duration(Duration::from_secs(2)), "2.00s");
    }

    #[test]
    fn test_metric() {
        let mut metric = Metric::new("test", MetricType::Render);

        metric.start();
        thread::sleep(Duration::from_millis(5));
        metric.end();

        assert_eq!(metric.sample_count(), 1);
    }

    #[test]
    fn test_metric_max_samples() {
        let mut metric = Metric::new("test", MetricType::Custom).max_samples(5);

        for i in 0..10 {
            metric.add_sample(Duration::from_millis(i));
        }

        assert_eq!(metric.sample_count(), 5);
    }

    #[test]
    fn test_snapshot() {
        let mut profiler = Profiler::new();
        profiler.record("test", Duration::from_millis(10));

        let snapshot = Snapshot::from_profiler(&profiler);
        assert!(snapshot.stats.contains_key("test"));
    }

    #[test]
    fn test_snapshot_compare() {
        let mut profiler = Profiler::new();
        profiler.record("test", Duration::from_millis(10));
        let snap1 = Snapshot::from_profiler(&profiler);

        profiler.record("test", Duration::from_millis(20));
        let snap2 = Snapshot::from_profiler(&profiler);

        let diff = snap2.compare(&snap1);
        assert!(diff.metric_diffs.contains_key("test"));
    }

    #[test]
    fn test_profiler_helper() {
        let p = profiler();
        assert!(p.is_enabled());
    }

    #[test]
    fn test_fps_counter_helper() {
        let fps = fps_counter();
        assert_eq!(fps.fps(), 0.0);
    }

    #[test]
    fn test_profiler_summary() {
        let mut profiler = Profiler::new();
        profiler.record("render", Duration::from_millis(16));

        let summary = profiler.summary();
        assert!(summary.contains("FPS"));
        assert!(summary.contains("render"));
    }

    // =========================================================================
    // Additional profiler tests
    // =========================================================================

    #[test]
    fn test_profiler_default() {
        let profiler = Profiler::default();
        assert!(profiler.is_enabled());
    }

    #[test]
    fn test_profiler_register() {
        let mut profiler = Profiler::new();
        profiler.register("custom_metric", MetricType::Custom);
        assert!(profiler.stats("custom_metric").is_none()); // No samples yet
    }

    #[test]
    fn test_profiler_start_without_register() {
        let mut profiler = Profiler::new();
        profiler.start("auto_register"); // Should auto-register
        let _ = profiler.end("auto_register");
        assert!(profiler.stats("auto_register").is_some());
    }

    #[test]
    fn test_profiler_record_without_register() {
        let mut profiler = Profiler::new();
        profiler.record("auto_record", Duration::from_millis(10));
        assert!(profiler.stats("auto_record").is_some());
    }

    #[test]
    fn test_profiler_metrics_iteration() {
        let mut profiler = Profiler::new();
        profiler.record("metric1", Duration::from_millis(10));
        profiler.record("metric2", Duration::from_millis(20));

        let mut count = 0;
        for (name, _metric) in profiler.metrics() {
            assert!(name == "metric1" || name == "metric2");
            count += 1;
        }
        assert_eq!(count, 2);
    }

    #[test]
    fn test_profiler_clear() {
        let mut profiler = Profiler::new();
        profiler.record("test", Duration::from_millis(10));
        profiler.clear();
        assert!(profiler.stats("test").is_none());
        assert_eq!(profiler.fps(), 0.0);
    }

    #[test]
    fn test_profiler_runtime() {
        let profiler = Profiler::new();
        thread::sleep(Duration::from_millis(10));
        let runtime = profiler.runtime();
        assert!(runtime >= Duration::from_millis(10));
    }

    #[test]
    fn test_profiler_time_returns_result() {
        let mut profiler = Profiler::new();
        let result = profiler.time("calc", || 2 + 2);
        assert_eq!(result, 4);
    }

    #[test]
    fn test_profiler_frame() {
        let mut profiler = Profiler::new();
        profiler.frame();
        profiler.frame();
        profiler.frame();
        // FPS should be calculated
        let fps = profiler.fps();
        // May be very high since we didn't sleep
        assert!(fps >= 0.0);
    }

    #[test]
    fn test_profiler_set_enabled() {
        let mut profiler = Profiler::new();
        assert!(profiler.is_enabled());
        profiler.set_enabled(false);
        assert!(!profiler.is_enabled());
        profiler.set_enabled(true);
        assert!(profiler.is_enabled());
    }

    #[test]
    fn test_metric_new_with_string() {
        let name = String::from("string_metric");
        let metric = Metric::new(name.clone(), MetricType::Event);
        assert_eq!(metric.name, name);
    }

    #[test]
    fn test_metric_clear() {
        let mut metric = Metric::new("test", MetricType::Render);
        metric.add_sample(Duration::from_millis(10));
        metric.clear();
        assert_eq!(metric.sample_count(), 0);
    }

    #[test]
    fn test_metric_samples_slice() {
        let mut metric = Metric::new("test", MetricType::Render);
        metric.add_sample(Duration::from_millis(10));
        metric.add_sample(Duration::from_millis(20));
        let samples = metric.samples();
        assert_eq!(samples.len(), 2);
    }

    #[test]
    fn test_metric_stats_empty() {
        let metric = Metric::new("test", MetricType::Render);
        assert!(metric.stats().is_none());
    }

    #[test]
    fn test_metric_end_without_start() {
        let mut metric = Metric::new("test", MetricType::Render);
        let result = metric.end();
        assert!(result.is_none());
    }

    #[test]
    fn test_metric_double_start() {
        let mut metric = Metric::new("test", MetricType::Render);
        metric.start();
        thread::sleep(Duration::from_millis(5));
        metric.start(); // Should reset start time
        thread::sleep(Duration::from_millis(2));
        let duration = metric.end();
        // Duration should be approximately 2ms (after second start), not 7ms
        // Use generous tolerance for CI systems
        assert!(duration.unwrap() < Duration::from_millis(50));
    }

    #[test]
    fn test_stats_from_samples_empty() {
        let stats = Stats::from_samples(&[]);
        assert!(stats.is_none());
    }

    #[test]
    fn test_stats_from_samples_single() {
        let samples = vec![Sample::new(Duration::from_millis(10))];
        let stats = Stats::from_samples(&samples).unwrap();
        assert_eq!(stats.count, 1);
        assert_eq!(stats.avg, Duration::from_millis(10));
        assert_eq!(stats.min, Duration::from_millis(10));
        assert_eq!(stats.max, Duration::from_millis(10));
    }

    #[test]
    fn test_fps_counter_default() {
        let fps = FpsCounter::default();
        assert_eq!(fps.fps(), 0.0);
    }

    #[test]
    fn test_fps_counter_window() {
        let fps = FpsCounter::new().window(Duration::from_secs(2));
        // Just verify it can be set
        let _ = fps;
    }

    #[test]
    fn test_fps_counter_frame_time() {
        let mut fps = FpsCounter::new();
        fps.frame();
        fps.frame();
        let frame_time = fps.frame_time();
        // With only 2 frames, may or may not have valid FPS
        let _ = frame_time;
    }

    #[test]
    fn test_fps_counter_reset() {
        let mut fps = FpsCounter::new();
        fps.frame();
        fps.frame();
        fps.reset();
        assert_eq!(fps.fps(), 0.0);
    }

    #[test]
    fn test_metric_type_all_variants() {
        let types = [
            MetricType::Render,
            MetricType::Event,
            MetricType::Layout,
            MetricType::Custom,
        ];
        for (i, t1) in types.iter().enumerate() {
            for (j, t2) in types.iter().enumerate() {
                if i == j {
                    assert_eq!(t1, t2);
                } else {
                    assert_ne!(t1, t2);
                }
            }
        }
    }

    #[test]
    fn test_sample_new() {
        let duration = Duration::from_millis(100);
        let sample = Sample::new(duration);
        assert_eq!(sample.duration, duration);
    }

    #[test]
    fn test_sample_timestamp() {
        let before = Instant::now();
        let sample = Sample::new(Duration::ZERO);
        let after = Instant::now();
        assert!(sample.timestamp >= before);
        assert!(sample.timestamp <= after);
    }

    #[test]
    fn test_timing_guard_drop() {
        let mut profiler = Profiler::new();
        {
            let _guard = TimingGuard::new(&mut profiler, "guard_test");
            thread::sleep(Duration::from_millis(5));
        } // Guard drops here
        assert!(profiler.stats("guard_test").is_some());
    }

    #[test]
    fn test_snapshot_all_fields() {
        let mut profiler = Profiler::new();
        profiler.record("test", Duration::from_millis(10));
        let snapshot = Snapshot::from_profiler(&profiler);
        assert!(snapshot.stats.contains_key("test"));
        assert!(snapshot.runtime >= Duration::ZERO);
    }

    #[test]
    fn test_snapshot_diff_empty() {
        let profiler = Profiler::new();
        let snap1 = Snapshot::from_profiler(&profiler);
        let snap2 = Snapshot::from_profiler(&profiler);
        let diff = snap2.compare(&snap1);
        assert!(diff.metric_diffs.is_empty());
    }

    #[test]
    fn test_snapshot_diff_different_metrics() {
        let mut profiler1 = Profiler::new();
        profiler1.record("metric1", Duration::from_millis(10));
        let snap1 = Snapshot::from_profiler(&profiler1);

        let mut profiler2 = Profiler::new();
        profiler2.record("metric2", Duration::from_millis(20));
        let snap2 = Snapshot::from_profiler(&profiler2);

        let diff = snap2.compare(&snap1);
        // No common metrics to compare
        assert!(diff.metric_diffs.is_empty());
    }
}