kael 0.2.0

GPU-accelerated native UI framework for Rust — build desktop apps with Metal, DirectX, and Vulkan rendering
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
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
//! Product-level benchmark workloads and harness for GPUI.
//!
//! This module defines standard benchmark scenarios that resemble real
//! products: messaging UI, workspace/editor UI, and media-control dashboard
//! UI. These workloads are used to measure startup, memory, responsiveness,
//! and energy use against Electron baselines.

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

use serde::{Deserialize, Serialize};

use crate::tracer::Tracer;

// ---------------------------------------------------------------------------
// Benchmark Scenarios
// ---------------------------------------------------------------------------

/// A predefined benchmark scenario resembling a real product workload.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum BenchmarkScenario {
    /// A messaging/chat client UI with lists, avatars, and input.
    Messaging,
    /// A workspace/editor UI with panes, tabs, and file trees.
    Workspace,
    /// A media-control dashboard with previews and device routing.
    MediaControl,
    /// IDE workspace with file tree, tabs, editor, terminal, diagnostics panel.
    Ide,
    /// Chat app with thousands of messages and live typing indicators.
    Chat,
    /// Notion-style document with nested blocks, embeds, and large undo history.
    Document,
    /// Figma-style canvas with thousands of nodes, pan/zoom, selection.
    Canvas,
    /// OBS/video editor with live preview, thumbnails, waveforms, and export.
    VideoEditor,
    /// Data dashboard with large tables, charts, filters, and real-time updates.
    Dashboard,
}

impl BenchmarkScenario {
    /// Human-readable description of the scenario.
    pub fn description(&self) -> &'static str {
        match self {
            Self::Messaging => {
                "Chat interface with conversation list, message bubbles, and composer"
            }
            Self::Workspace => "IDE-like workspace with sidebar, editor tabs, and terminal panel",
            Self::MediaControl => {
                "OBS-style control surface with scene list, preview, and source properties"
            }
            Self::Ide => "Full IDE with file tree, tabs, editor, terminal, and diagnostics panel",
            Self::Chat => {
                "Chat app with thousands of messages, threads, and live typing indicators"
            }
            Self::Document => {
                "Notion-style document with nested blocks, embeds, and large undo history"
            }
            Self::Canvas => "Figma-style canvas with thousands of nodes, pan/zoom, and selection",
            Self::VideoEditor => {
                "Video editor with live preview, timeline, thumbnails, waveforms, and export"
            }
            Self::Dashboard => {
                "Data dashboard with large tables, charts, filters, and real-time updates"
            }
        }
    }

    /// Approximate complexity score (higher = more elements).
    pub fn complexity_score(&self) -> u32 {
        match self {
            Self::Messaging => 500,
            Self::Workspace => 1200,
            Self::MediaControl => 800,
            Self::Ide => 2000,
            Self::Chat => 1500,
            Self::Document => 1000,
            Self::Canvas => 3000,
            Self::VideoEditor => 2500,
            Self::Dashboard => 1800,
        }
    }

    /// Returns all defined benchmark scenarios.
    pub fn all() -> &'static [BenchmarkScenario] {
        &[
            Self::Messaging,
            Self::Workspace,
            Self::MediaControl,
            Self::Ide,
            Self::Chat,
            Self::Document,
            Self::Canvas,
            Self::VideoEditor,
            Self::Dashboard,
        ]
    }
}

// ---------------------------------------------------------------------------
// Metrics
// ---------------------------------------------------------------------------

/// A single measurement collected during a benchmark run.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct BenchmarkMeasurement {
    /// The metric being measured.
    pub metric: BenchmarkMetric,
    /// The measured value.
    pub value: f64,
    /// The unit of the value.
    pub unit: MetricUnit,
    /// When the measurement was taken relative to benchmark start.
    pub elapsed: Duration,
}

/// Types of benchmark metrics.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum BenchmarkMetric {
    /// Time from process launch to first frame rendered.
    ColdStart,
    /// Time from background to foreground with first frame rendered.
    WarmStart,
    /// Time until the UI is interactive after launch.
    FirstInteractiveFrame,
    /// Resident memory at idle.
    IdleMemory,
    /// Input event to frame presentation latency.
    InputLatency,
    /// Median frame time (50th percentile).
    FrameTimeP50,
    /// 95th percentile frame time.
    FrameTimeP95,
    /// 99th percentile frame time.
    FrameTimeP99,
    /// Input-to-present latency during scroll interactions.
    ScrollLatency,
    /// Time to complete a window resize interaction smoothly.
    ResizeSmoothness,
    /// Time to complete a scroll interaction smoothly.
    ScrollSmoothness,
    /// Memory growth over a long session in megabytes.
    MemoryGrowth,
    /// CPU utilization over a long session.
    LongSessionCpu,
    /// GPU utilization percentage.
    GpuUsage,
    /// Energy impact score over a long session.
    LongSessionEnergy,
    /// Idle power consumption score.
    IdlePower,
    /// Thread/timer wakeups per second at idle.
    WakeupsPerSecond,
    /// Asset cache hit rate as a percentage.
    AssetCacheHitRate,
}

/// Units for benchmark measurements.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum MetricUnit {
    /// Time in milliseconds.
    Milliseconds,
    /// Time in microseconds.
    Microseconds,
    /// Memory in megabytes.
    Megabytes,
    /// A percentage value.
    Percent,
    /// Frame rate in frames per second.
    FramesPerSecond,
    /// Wakeups per second.
    WakeupsPerSec,
    /// A dimensionless score.
    Score,
}

impl std::fmt::Display for MetricUnit {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Milliseconds => write!(f, "ms"),
            Self::Microseconds => write!(f, "µs"),
            Self::Megabytes => write!(f, "MB"),
            Self::Percent => write!(f, "%"),
            Self::FramesPerSecond => write!(f, "fps"),
            Self::WakeupsPerSec => write!(f, "wakeups/s"),
            Self::Score => write!(f, "score"),
        }
    }
}

impl BenchmarkMetric {
    /// Whether lower values are better for this metric.
    pub fn lower_is_better(&self) -> bool {
        match self {
            Self::AssetCacheHitRate => false,
            _ => true,
        }
    }
}

