use std::time::{Duration, Instant};
pub struct FrameProfile {
samples: Vec<u32>,
capacity: usize,
next_report_at: Instant,
frame_count: u32,
report_interval: Duration,
}
impl Default for FrameProfile {
fn default() -> Self {
Self::new()
}
}
impl FrameProfile {
pub fn new() -> Self {
Self::with_capacity(1024, Duration::from_secs(1))
}
pub fn with_capacity(capacity: usize, report_interval: Duration) -> Self {
Self {
samples: Vec::with_capacity(capacity),
capacity,
next_report_at: Instant::now() + report_interval,
frame_count: 0,
report_interval,
}
}
pub fn record(&mut self, paint_us: u32) {
if self.samples.len() < self.capacity {
self.samples.push(paint_us);
}
self.frame_count += 1;
}
pub fn maybe_report(&mut self) -> Option<FrameReport> {
let now = Instant::now();
if now < self.next_report_at {
return None;
}
self.next_report_at = now + self.report_interval;
if self.samples.is_empty() {
return None;
}
let mut sorted: Vec<u32> = std::mem::take(&mut self.samples);
sorted.sort_unstable();
let n = sorted.len() as u32;
let total: u64 = sorted.iter().map(|&x| x as u64).sum();
let avg_us = (total / n as u64) as u32;
let min_us = *sorted.first().unwrap();
let max_us = *sorted.last().unwrap();
let p50_us = sorted[(n as usize * 50) / 100];
let p95_us = sorted[(n as usize * 95) / 100];
let p99_us = sorted[(n as usize * 99) / 100];
let fps = n as f32;
Some(FrameReport { frames: n, avg_us, min_us, max_us, p50_us, p95_us, p99_us, fps })
}
}
#[derive(Debug, Clone, Copy)]
pub struct FrameReport {
pub frames: u32,
pub avg_us: u32,
pub min_us: u32,
pub max_us: u32,
pub p50_us: u32,
pub p95_us: u32,
pub p99_us: u32,
pub fps: f32,
}
impl FrameReport {
pub fn log(&self) {
log::info!(
"[B3 frame profile] fps={:.0} avg={}us min={}us p50={}us p95={}us p99={}us max={}us (frames={})",
self.fps,
self.avg_us,
self.min_us,
self.p50_us,
self.p95_us,
self.p99_us,
self.max_us,
self.frames
);
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn profile_record_and_report() {
let mut p = FrameProfile::with_capacity(8, Duration::from_millis(50));
for _ in 0..5 {
p.record(100);
}
assert!(p.maybe_report().is_none());
std::thread::sleep(Duration::from_millis(60));
let r = p.maybe_report().expect("should report");
assert_eq!(r.frames, 5);
assert_eq!(r.min_us, 100);
assert_eq!(r.max_us, 100);
assert_eq!(r.avg_us, 100);
assert_eq!(r.p50_us, 100);
}
#[test]
fn profile_window_clears_each_report() {
let mut p = FrameProfile::with_capacity(8, Duration::from_millis(50));
for _ in 0..5 {
p.record(100);
}
std::thread::sleep(Duration::from_millis(60));
let r1 = p.maybe_report().expect("should report");
assert_eq!(r1.frames, 5, "第 1 窗口 5 帧");
for _ in 0..3 {
p.record(200);
}
std::thread::sleep(Duration::from_millis(60));
let r2 = p.maybe_report().expect("should report");
assert_eq!(r2.frames, 3, "第 2 窗口 3 帧(不是 5+3 累计)");
assert_eq!(r2.avg_us, 200);
assert_eq!(r2.min_us, 200);
assert_eq!(r2.max_us, 200);
}
#[test]
fn profile_percentile_correct() {
let mut p = FrameProfile::with_capacity(100, Duration::from_millis(50));
for i in 1..=100 {
p.record(i);
}
std::thread::sleep(Duration::from_millis(60));
let r = p.maybe_report().expect("should report");
assert_eq!(r.frames, 100);
assert_eq!(r.min_us, 1);
assert_eq!(r.max_us, 100);
assert!((r.avg_us as i64 - 50).unsigned_abs() < 2, "avg≈50, got {}", r.avg_us);
assert_eq!(r.p50_us, 51);
assert_eq!(r.p99_us, 100);
}
}