1use std::time::{Duration, Instant};
20
21pub struct FrameProfile {
27 samples: Vec<u32>,
29 capacity: usize,
31 next_report_at: Instant,
33 frame_count: u32,
35 report_interval: Duration,
37}
38
39impl Default for FrameProfile {
40 fn default() -> Self {
41 Self::new()
42 }
43}
44
45impl FrameProfile {
46 pub fn new() -> Self {
47 Self::with_capacity(1024, Duration::from_secs(1))
48 }
49
50 pub fn with_capacity(capacity: usize, report_interval: Duration) -> Self {
51 Self {
52 samples: Vec::with_capacity(capacity),
53 capacity,
54 next_report_at: Instant::now() + report_interval,
55 frame_count: 0,
56 report_interval,
57 }
58 }
59
60 pub fn record(&mut self, paint_us: u32) {
62 if self.samples.len() < self.capacity {
63 self.samples.push(paint_us);
64 }
65 self.frame_count += 1;
68 }
69
70 pub fn maybe_report(&mut self) -> Option<FrameReport> {
74 let now = Instant::now();
75 if now < self.next_report_at {
76 return None;
77 }
78 self.next_report_at = now + self.report_interval;
80 if self.samples.is_empty() {
81 return None;
82 }
83
84 let mut sorted: Vec<u32> = std::mem::take(&mut self.samples);
87 sorted.sort_unstable();
88
89 let n = sorted.len() as u32;
90 let total: u64 = sorted.iter().map(|&x| x as u64).sum();
91 let avg_us = (total / n as u64) as u32;
92 let min_us = *sorted.first().unwrap();
93 let max_us = *sorted.last().unwrap();
94 let p50_us = sorted[(n as usize * 50) / 100];
95 let p95_us = sorted[(n as usize * 95) / 100];
96 let p99_us = sorted[(n as usize * 99) / 100];
97
98 let fps = n as f32;
100
101 Some(FrameReport { frames: n, avg_us, min_us, max_us, p50_us, p95_us, p99_us, fps })
102 }
103}
104
105#[derive(Debug, Clone, Copy)]
107pub struct FrameReport {
108 pub frames: u32,
110 pub avg_us: u32,
112 pub min_us: u32,
113 pub max_us: u32,
114 pub p50_us: u32,
115 pub p95_us: u32,
116 pub p99_us: u32,
117 pub fps: f32,
119}
120
121impl FrameReport {
122 pub fn log(&self) {
123 log::info!(
124 "[B3 frame profile] fps={:.0} avg={}us min={}us p50={}us p95={}us p99={}us max={}us (frames={})",
125 self.fps,
126 self.avg_us,
127 self.min_us,
128 self.p50_us,
129 self.p95_us,
130 self.p99_us,
131 self.max_us,
132 self.frames
133 );
134 }
135}
136
137#[cfg(test)]
138mod tests {
139 use super::*;
140
141 #[test]
142 fn profile_record_and_report() {
143 let mut p = FrameProfile::with_capacity(8, Duration::from_millis(50));
144 for _ in 0..5 {
146 p.record(100);
147 }
148 assert!(p.maybe_report().is_none());
150 std::thread::sleep(Duration::from_millis(60));
152 let r = p.maybe_report().expect("should report");
153 assert_eq!(r.frames, 5);
154 assert_eq!(r.min_us, 100);
155 assert_eq!(r.max_us, 100);
156 assert_eq!(r.avg_us, 100);
157 assert_eq!(r.p50_us, 100);
158 }
159
160 #[test]
161 fn profile_window_clears_each_report() {
162 let mut p = FrameProfile::with_capacity(8, Duration::from_millis(50));
166 for _ in 0..5 {
167 p.record(100);
168 }
169 std::thread::sleep(Duration::from_millis(60));
170 let r1 = p.maybe_report().expect("should report");
171 assert_eq!(r1.frames, 5, "第 1 窗口 5 帧");
172
173 for _ in 0..3 {
175 p.record(200);
176 }
177 std::thread::sleep(Duration::from_millis(60));
178 let r2 = p.maybe_report().expect("should report");
179 assert_eq!(r2.frames, 3, "第 2 窗口 3 帧(不是 5+3 累计)");
180 assert_eq!(r2.avg_us, 200);
181 assert_eq!(r2.min_us, 200);
182 assert_eq!(r2.max_us, 200);
183 }
184
185 #[test]
186 fn profile_percentile_correct() {
187 let mut p = FrameProfile::with_capacity(100, Duration::from_millis(50));
188 for i in 1..=100 {
190 p.record(i);
191 }
192 std::thread::sleep(Duration::from_millis(60));
193 let r = p.maybe_report().expect("should report");
194 assert_eq!(r.frames, 100);
195 assert_eq!(r.min_us, 1);
196 assert_eq!(r.max_us, 100);
197 assert!((r.avg_us as i64 - 50).unsigned_abs() < 2, "avg≈50, got {}", r.avg_us);
198 assert_eq!(r.p50_us, 51);
200 assert_eq!(r.p99_us, 100);
202 }
203}