// ---------------------------------------------------------------------------
// Benchmark Run
// ---------------------------------------------------------------------------

/// The full result of a benchmark run.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct BenchmarkResult {
    /// The scenario that was benchmarked.
    pub scenario: BenchmarkScenario,
    /// The name of the platform/framework under test.
    pub subject: String,
    /// Individual measurements.
    pub measurements: Vec<BenchmarkMeasurement>,
    /// Start time of the benchmark.
    #[serde(skip, default = "Instant::now")]
    pub started_at: Instant,
    /// Total duration of the benchmark run.
    pub duration: Duration,
    /// Hardware and OS conditions recorded for fair comparison.
    pub environment: BenchmarkEnvironment,
}

/// Hardware and OS environment recorded during benchmarking.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct BenchmarkEnvironment {
    /// Operating system name.
    pub os_name: String,
    /// Operating system version.
    pub os_version: String,
    /// CPU description.
    pub cpu: String,
    /// Total system memory in GB.
    pub memory_gb: u32,
    /// GPU description.
    pub gpu: String,
}

impl BenchmarkEnvironment {
    /// Collect the current environment information.
    pub fn current() -> Self {
        Self {
            os_name: std::env::consts::OS.to_string(),
            os_version: Self::os_version(),
            cpu: Self::cpu_info(),
            memory_gb: Self::system_memory_gb(),
            gpu: String::new(),
        }
    }

    #[cfg(target_os = "macos")]
    fn os_version() -> String {
        unsafe {
            let mut size = 0usize;
            if libc::sysctlbyname(
                c"kern.osproductversion".as_ptr(),
                std::ptr::null_mut(),
                &mut size,
                std::ptr::null_mut(),
                0,
            ) == 0
                && size > 0
            {
                let mut buf = vec![0u8; size];
                if libc::sysctlbyname(
                    c"kern.osproductversion".as_ptr(),
                    buf.as_mut_ptr() as *mut _,
                    &mut size,
                    std::ptr::null_mut(),
                    0,
                ) == 0
                {
                    return String::from_utf8_lossy(&buf[..buf.len().saturating_sub(1)])
                        .to_string();
                }
            }
        }
        String::new()
    }

    #[cfg(not(target_os = "macos"))]
    fn os_version() -> String {
        String::new()
    }

    fn cpu_info() -> String {
        std::env::var("PROCESSOR_IDENTIFIER")
            .or_else(|_| std::env::var("CPU"))
            .unwrap_or_default()
    }

    #[cfg(target_os = "macos")]
    fn system_memory_gb() -> u32 {
        unsafe {
            let mut mem: u64 = 0;
            let mut size = std::mem::size_of::<u64>();
            if libc::sysctlbyname(
                c"hw.memsize".as_ptr(),
                &mut mem as *mut _ as *mut _,
                &mut size,
                std::ptr::null_mut(),
                0,
            ) == 0
            {
                return (mem / (1024 * 1024 * 1024)) as u32;
            }
        }
        0
    }

    #[cfg(target_os = "linux")]
    fn system_memory_gb() -> u32 {
        if let Ok(contents) = std::fs::read_to_string("/proc/meminfo") {
            for line in contents.lines() {
                if let Some(rest) = line.strip_prefix("MemTotal:") {
                    if let Some(kb_str) = rest.trim().split_whitespace().next() {
                        if let Ok(kb) = kb_str.parse::<u64>() {
                            return (kb / (1024 * 1024)) as u32;
                        }
                    }
                }
            }
        }
        0
    }

    #[cfg(not(any(target_os = "macos", target_os = "linux")))]
    fn system_memory_gb() -> u32 {
        0
    }
}

// ---------------------------------------------------------------------------
// Metric Collectors
// ---------------------------------------------------------------------------

/// Trait for benchmark metric collectors.
pub trait MetricCollector: Send {
    /// Start the collector.
    fn start(&mut self);
    /// Stop the collector and return measurements.
    fn stop(&mut self) -> Vec<BenchmarkMeasurement>;
    /// Return a mutable reference to `Any` for downcasting.
    fn as_any_mut(&mut self) -> &mut dyn std::any::Any;
}

/// Measures time from creation until first collection.
pub struct ColdStartCollector {
    start: Instant,
    stopped: bool,
}

impl ColdStartCollector {
    /// Create a new collector starting now.
    pub fn new() -> Self {
        Self {
            start: Instant::now(),
            stopped: false,
        }
    }
}

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

impl MetricCollector for ColdStartCollector {
    fn start(&mut self) {
        self.start = Instant::now();
        self.stopped = false;
    }

    fn stop(&mut self) -> Vec<BenchmarkMeasurement> {
        if self.stopped {
            return Vec::new();
        }
        self.stopped = true;
        let elapsed = self.start.elapsed();
        vec![BenchmarkMeasurement {
            metric: BenchmarkMetric::ColdStart,
            value: elapsed.as_secs_f64() * 1000.0,
            unit: MetricUnit::Milliseconds,
            elapsed,
        }]
    }

    fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
        self
    }
}

/// Measures resident memory using platform APIs.
pub struct MemoryCollector {
    sample_time: Instant,
}

impl MemoryCollector {
    /// Create a new memory collector.
    pub fn new() -> Self {
        Self {
            sample_time: Instant::now(),
        }
    }

