1#![allow(clippy::redundant_pub_crate)]
7
8mod export;
9mod metrics;
10mod span;
11mod trace;
12
13pub use export::{ChromeTrace, CiMetrics, FlameGraph};
14pub use metrics::{FrameMetrics, MemoryMetrics, PerformanceMetrics, Statistics};
15pub use span::{Span, SpanGuard, SpanId};
16pub use trace::{Trace, TraceConfig, Tracer};
17
18pub const DEFAULT_SAMPLE_RATE: u32 = 1000;
20
21#[cfg(test)]
22#[allow(clippy::unwrap_used, clippy::expect_used, clippy::float_cmp)]
23mod tests {
24 use super::*;
25
26 #[test]
31 fn h0_perf_01_tracer_creation() {
32 let tracer = Tracer::new();
33 assert!(!tracer.is_recording());
34 }
35
36 #[test]
37 fn h0_perf_02_tracer_with_config() {
38 let config = TraceConfig::default()
39 .with_sample_rate(500)
40 .with_memory_tracking(true);
41
42 let tracer = Tracer::with_config(config);
43 assert_eq!(tracer.config().sample_rate, 500);
44 assert!(tracer.config().capture_memory);
45 }
46
47 #[test]
52 fn h0_perf_03_span_creation() {
53 let mut tracer = Tracer::new();
54 tracer.start();
55
56 {
57 let _guard = tracer.span("test_span");
58 std::thread::sleep(std::time::Duration::from_millis(1));
59 }
60
61 let trace = tracer.stop();
62 assert!(trace.span_count() > 0);
63 }
64
65 #[test]
66 fn h0_perf_04_nested_spans() {
67 let mut tracer = Tracer::new();
68 tracer.start();
69
70 {
71 let _outer = tracer.span("outer");
72 {
73 let _inner = tracer.span("inner");
74 }
75 }
76
77 let trace = tracer.stop();
78 assert!(trace.span_count() >= 2);
79 }
80
81 #[test]
86 fn h0_perf_05_statistics() {
87 let stats = Statistics::from_values(&[1.0, 2.0, 3.0, 4.0, 5.0]);
88
89 assert!((stats.mean - 3.0).abs() < f64::EPSILON);
90 assert!((stats.min - 1.0).abs() < f64::EPSILON);
91 assert!((stats.max - 5.0).abs() < f64::EPSILON);
92 }
93
94 #[test]
95 fn h0_perf_06_frame_metrics() {
96 let metrics = FrameMetrics::new(16.67); assert!((metrics.frame_time_ms - 16.67).abs() < 0.01);
98 assert!(metrics.fps() > 59.0 && metrics.fps() < 61.0);
99 }
100
101 #[test]
106 fn h0_perf_07_chrome_trace_export() {
107 let mut tracer = Tracer::new();
108 tracer.start();
109 {
110 let _guard = tracer.span("test");
111 }
112 let trace = tracer.stop();
113
114 let chrome_trace = ChromeTrace::from_trace(&trace);
115 let json = chrome_trace.to_json();
116
117 assert!(json.contains("traceEvents"));
118 }
119
120 #[test]
121 fn h0_perf_08_ci_metrics_export() {
122 let mut tracer = Tracer::new();
123 tracer.start();
124 {
125 let _guard = tracer.span("test");
126 }
127 let trace = tracer.stop();
128
129 let ci_metrics = CiMetrics::from_trace(&trace);
130 let json = ci_metrics.to_json();
131
132 assert!(json.contains("span_count"));
133 }
134
135 #[test]
140 fn h0_perf_09_memory_metrics() {
141 let metrics = MemoryMetrics {
142 heap_used: 1024 * 1024,
143 heap_total: 2 * 1024 * 1024,
144 peak_usage: 1536 * 1024,
145 };
146
147 assert_eq!(metrics.usage_percent(), 50.0);
148 }
149}