Add a throwaway profiling example for the encode bench shape
Browse files at this commitdfbf8b335c7787c1395c073201b00c029ce23afeParent: 9fab3bf- chore
- add a throwaway profiling example for the encode bench shape
1 changed file +33 -0
crates/ec-h264/examples/bench_encode.rsadded+33 -0
| @@ -0,0 +1,33 @@ | |||
| 1 | +//! Throwaway profiling harness for lane-h264perf: repeats the bench's own | ||
| 2 | +//! 640x360x30-frame encode loop N times so a perf sample has enough hits to | ||
| 3 | +//! be useful. Not wired into any product surface; delete before merge if | ||
| 4 | +//! still present. | ||
| 5 | +use ec_h264::{Encoder, EncoderConfig, PictureView}; | ||
| 6 | +use std::time::Instant; | ||
| 7 | + | ||
| 8 | +fn main() { | ||
| 9 | + let path = std::env::args().nth(1).expect("path to raw yuv420p"); | ||
| 10 | + let reps: u32 = std::env::args().nth(2).and_then(|s| s.parse().ok()).unwrap_or(20); | ||
| 11 | + let (w, h) = (640u32, 360u32); | ||
| 12 | + let data = std::fs::read(&path).expect("read raw yuv"); | ||
| 13 | + let frame_len = (w * h + 2 * (w.div_ceil(2) * h.div_ceil(2))) as usize; | ||
| 14 | + let n = data.len() / frame_len; | ||
| 15 | + let start = Instant::now(); | ||
| 16 | + for _ in 0..reps { | ||
| 17 | + let mut cfg = EncoderConfig::new(w, h); | ||
| 18 | + cfg.threads = 1; | ||
| 19 | + let mut enc = Encoder::new(cfg).expect("h264 encoder"); | ||
| 20 | + for frame in data.chunks_exact(frame_len).take(n) { | ||
| 21 | + let (y, rest) = frame.split_at((w * h) as usize); | ||
| 22 | + let (u, v) = rest.split_at(rest.len() / 2); | ||
| 23 | + let view = PictureView::i420(w, h, y, u, v); | ||
| 24 | + enc.encode(&view).expect("h264 encode"); | ||
| 25 | + } | ||
| 26 | + } | ||
| 27 | + let wall = start.elapsed(); | ||
| 28 | + eprintln!( | ||
| 29 | + "{reps} reps x {n} frames in {:.3}s ({:.2} ms/rep)", | ||
| 30 | + wall.as_secs_f64(), | ||
| 31 | + wall.as_secs_f64() * 1000.0 / f64::from(reps) | ||
| 32 | + ); | ||
| 33 | +} | ||