    /// Read current resident memory in megabytes.
    pub fn resident_mb() -> f64 {
        #[cfg(target_os = "linux")]
        {
            if let Ok(contents) = std::fs::read_to_string("/proc/self/status") {
                for line in contents.lines() {
                    if let Some(rest) = line.strip_prefix("VmRSS:") {
                        if let Some(kb_str) = rest.trim().split_whitespace().next() {
                            if let Ok(kb) = kb_str.parse::<f64>() {
                                return kb / 1024.0;
                            }
                        }
                    }
                }
            }
        }

        #[cfg(target_os = "macos")]
        {
            let mut rusage: libc::rusage = unsafe { std::mem::zeroed() };
            if unsafe { libc::getrusage(libc::RUSAGE_SELF, &mut rusage) } == 0 {
                return rusage.ru_maxrss as f64 / (1024.0 * 1024.0);
            }
        }

        #[cfg(target_os = "windows")]
        {
            use windows::Win32::System::ProcessStatus::GetProcessMemoryInfo;
            use windows::Win32::System::Threading::GetCurrentProcess;
            unsafe {
                let mut counters = std::mem::zeroed();
                let process = GetCurrentProcess();
                if GetProcessMemoryInfo(
                    process,
                    &mut counters,
                    std::mem::size_of_val(&counters) as u32,
                )
                .is_ok()
                {
                    return counters.WorkingSetSize as f64 / (1024.0 * 1024.0);
                }
            }
        }

        0.0
    }
}

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

impl MetricCollector for MemoryCollector {
    fn start(&mut self) {
        self.sample_time = Instant::now();
    }

    fn stop(&mut self) -> Vec<BenchmarkMeasurement> {
        vec![BenchmarkMeasurement {
            metric: BenchmarkMetric::IdleMemory,
            value: Self::resident_mb(),
            unit: MetricUnit::Megabytes,
            elapsed: self.sample_time.elapsed(),
        }]
    }

    fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
        self
    }
}

/// Measures input-to-frame presentation latency.
pub struct InputLatencyCollector {
    input_time: Option<Instant>,
    latencies: Vec<Duration>,
}

impl InputLatencyCollector {
    /// Create a new latency collector.
    pub fn new() -> Self {
        Self {
            input_time: None,
            latencies: Vec::new(),
        }
    }

    /// Record that an input event occurred.
    pub fn record_input(&mut self) {
        self.input_time = Some(Instant::now());
    }

    /// Record that the frame was presented.
    pub fn record_frame_presented(&mut self) {
        if let Some(input_time) = self.input_time.take() {
            self.latencies.push(input_time.elapsed());
        }
    }

    /// Average latency in milliseconds.
    pub fn average_ms(&self) -> f64 {
        if self.latencies.is_empty() {
            return 0.0;
        }
        let total_us: u128 = self.latencies.iter().map(|d| d.as_micros()).sum();
        total_us as f64 / self.latencies.len() as f64 / 1000.0
    }
}

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

impl MetricCollector for InputLatencyCollector {
    fn start(&mut self) {
        self.input_time = None;
        self.latencies.clear();
    }

    fn stop(&mut self) -> Vec<BenchmarkMeasurement> {
        vec![BenchmarkMeasurement {
            metric: BenchmarkMetric::InputLatency,
            value: self.average_ms(),
            unit: MetricUnit::Milliseconds,
            elapsed: Duration::default(),
        }]
    }

    fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
        self
    }
}

/// Measures frame times during resize or scroll interactions.
pub struct SmoothnessCollector {
    frames: Vec<Duration>,
    last_frame: Option<Instant>,
    metric: BenchmarkMetric,
}

impl SmoothnessCollector {
    /// Create a new smoothness collector for the given metric.
    pub fn new(metric: BenchmarkMetric) -> Self {
        assert!(
            metric == BenchmarkMetric::ResizeSmoothness
                || metric == BenchmarkMetric::ScrollSmoothness,
            "SmoothnessCollector only supports resize or scroll metrics"
        );
        Self {
            frames: Vec::new(),
            last_frame: None,
            metric,
        }
    }

    /// Record a frame timestamp.
    pub fn record_frame(&mut self) {
        let now = Instant::now();
        if let Some(last) = self.last_frame {
            self.frames.push(now.duration_since(last));
        }
        self.last_frame = Some(now);
    }

    /// Average frame time in milliseconds.
    pub fn average_frame_time_ms(&self) -> f64 {
        if self.frames.is_empty() {
            return 0.0;
        }
        let total_us: u128 = self.frames.iter().map(|d| d.as_micros()).sum();
        total_us as f64 / self.frames.len() as f64 / 1000.0
    }

    /// Minimum frame time in milliseconds.
    pub fn min_frame_time_ms(&self) -> f64 {
        self.frames
            .iter()
            .map(|d| d.as_secs_f64() * 1000.0)
            .fold(f64::MAX, f64::min)
            .min(f64::MAX)
    }

    /// Maximum frame time in milliseconds.
    pub fn max_frame_time_ms(&self) -> f64 {
        self.frames
            .iter()
            .map(|d| d.as_secs_f64() * 1000.0)
            .fold(0.0, f64::max)
    }

    /// Estimated FPS from average frame time.
    pub fn estimated_fps(&self) -> f64 {
        let avg_ms = self.average_frame_time_ms();
        if avg_ms > 0.0 { 1000.0 / avg_ms } else { 0.0 }
    }
}

impl MetricCollector for SmoothnessCollector {
    fn start(&mut self) {
        self.frames.clear();
        self.last_frame = None;
    }

    fn stop(&mut self) -> Vec<BenchmarkMeasurement> {
        vec![
            BenchmarkMeasurement {
                metric: self.metric,
                value: self.average_frame_time_ms(),
                unit: MetricUnit::Milliseconds,
                elapsed: Duration::default(),
            },
            BenchmarkMeasurement {
                metric: self.metric,
                value: self.estimated_fps(),
                unit: MetricUnit::FramesPerSecond,
                elapsed: Duration::default(),
            },
        ]
    }

    fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
        self
    }
}

/// Measures CPU utilization and energy impact over a long session.
pub struct LongSessionCollector {
    start: Instant,
    samples: Vec<CpuSample>,
    last_cpu_time: Duration,
    sampling_interval: Duration,
}

#[derive(Debug, Clone, Copy)]
#[allow(dead_code)]
struct CpuSample {
    elapsed: Duration,
    cpu_percent: f64,
}

impl LongSessionCollector {
    /// Create a new long session collector with the given sampling interval.
    pub fn new(sampling_interval: Duration) -> Self {
        Self {
            start: Instant::now(),
            samples: Vec::new(),
            last_cpu_time: Duration::default(),
            sampling_interval,
        }
    }

    /// Sample current CPU usage. Call periodically.
    pub fn sample(&mut self) {
        let now = Instant::now();
        let elapsed = now.duration_since(self.start);
        let cpu_time = Self::process_cpu_time();
        let delta_cpu = cpu_time.saturating_sub(self.last_cpu_time);
        self.last_cpu_time = cpu_time;

        let cpu_percent = if self.sampling_interval.as_secs_f64() > 0.0 {
            (delta_cpu.as_secs_f64() / self.sampling_interval.as_secs_f64()) * 100.0
        } else {
            0.0
        };

        self.samples.push(CpuSample {
            elapsed,
            cpu_percent: cpu_percent.min(100.0 * num_cpus::get() as f64),
        });
    }

    fn process_cpu_time() -> Duration {
        #[cfg(any(target_os = "macos", target_os = "linux"))]
        {
            let mut rusage: libc::rusage = unsafe { std::mem::zeroed() };
            if unsafe { libc::getrusage(libc::RUSAGE_SELF, &mut rusage) } == 0 {
                let utime = Duration::from_secs(rusage.ru_utime.tv_sec as u64)
                    + Duration::from_micros(rusage.ru_utime.tv_usec as u64);
                let stime = Duration::from_secs(rusage.ru_stime.tv_sec as u64)
                    + Duration::from_micros(rusage.ru_stime.tv_usec as u64);
                return utime + stime;
            }
        }

        #[cfg(target_os = "windows")]
        {
            use windows::Win32::System::Threading::GetCurrentProcess;
            use windows::Win32::System::Threading::GetProcessTimes;
            unsafe {
                let mut creation = std::mem::zeroed();
                let mut exit = std::mem::zeroed();
                let mut kernel = std::mem::zeroed();
                let mut user = std::mem::zeroed();
                let process = GetCurrentProcess();
                if GetProcessTimes(process, &mut creation, &mut exit, &mut kernel, &mut user)
                    .is_ok()
                {
                    let kernel_us =
                        ((kernel.dwHighDateTime as u64) << 32 | kernel.dwLowDateTime as u64) / 10;
                    let user_us =
                        ((user.dwHighDateTime as u64) << 32 | user.dwLowDateTime as u64) / 10;
                    return Duration::from_micros(kernel_us + user_us);
                }
            }
        }

        Duration::default()
    }

    /// Average CPU percentage across all samples.
    pub fn average_cpu_percent(&self) -> f64 {
        if self.samples.is_empty() {
            return 0.0;
        }
        self.samples.iter().map(|s| s.cpu_percent).sum::<f64>() / self.samples.len() as f64
    }

    /// Estimated energy impact score (0-100, higher = more energy used).
    pub fn energy_score(&self) -> f64 {
        let avg_cpu = self.average_cpu_percent();
        let duration_minutes = self.start.elapsed().as_secs_f64() / 60.0;
        (avg_cpu * duration_minutes / 100.0).min(100.0)
    }
}

impl Default for LongSessionCollector {
    fn default() -> Self {
        Self::new(Duration::from_secs(1))
    }
}

impl MetricCollector for LongSessionCollector {
    fn start(&mut self) {
        self.start = Instant::now();
        self.samples.clear();
        self.last_cpu_time = Self::process_cpu_time();
    }

    fn stop(&mut self) -> Vec<BenchmarkMeasurement> {
        vec![
            BenchmarkMeasurement {
                metric: BenchmarkMetric::LongSessionCpu,
                value: self.average_cpu_percent(),
                unit: MetricUnit::Percent,
                elapsed: self.start.elapsed(),
            },
            BenchmarkMeasurement {
                metric: BenchmarkMetric::LongSessionEnergy,
                value: self.energy_score(),
                unit: MetricUnit::Score,
                elapsed: self.start.elapsed(),
            },
        ]
    }

    fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
        self
    }
}

/// Collects frame times and computes percentiles (P50, P95, P99).
pub struct FrameTimeCollector {
    frame_times: Vec<Duration>,
    last_frame: Option<Instant>,
}

impl FrameTimeCollector {
    /// Create a new frame time collector.
    pub fn new() -> Self {
        Self {
            frame_times: Vec::new(),
            last_frame: None,
        }
    }

    /// Record a frame timestamp for percentile computation.
    pub fn record_frame(&mut self) {
        let now = Instant::now();
        if let Some(last) = self.last_frame {
            self.frame_times.push(now.duration_since(last));
        }
        self.last_frame = Some(now);
    }

    fn percentile_ms(&self, p: f64) -> f64 {
        if self.frame_times.is_empty() {
            return 0.0;
        }
        let mut sorted: Vec<f64> = self
            .frame_times
            .iter()
            .map(|d| d.as_secs_f64() * 1000.0)
            .collect();
        sorted.sort_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal));
        let idx = ((p / 100.0) * (sorted.len() - 1) as f64).round() as usize;
        sorted[idx.min(sorted.len() - 1)]
    }
}

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

impl MetricCollector for FrameTimeCollector {
    fn start(&mut self) {
        self.frame_times.clear();
        self.last_frame = None;
    }

    fn stop(&mut self) -> Vec<BenchmarkMeasurement> {
        vec![
            BenchmarkMeasurement {
                metric: BenchmarkMetric::FrameTimeP50,
                value: self.percentile_ms(50.0),
                unit: MetricUnit::Milliseconds,
                elapsed: Duration::default(),
            },
            BenchmarkMeasurement {
                metric: BenchmarkMetric::FrameTimeP95,
                value: self.percentile_ms(95.0),
                unit: MetricUnit::Milliseconds,
                elapsed: Duration::default(),
            },
            BenchmarkMeasurement {
                metric: BenchmarkMetric::FrameTimeP99,
                value: self.percentile_ms(99.0),
                unit: MetricUnit::Milliseconds,
                elapsed: Duration::default(),
            },
        ]
    }

    fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
        self
    }
}

/// Tracks memory growth over the duration of a benchmark.
pub struct MemoryGrowthCollector {
    start_memory_mb: f64,
}

impl MemoryGrowthCollector {
    /// Create a new memory growth collector.
    pub fn new() -> Self {
        Self {
            start_memory_mb: 0.0,
        }
    }
}

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

impl MetricCollector for MemoryGrowthCollector {
    fn start(&mut self) {
        self.start_memory_mb = MemoryCollector::resident_mb();
    }

    fn stop(&mut self) -> Vec<BenchmarkMeasurement> {
        let end_mb = MemoryCollector::resident_mb();
        vec![BenchmarkMeasurement {
            metric: BenchmarkMetric::MemoryGrowth,
            value: end_mb - self.start_memory_mb,
            unit: MetricUnit::Megabytes,
            elapsed: Duration::default(),
        }]
    }

    fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
        self
    }
}

/// Tracks asset cache hit rate during a benchmark.
pub struct CacheHitRateCollector {
    hits: u64,
    misses: u64,
}

impl CacheHitRateCollector {
    /// Create a new cache hit rate collector.
    pub fn new() -> Self {
        Self { hits: 0, misses: 0 }
    }

    /// Record a cache hit.
    pub fn record_hit(&mut self) {
        self.hits += 1;
    }

    /// Record a cache miss.
    pub fn record_miss(&mut self) {
        self.misses += 1;
    }

    /// Compute hit rate as a percentage (0-100).
    pub fn hit_rate(&self) -> f64 {
        let total = self.hits + self.misses;
        if total == 0 {
            return 0.0;
        }
        (self.hits as f64 / total as f64) * 100.0
    }
}

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

impl MetricCollector for CacheHitRateCollector {
    fn start(&mut self) {
        self.hits = 0;
        self.misses = 0;
    }

    fn stop(&mut self) -> Vec<BenchmarkMeasurement> {
        vec![BenchmarkMeasurement {
            metric: BenchmarkMetric::AssetCacheHitRate,
            value: self.hit_rate(),
            unit: MetricUnit::Percent,
            elapsed: Duration::default(),
        }]
    }

    fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
        self
    }
}

// ---------------------------------------------------------------------------
// Regression Thresholds
// ---------------------------------------------------------------------------

/// Configurable regression thresholds per metric.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RegressionThresholds {
    /// Default threshold percentage for all metrics.
    pub default_percent: f64,
    /// Per-metric threshold overrides.
    pub overrides: std::collections::HashMap<BenchmarkMetric, f64>,
}

impl RegressionThresholds {
    /// Create thresholds with the given default percentage.
    pub fn new(default_percent: f64) -> Self {
        Self {
            default_percent,
            overrides: std::collections::HashMap::new(),
        }
    }

    /// Add a per-metric threshold override.
    pub fn with_override(mut self, metric: BenchmarkMetric, percent: f64) -> Self {
        self.overrides.insert(metric, percent);
        self
    }

    /// Get the threshold for a specific metric.
    pub fn threshold_for(&self, metric: BenchmarkMetric) -> f64 {
        self.overrides
            .get(&metric)
            .copied()
            .unwrap_or(self.default_percent)
    }
}

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

/// Check regressions with per-metric thresholds.
pub fn check_regressions_with_thresholds(
    baseline: &[BenchmarkResult],
    candidate: &[BenchmarkResult],
    thresholds: &RegressionThresholds,
) -> Vec<Regression> {
    let mut regressions = Vec::new();

    for candidate_result in candidate {
        if let Some(baseline_result) = baseline
            .iter()
            .find(|b| b.scenario == candidate_result.scenario)
        {
            for comparison in compare_results(baseline_result, candidate_result) {
                let threshold = thresholds.threshold_for(comparison.metric);
                let is_regression = if comparison.lower_is_better {
                    comparison.percent_change > threshold
                } else {
                    comparison.percent_change < -threshold
                };

                if is_regression {
                    regressions.push(Regression {
                        scenario: candidate_result.scenario,
                        metric: comparison.metric,
                        baseline: comparison.baseline,
                        candidate: comparison.candidate,
                        percent_change: comparison.percent_change,
                        unit: comparison.unit,
                    });
                }
            }
        }
    }

    regressions
}

// ---------------------------------------------------------------------------
// CI Report
// ---------------------------------------------------------------------------

/// A CI-friendly benchmark report.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CiReport {
    /// Benchmark results from the candidate run.
    pub results: Vec<BenchmarkResult>,
    /// Detected regressions.
    pub regressions: Vec<Regression>,
    /// Whether the run passed all regression checks.
    pub passed: bool,
    /// Path to the attached Chrome Trace file, if any.
    pub trace_file: Option<String>,
}

impl CiReport {
    /// Generate a CI report comparing candidate results against a baseline.
    pub fn generate(
        baseline: &[BenchmarkResult],
        candidate: &[BenchmarkResult],
        thresholds: &RegressionThresholds,
        trace_file: Option<String>,
    ) -> Self {
        let regressions = check_regressions_with_thresholds(baseline, candidate, thresholds);
        Self {
            results: candidate.to_vec(),
            regressions: regressions.clone(),
            passed: regressions.is_empty(),
            trace_file,
        }
    }

    /// Serialize the report to JSON.
    pub fn to_json(&self) -> Result<String, serde_json::Error> {
        serde_json::to_string_pretty(self)
    }

    /// Generate a human-readable summary of the report.
    pub fn summary(&self) -> String {
        let mut out = String::new();
        out.push_str(&format!(
            "Benchmark Report: {}\n",
            if self.passed { "PASSED" } else { "FAILED" }
        ));
        out.push_str(&format!("Results: {} scenarios\n", self.results.len()));
        if !self.regressions.is_empty() {
            out.push_str(&format!("Regressions: {}\n", self.regressions.len()));
            for reg in &self.regressions {
                out.push_str(&format!(
                    "  {:?}/{:?}: {:.1}{} -> {:.1}{} ({:+.1}%)\n",
                    reg.scenario,
                    reg.metric,
                    reg.baseline,
                    reg.unit,
                    reg.candidate,
                    reg.unit,
                    reg.percent_change,
                ));
            }
        }
        if let Some(trace) = &self.trace_file {
            out.push_str(&format!("Trace: {}\n", trace));
        }
        out
    }
}

// ---------------------------------------------------------------------------
// Harness
// ---------------------------------------------------------------------------

/// A benchmark harness that runs scenarios and collects measurements.
pub struct BenchmarkHarness {
    results: Vec<BenchmarkResult>,
    tracer: Option<Tracer>,
}

impl BenchmarkHarness {
    /// Create a new harness.
    pub fn new() -> Self {
        Self {
            results: Vec::new(),
            tracer: Tracer::global(),
        }
    }

    /// Set a tracer for emitting benchmark phase spans.
    pub fn with_tracer(mut self, tracer: Tracer) -> Self {
        self.tracer = Some(tracer);
        self
    }

    /// Run a benchmark scenario and collect results.
    pub fn run<F>(
        &mut self,
        scenario: BenchmarkScenario,
        subject: impl Into<String>,
        runner: F,
    ) -> BenchmarkResult
    where
        F: FnOnce(&mut Vec<BenchmarkMeasurement>),
    {
        let subject = subject.into();
        let started_at = Instant::now();
        let mut measurements = Vec::new();

        let tracer = self.tracer.clone();
        if let Some(tracer) = tracer {
            let phase_name = format!("benchmark_start:{:?}", scenario);
            tracer.record_duration(phase_name, "benchmark", || {
                tracer.record_duration("runner", "benchmark", || {
                    runner(&mut measurements);
                });
            });
        } else {
            runner(&mut measurements);
        }

        let duration = started_at.elapsed();

        let result = BenchmarkResult {
            scenario,
            subject,
            measurements,
            started_at,
            duration,
            environment: BenchmarkEnvironment::current(),
        };

        self.results.push(result.clone());
        result
    }

    /// Run a benchmark with explicit metric collectors.
    pub fn run_with_collectors(
        &mut self,
        scenario: BenchmarkScenario,
        subject: impl Into<String>,
        collectors: &mut [&mut dyn MetricCollector],
        runner: impl FnOnce(&mut [&mut dyn MetricCollector]),
    ) -> BenchmarkResult {
        let subject = subject.into();
        let started_at = Instant::now();

        for collector in collectors.iter_mut() {
            collector.start();
        }

        let tracer = self.tracer.clone();
        if let Some(tracer) = tracer {
            tracer.record_duration("runner", "benchmark", || {
                runner(collectors);
            });
        } else {
            runner(collectors);
        }

        let mut measurements = Vec::new();
        for collector in collectors.iter_mut() {
            measurements.extend(collector.stop());
        }

        let duration = started_at.elapsed();

        let result = BenchmarkResult {
            scenario,
            subject,
            measurements,
            started_at,
            duration,
            environment: BenchmarkEnvironment::current(),
        };

        self.results.push(result.clone());
        result
    }

    /// Return all collected results.
    pub fn results(&self) -> &[BenchmarkResult] {
        &self.results
    }

    /// Export results to a JSON string.
    pub fn export_to_json(&self) -> Result<String, serde_json::Error> {
        serde_json::to_string_pretty(&self.results)
    }

    /// Write the attached tracer's events to a Chrome Trace format file.
    pub fn write_trace_artifact(&self, path: impl Into<std::path::PathBuf>) -> anyhow::Result<()> {
        if let Some(tracer) = &self.tracer {
            tracer.write_to_file(path)?;
        }
        Ok(())
    }

    /// Generate a CI report comparing against a baseline file.
    pub fn generate_ci_report(
        &self,
        baseline: &[BenchmarkResult],
        thresholds: &RegressionThresholds,
        trace_file: Option<String>,
    ) -> CiReport {
        CiReport::generate(baseline, &self.results, thresholds, trace_file)
    }
}

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

// ---------------------------------------------------------------------------
// Comparative Analysis
// ---------------------------------------------------------------------------

/// Compare two benchmark results for the same scenario.
pub fn compare_results(
    baseline: &BenchmarkResult,
    candidate: &BenchmarkResult,
) -> Vec<MetricComparison> {
    let mut comparisons = Vec::new();

    for baseline_m in &baseline.measurements {
        if let Some(candidate_m) = candidate
            .measurements
            .iter()
            .find(|m| m.metric == baseline_m.metric)
        {
            let delta = candidate_m.value - baseline_m.value;
            let percent_change = if baseline_m.value != 0.0 {
                (delta / baseline_m.value) * 100.0
            } else {
                0.0
            };

            comparisons.push(MetricComparison {
                metric: baseline_m.metric,
                baseline: baseline_m.value,
                candidate: candidate_m.value,
                delta,
                percent_change,
                unit: baseline_m.unit,
                lower_is_better: baseline_m.metric.lower_is_better(),
            });
        }
    }

    comparisons
}

/// Load benchmark results from a JSON string.
pub fn load_results_from_json(json: &str) -> Result<Vec<BenchmarkResult>, serde_json::Error> {
    serde_json::from_str(json)
}

/// Compare a result set against a baseline file and report regressions.
pub fn check_regressions(
    baseline: &[BenchmarkResult],
    candidate: &[BenchmarkResult],
    threshold_percent: f64,
) -> Vec<Regression> {
    let mut regressions = Vec::new();

    for candidate_result in candidate {
        if let Some(baseline_result) = baseline
            .iter()
            .find(|b| b.scenario == candidate_result.scenario)
        {
            for comparison in compare_results(baseline_result, candidate_result) {
                let is_regression = if comparison.lower_is_better {
                    comparison.percent_change > threshold_percent
                } else {
                    comparison.percent_change < -threshold_percent
                };

                if is_regression {
                    regressions.push(Regression {
                        scenario: candidate_result.scenario,
                        metric: comparison.metric,
                        baseline: comparison.baseline,
                        candidate: comparison.candidate,
                        percent_change: comparison.percent_change,
                        unit: comparison.unit,
                    });
                }
            }
        }
    }

    regressions
}

/// A detected regression between baseline and candidate.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Regression {
    /// The scenario where regression occurred.
    pub scenario: BenchmarkScenario,
    /// The metric that regressed.
    pub metric: BenchmarkMetric,
    /// Baseline value.
    pub baseline: f64,
    /// Candidate value.
    pub candidate: f64,
    /// Percentage change.
    pub percent_change: f64,
    /// Unit of measurement.
    pub unit: MetricUnit,
}

/// A comparison between baseline and candidate for a single metric.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct MetricComparison {
    /// The metric being compared.
    pub metric: BenchmarkMetric,
    /// Baseline measurement.
    pub baseline: f64,
    /// Candidate measurement.
    pub candidate: f64,
    /// Absolute difference (candidate - baseline).
    pub delta: f64,
    /// Percentage change.
    pub percent_change: f64,
    /// Unit of measurement.
    pub unit: MetricUnit,
    /// Whether a lower value is better for this metric.
    pub lower_is_better: bool,
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

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

    #[test]
    fn test_scenario_descriptions() {
        assert!(!BenchmarkScenario::Messaging.description().is_empty());
        assert!(!BenchmarkScenario::Workspace.description().is_empty());
        assert!(!BenchmarkScenario::MediaControl.description().is_empty());
    }

    #[test]
    fn test_harness_run() {
        let mut harness = BenchmarkHarness::new();
        let result = harness.run(BenchmarkScenario::Messaging, "kael", |measurements| {
            measurements.push(BenchmarkMeasurement {
                metric: BenchmarkMetric::ColdStart,
                value: 120.0,
                unit: MetricUnit::Milliseconds,
                elapsed: Duration::from_secs(1),
            });
        });

        assert_eq!(result.scenario, BenchmarkScenario::Messaging);
        assert_eq!(result.subject, "kael");
        assert_eq!(result.measurements.len(), 1);
        assert_eq!(result.measurements[0].value, 120.0);
    }

    #[test]
    fn test_harness_run_with_collectors() {
        let mut harness = BenchmarkHarness::new();
        let mut cold_start = ColdStartCollector::new();
        let mut memory = MemoryCollector::new();
        let mut collectors: [&mut dyn MetricCollector; 2] = [&mut cold_start, &mut memory];

        let result = harness.run_with_collectors(
            BenchmarkScenario::Messaging,
            "kael",
            &mut collectors,
            |_collectors| {},
        );

        assert_eq!(result.scenario, BenchmarkScenario::Messaging);
        assert!(!result.measurements.is_empty());
    }

    #[test]
    fn test_compare_results() {
        let baseline = BenchmarkResult {
            scenario: BenchmarkScenario::Messaging,
            subject: "electron".to_string(),
            measurements: vec![
                BenchmarkMeasurement {
                    metric: BenchmarkMetric::ColdStart,
                    value: 500.0,
                    unit: MetricUnit::Milliseconds,
                    elapsed: Duration::from_secs(1),
                },
                BenchmarkMeasurement {
                    metric: BenchmarkMetric::IdleMemory,
                    value: 250.0,
                    unit: MetricUnit::Megabytes,
                    elapsed: Duration::from_secs(2),
                },
            ],
            started_at: Instant::now(),
            duration: Duration::from_secs(3),
            environment: BenchmarkEnvironment::current(),
        };

        let candidate = BenchmarkResult {
            scenario: BenchmarkScenario::Messaging,
            subject: "kael".to_string(),
            measurements: vec![
                BenchmarkMeasurement {
                    metric: BenchmarkMetric::ColdStart,
                    value: 120.0,
                    unit: MetricUnit::Milliseconds,
                    elapsed: Duration::from_secs(1),
                },
                BenchmarkMeasurement {
                    metric: BenchmarkMetric::IdleMemory,
                    value: 80.0,
                    unit: MetricUnit::Megabytes,
                    elapsed: Duration::from_secs(2),
                },
            ],
            started_at: Instant::now(),
            duration: Duration::from_secs(3),
            environment: BenchmarkEnvironment::current(),
        };

        let comparisons = compare_results(&baseline, &candidate);
        assert_eq!(comparisons.len(), 2);

        let cold_start = comparisons
            .iter()
            .find(|c| c.metric == BenchmarkMetric::ColdStart)
            .unwrap();
        assert_eq!(cold_start.delta, -380.0);
        assert_eq!(cold_start.percent_change, -76.0);
        assert!(cold_start.lower_is_better);
    }

    #[test]
    fn test_metric_unit_display() {
        assert_eq!(MetricUnit::Milliseconds.to_string(), "ms");
        assert_eq!(MetricUnit::Megabytes.to_string(), "MB");
        assert_eq!(MetricUnit::Percent.to_string(), "%");
    }

    #[test]
    fn test_cold_start_collector() {
        let mut collector = ColdStartCollector::new();
        std::thread::sleep(Duration::from_millis(10));
        let measurements = collector.stop();
        assert_eq!(measurements.len(), 1);
        assert_eq!(measurements[0].metric, BenchmarkMetric::ColdStart);
        assert!(measurements[0].value >= 10.0);
    }

    #[test]
    fn test_memory_collector_returns_value() {
        let mut collector = MemoryCollector::new();
        let measurements = collector.stop();
        assert_eq!(measurements.len(), 1);
        assert_eq!(measurements[0].metric, BenchmarkMetric::IdleMemory);
        assert!(measurements[0].value >= 0.0);
    }

    #[test]
    fn test_input_latency_collector() {
        let mut collector = InputLatencyCollector::new();
        collector.start();
        collector.record_input();
        std::thread::sleep(Duration::from_millis(5));
        collector.record_frame_presented();
        let measurements = collector.stop();
        assert_eq!(measurements.len(), 1);
        assert_eq!(measurements[0].metric, BenchmarkMetric::InputLatency);
        assert!(measurements[0].value >= 5.0);
    }

    #[test]
    fn test_smoothness_collector() {
        let mut collector = SmoothnessCollector::new(BenchmarkMetric::ScrollSmoothness);
        collector.start();
        for _ in 0..10 {
            std::thread::sleep(Duration::from_millis(16));
            collector.record_frame();
        }
        let measurements = collector.stop();
        assert!(!measurements.is_empty());
    }

    #[test]
    fn test_long_session_collector() {
        let mut collector = LongSessionCollector::new(Duration::from_millis(50));
        collector.start();
        for _ in 0..5 {
            std::thread::sleep(Duration::from_millis(50));
            collector.sample();
        }
        let measurements = collector.stop();
        assert_eq!(measurements.len(), 2);
        let cpu = measurements
            .iter()
            .find(|m| m.metric == BenchmarkMetric::LongSessionCpu)
            .unwrap();
        assert!(cpu.value >= 0.0);
    }

    #[test]
    fn test_check_regressions() {
        let baseline = vec![BenchmarkResult {
            scenario: BenchmarkScenario::Messaging,
            subject: "baseline".to_string(),
            measurements: vec![BenchmarkMeasurement {
                metric: BenchmarkMetric::ColdStart,
                value: 100.0,
                unit: MetricUnit::Milliseconds,
                elapsed: Duration::default(),
            }],
            started_at: Instant::now(),
            duration: Duration::default(),
            environment: BenchmarkEnvironment::current(),
        }];

        let candidate = vec![BenchmarkResult {
            scenario: BenchmarkScenario::Messaging,
            subject: "candidate".to_string(),
            measurements: vec![BenchmarkMeasurement {
                metric: BenchmarkMetric::ColdStart,
                value: 150.0,
                unit: MetricUnit::Milliseconds,
                elapsed: Duration::default(),
            }],
            started_at: Instant::now(),
            duration: Duration::default(),
            environment: BenchmarkEnvironment::current(),
        }];

        let regressions = check_regressions(&baseline, &candidate, 10.0);
        assert_eq!(regressions.len(), 1);
        assert_eq!(regressions[0].percent_change, 50.0);
    }

    #[test]
    fn test_all_scenarios_have_descriptions() {
        for scenario in BenchmarkScenario::all() {
            assert!(!scenario.description().is_empty());
            assert!(scenario.complexity_score() > 0);
        }
    }

    #[test]
    fn test_frame_time_collector() {
        let mut collector = FrameTimeCollector::new();
        collector.start();
        for _ in 0..20 {
            std::thread::sleep(Duration::from_millis(8));
            collector.record_frame();
        }
        let measurements = collector.stop();
        assert_eq!(measurements.len(), 3);
        let p50 = measurements
            .iter()
            .find(|m| m.metric == BenchmarkMetric::FrameTimeP50)
            .unwrap();
        let p99 = measurements
            .iter()
            .find(|m| m.metric == BenchmarkMetric::FrameTimeP99)
            .unwrap();
        assert!(p50.value > 0.0);
        assert!(p99.value >= p50.value);
    }

    #[test]
    fn test_memory_growth_collector() {
        let mut collector = MemoryGrowthCollector::new();
        collector.start();
        let measurements = collector.stop();
        assert_eq!(measurements.len(), 1);
        assert_eq!(measurements[0].metric, BenchmarkMetric::MemoryGrowth);
    }

    #[test]
    fn test_cache_hit_rate_collector() {
        let mut collector = CacheHitRateCollector::new();
        collector.start();
        for _ in 0..7 {
            collector.record_hit();
        }
        for _ in 0..3 {
            collector.record_miss();
        }
        let measurements = collector.stop();
        assert_eq!(measurements.len(), 1);
        assert_eq!(measurements[0].metric, BenchmarkMetric::AssetCacheHitRate);
        assert!((measurements[0].value - 70.0).abs() < 0.01);
    }

    #[test]
    fn test_regression_thresholds() {
        let thresholds =
            RegressionThresholds::new(10.0).with_override(BenchmarkMetric::ColdStart, 5.0);
        assert_eq!(thresholds.threshold_for(BenchmarkMetric::ColdStart), 5.0);
        assert_eq!(thresholds.threshold_for(BenchmarkMetric::IdleMemory), 10.0);
    }

    #[test]
    fn test_ci_report_generation() {
        let baseline = vec![BenchmarkResult {
            scenario: BenchmarkScenario::Ide,
            subject: "baseline".to_string(),
            measurements: vec![BenchmarkMeasurement {
                metric: BenchmarkMetric::ColdStart,
                value: 100.0,
                unit: MetricUnit::Milliseconds,
                elapsed: Duration::default(),
            }],
            started_at: Instant::now(),
            duration: Duration::default(),
            environment: BenchmarkEnvironment::current(),
        }];

        let candidate = vec![BenchmarkResult {
            scenario: BenchmarkScenario::Ide,
            subject: "candidate".to_string(),
            measurements: vec![BenchmarkMeasurement {
                metric: BenchmarkMetric::ColdStart,
                value: 105.0,
                unit: MetricUnit::Milliseconds,
                elapsed: Duration::default(),
            }],
            started_at: Instant::now(),
            duration: Duration::default(),
            environment: BenchmarkEnvironment::current(),
        }];

        let thresholds = RegressionThresholds::new(10.0);
        let report = CiReport::generate(&baseline, &candidate, &thresholds, None);
        assert!(report.passed);
        assert!(report.regressions.is_empty());
        assert!(!report.summary().is_empty());
        assert!(report.to_json().is_ok());
    }

    #[test]
    fn test_metric_lower_is_better() {
        assert!(BenchmarkMetric::ColdStart.lower_is_better());
        assert!(BenchmarkMetric::IdleMemory.lower_is_better());
        assert!(!BenchmarkMetric::AssetCacheHitRate.lower_is_better());
    }

    #[test]
    fn test_load_results_from_json() {
        let json = r#"[{
            "scenario": "Messaging",
            "subject": "kael",
            "measurements": [{
                "metric": "ColdStart",
                "value": 120.0,
                "unit": "Milliseconds",
                "elapsed": {"secs": 1, "nanos": 0}
            }],
            "duration": {"secs": 2, "nanos": 0},
            "environment": {
                "os_name": "linux",
                "os_version": "",
                "cpu": "",
                "memory_gb": 0,
                "gpu": ""
            }
        }]"#;
        let results = load_results_from_json(json).unwrap();
        assert_eq!(results.len(), 1);
        assert_eq!(results[0].scenario, BenchmarkScenario::Messaging);
    }
}