The decode/encode baseline, measured on real 4K films
Browse files at this commitcb2833f0e6b365fa40de21aeb5be3fcd351b021eParent: d381b07- feat
- the decode/encode baseline, measured on real 4K films
2 changed files +532 -0
- crates/engine/examples/bench.rs +425 -0
- scripts/bench.sh +107 -0
crates/engine/examples/bench.rsadded+425 -0
| @@ -0,0 +1,425 @@ | |||
| 1 | +//! The baseline every later decode/encode change is measured against: open, | ||
| 2 | +//! seek, scrub and export, timed on the real library rather than on a fixture. | ||
| 3 | +//! | ||
| 4 | +//! `cargo run --release -p engine --example bench -- <metric> <file> [args]` | ||
| 5 | +//! | ||
| 6 | +//! One metric per process, on purpose: a decoder that panics (the vendored | ||
| 7 | +//! HEVC parser does, on a 2160p remux) must cost one row and not the run, so | ||
| 8 | +//! `scripts/bench.sh` reads the exit status of each of these and writes the | ||
| 9 | +//! failure down as data. | ||
| 10 | +//! | ||
| 11 | +//! Every row is one TSV line -- `file, metric, unit, n, median, min, max, note` | ||
| 12 | +//! -- so the whole baseline is `cat`ted together and diffed later. | ||
| 13 | +//! | ||
| 14 | +//! **Cold and warm.** Cold means the file's pages are dropped from the page | ||
| 15 | +//! cache immediately before the measurement, with `posix_fadvise(DONTNEED)` on | ||
| 16 | +//! a fd of our own: no `drop_caches`, no root, and nothing else on the machine | ||
| 17 | +//! is evicted. It is advisory -- a page another process still holds mapped | ||
| 18 | +//! stays -- which is why the cold numbers are a *floor* on the cold cost and | ||
| 19 | +//! the run log records that. Warm means the same open repeated with the cache | ||
| 20 | +//! left as the previous one filled it. | ||
| 21 | +//! | ||
| 22 | +//! **Media files are read only here.** Nothing in this file opens anything for | ||
| 23 | +//! writing except the export destination it is handed. | ||
| 24 | +//! | ||
| 25 | +//! Env: `BENCH_TTFF_TIMEOUT` (s, default 180) how long a seek may wait for its | ||
| 26 | +//! first frame, `BENCH_EXPORT_CAP` (s, default 300) the wall clock any single | ||
| 27 | +//! export measurement may take, `BENCH_EXPORT_REPS` (default 5) how many times | ||
| 28 | +//! an export is repeated *if the cap leaves room* -- a 4K encode gets one | ||
| 29 | +//! capped rep, the small control gets five. | ||
| 30 | + | ||
| 31 | +use std::path::{Path, PathBuf}; | ||
| 32 | +use std::time::{Duration, Instant}; | ||
| 33 | + | ||
| 34 | +use engine::export::{ExportSettings, Format}; | ||
| 35 | +use engine::project::{Clip, LaneKind, Project, Source, Speed}; | ||
| 36 | + | ||
| 37 | +// `posix_fadvise(2)`, straight from libc, which std already links: dropping | ||
| 38 | +// this file's pages needs no crate and no privilege. | ||
| 39 | +unsafe extern "C" { | ||
| 40 | + fn posix_fadvise(fd: i32, offset: i64, len: i64, advice: i32) -> i32; | ||
| 41 | +} | ||
| 42 | +const POSIX_FADV_DONTNEED: i32 = 4; | ||
| 43 | + | ||
| 44 | +fn main() { | ||
| 45 | + let mut args = std::env::args().skip(1); | ||
| 46 | + let metric = args.next().unwrap_or_else(|| usage()); | ||
| 47 | + let path = PathBuf::from(args.next().unwrap_or_else(|| usage())); | ||
| 48 | + match metric.as_str() { | ||
| 49 | + "open" => open_bench(&path), | ||
| 50 | + "seek" => seek_bench(&path, arg_f64(args.next(), "seconds")), | ||
| 51 | + "scrub" => scrub_bench(&path), | ||
| 52 | + "export" => { | ||
| 53 | + let seat = args.next().unwrap_or_else(|| usage()); | ||
| 54 | + let out_dir = PathBuf::from(args.next().unwrap_or_else(|| usage())); | ||
| 55 | + export_bench(&path, &seat, &out_dir); | ||
| 56 | + } | ||
| 57 | + _ => usage(), | ||
| 58 | + } | ||
| 59 | +} | ||
| 60 | + | ||
| 61 | +fn usage() -> ! { | ||
| 62 | + eprintln!( | ||
| 63 | + "usage: bench open <file>\n bench seek <file> <secs>\n \ | ||
| 64 | + bench scrub <file>\n bench export <file> <h264sw|h264hw|av1|hevc> <out_dir>" | ||
| 65 | + ); | ||
| 66 | + std::process::exit(2) | ||
| 67 | +} | ||
| 68 | + | ||
| 69 | +fn arg_f64(arg: Option<String>, what: &str) -> f64 { | ||
| 70 | + arg.and_then(|s| s.parse().ok()) | ||
| 71 | + .unwrap_or_else(|| panic!("missing {what}")) | ||
| 72 | +} | ||
| 73 | + | ||
| 74 | +// ---------------------------------------------------------------- reporting | ||
| 75 | + | ||
| 76 | +/// One TSV row: the median is what a comparison uses, the min and max are what | ||
| 77 | +/// says whether the median means anything. | ||
| 78 | +fn row(file: &Path, metric: &str, unit: &str, samples: &[f64], note: &str) { | ||
| 79 | + let name = file.file_name().unwrap_or(file.as_os_str()).to_string_lossy(); | ||
| 80 | + if samples.is_empty() { | ||
| 81 | + println!("{name}\t{metric}\t{unit}\t0\t\t\t\t{note}"); | ||
| 82 | + return; | ||
| 83 | + } | ||
| 84 | + let mut sorted = samples.to_vec(); | ||
| 85 | + sorted.sort_by(f64::total_cmp); | ||
| 86 | + let median = if sorted.len() % 2 == 1 { | ||
| 87 | + sorted[sorted.len() / 2] | ||
| 88 | + } else { | ||
| 89 | + (sorted[sorted.len() / 2 - 1] + sorted[sorted.len() / 2]) / 2.0 | ||
| 90 | + }; | ||
| 91 | + println!( | ||
| 92 | + "{name}\t{metric}\t{unit}\t{}\t{median:.2}\t{:.2}\t{:.2}\t{note}", | ||
| 93 | + sorted.len(), | ||
| 94 | + sorted[0], | ||
| 95 | + sorted[sorted.len() - 1], | ||
| 96 | + ); | ||
| 97 | +} | ||
| 98 | + | ||
| 99 | +// ------------------------------------------------------------------- helpers | ||
| 100 | + | ||
| 101 | +/// Drops this file's page cache. Advisory: pages someone else holds survive, | ||
| 102 | +/// so a "cold" number here is the optimistic end of cold. | ||
| 103 | +fn evict(path: &Path) { | ||
| 104 | + use std::os::unix::io::AsRawFd; | ||
| 105 | + let file = std::fs::File::open(path).expect("open for evict"); | ||
| 106 | + let len = file.metadata().map_or(0, |m| m.len()); | ||
| 107 | + // SAFETY: a valid fd of ours, a length from its own metadata, and an | ||
| 108 | + // advice value that changes nothing but the kernel's cache bookkeeping. | ||
| 109 | + let rc = unsafe { posix_fadvise(file.as_raw_fd(), 0, len as i64, POSIX_FADV_DONTNEED) }; | ||
| 110 | + if rc != 0 { | ||
| 111 | + eprintln!("posix_fadvise: rc {rc} (measurement is warm, not cold)"); | ||
| 112 | + } | ||
| 113 | +} | ||
| 114 | + | ||
| 115 | +/// Threads this process holds right now -- the scrub metric that matters as | ||
| 116 | +/// much as the latency, a drag storm having peaked in the hundreds. | ||
| 117 | +fn threads() -> usize { | ||
| 118 | + std::fs::read_dir("/proc/self/task").map_or(0, Iterator::count) | ||
| 119 | +} | ||
| 120 | + | ||
| 121 | +fn ttff_timeout() -> Duration { | ||
| 122 | + Duration::from_secs_f64(env_f64("BENCH_TTFF_TIMEOUT", 180.0)) | ||
| 123 | +} | ||
| 124 | + | ||
| 125 | +fn env_f64(key: &str, default: f64) -> f64 { | ||
| 126 | + std::env::var(key) | ||
| 127 | + .ok() | ||
| 128 | + .and_then(|v| v.parse().ok()) | ||
| 129 | + .unwrap_or(default) | ||
| 130 | +} | ||
| 131 | + | ||
| 132 | +/// Seeks and waits for the picture that seek asked for: the number a user | ||
| 133 | +/// feels. The error side is the two ways no picture arrives, and both are a | ||
| 134 | +/// row rather than a crash: `worker died`, which is what a decoder panicking | ||
| 135 | +/// on its own thread looks like from here (the channel drops, the session runs | ||
| 136 | +/// out of clips, and that is end-of-stream a millisecond after a seek), and | ||
| 137 | +/// `timeout`, which is a decode still grinding when the clock ran out. | ||
| 138 | +/// | ||
| 139 | +/// `peak` collects the thread count seen while waiting, so the scrub's storm | ||
| 140 | +/// is measured by the same poll loop that measures its latency. | ||
| 141 | +fn seek_ttff( | ||
| 142 | + session: &mut engine::PlaybackSession, | ||
| 143 | + secs: f64, | ||
| 144 | + peak: &mut usize, | ||
| 145 | +) -> Result<f64, &'static str> { | ||
| 146 | + let deadline = Instant::now() + ttff_timeout(); | ||
| 147 | + let t = Instant::now(); | ||
| 148 | + session.seek(secs); | ||
| 149 | + loop { | ||
| 150 | + *peak = (*peak).max(threads()); | ||
| 151 | + if session.try_frame().is_some() { | ||
| 152 | + return Ok(t.elapsed().as_secs_f64() * 1000.0); | ||
| 153 | + } | ||
| 154 | + if session.is_eos() { | ||
| 155 | + return Err("worker died"); | ||
| 156 | + } | ||
| 157 | + if Instant::now() >= deadline { | ||
| 158 | + return Err("timeout"); | ||
| 159 | + } | ||
| 160 | + std::thread::sleep(Duration::from_millis(1)); | ||
| 161 | + } | ||
| 162 | +} | ||
| 163 | + | ||
| 164 | +// -------------------------------------------------------------------- open | ||
| 165 | + | ||
| 166 | +/// `Demuxer::open`, five times cold and five times warm. Matroska has no index | ||
| 167 | +/// here and this walks every cluster header of the whole file, which is why it | ||
| 168 | +/// is the first thing measured. | ||
| 169 | +fn open_bench(path: &Path) { | ||
| 170 | + let mut cold = Vec::new(); | ||
| 171 | + let mut warm = Vec::new(); | ||
| 172 | + let mut note = String::new(); | ||
| 173 | + for _ in 0..5 { | ||
| 174 | + evict(path); | ||
| 175 | + let t = Instant::now(); | ||
| 176 | + match engine::demux::Demuxer::open(path) { | ||
| 177 | + Ok((meta, _)) => { | ||
| 178 | + cold.push(t.elapsed().as_secs_f64() * 1000.0); | ||
| 179 | + if note.is_empty() { | ||
| 180 | + note = format!( | ||
| 181 | + "{}x{} {:.3}fps {} frames", | ||
| 182 | + meta.width, meta.height, meta.frame_rate, meta.frame_count | ||
| 183 | + ); | ||
| 184 | + } | ||
| 185 | + } | ||
| 186 | + Err(e) => { | ||
| 187 | + row(path, "open_cold", "ms", &[], &format!("FAIL({e})")); | ||
| 188 | + return; | ||
| 189 | + } | ||
| 190 | + } | ||
| 191 | + let t = Instant::now(); | ||
| 192 | + let _ = engine::demux::Demuxer::open(path).expect("warm open"); | ||
| 193 | + warm.push(t.elapsed().as_secs_f64() * 1000.0); | ||
| 194 | + } | ||
| 195 | + row(path, "open_cold", "ms", &cold, ¬e); | ||
| 196 | + row(path, "open_warm", "ms", &warm, ¬e); | ||
| 197 | +} | ||
| 198 | + | ||
| 199 | +// -------------------------------------------------------------------- seek | ||
| 200 | + | ||
| 201 | +/// First-frame latency for a seek to `secs`, five times, on a session that is | ||
| 202 | +/// already open -- the `open_worker_deferred` path (`decode.rs`), which is | ||
| 203 | +/// what every seek, clip boundary and export span goes through. | ||
| 204 | +/// | ||
| 205 | +/// The file is evicted once before the session is opened, so rep 1 is cold and | ||
| 206 | +/// the rest are as warm as the previous seek left them: the spread between min | ||
| 207 | +/// and max is that difference and is meant to be read. | ||
| 208 | +fn seek_bench(path: &Path, secs: f64) { | ||
| 209 | + evict(path); | ||
| 210 | + let mut session = match engine::PlaybackSession::open(path) { | ||
| 211 | + Ok(session) => session, | ||
| 212 | + Err(e) => { | ||
| 213 | + row(path, &format!("seek_ttff_{secs:.0}s"), "ms", &[], &format!("FAIL({e})")); | ||
| 214 | + return; | ||
| 215 | + } | ||
| 216 | + }; | ||
| 217 | + let duration = session.timeline_duration(); | ||
| 218 | + if secs > duration { | ||
| 219 | + row( | ||
| 220 | + path, | ||
| 221 | + &format!("seek_ttff_{secs:.0}s"), | ||
| 222 | + "ms", | ||
| 223 | + &[], | ||
| 224 | + &format!("SKIP(file is {duration:.0}s)"), | ||
| 225 | + ); | ||
| 226 | + return; | ||
| 227 | + } | ||
| 228 | + let mut samples = Vec::new(); | ||
| 229 | + let mut missed = Vec::new(); | ||
| 230 | + let mut peak = 0; | ||
| 231 | + for _ in 0..5 { | ||
| 232 | + match seek_ttff(&mut session, secs, &mut peak) { | ||
| 233 | + Ok(ms) => samples.push(ms), | ||
| 234 | + Err(why) => missed.push(why), | ||
| 235 | + } | ||
| 236 | + } | ||
| 237 | + let backend = session.decode_backend().label(); | ||
| 238 | + let note = if missed.is_empty() { | ||
| 239 | + format!("backend {backend}") | ||
| 240 | + } else { | ||
| 241 | + format!( | ||
| 242 | + "backend {backend}, {}/5 NO-FRAME({})", | ||
| 243 | + missed.len(), | ||
| 244 | + missed[0] | ||
| 245 | + ) | ||
| 246 | + }; | ||
| 247 | + row(path, &format!("seek_ttff_{secs:.0}s"), "ms", &samples, ¬e); | ||
| 248 | +} | ||
| 249 | + | ||
| 250 | +// ------------------------------------------------------------------- scrub | ||
| 251 | + | ||
| 252 | +/// Twenty scrub steps: the playhead dragged across a quarter of the film, one | ||
| 253 | +/// frame waited for at each stop. Two numbers come out -- the per-step | ||
| 254 | +/// first-frame latency, and the peak thread count, because every abandoned | ||
| 255 | +/// step leaves a decode worker inside an uncancellable open. | ||
| 256 | +/// | ||
| 257 | +/// Then the same twenty steps *without* waiting, which is the drag a user | ||
| 258 | +/// really makes: no latency to report (no frame is waited for), only the | ||
| 259 | +/// thread peak, which is where the hundreds came from. | ||
| 260 | +fn scrub_bench(path: &Path) { | ||
| 261 | + evict(path); | ||
| 262 | + let mut session = match engine::PlaybackSession::open(path) { | ||
| 263 | + Ok(session) => session, | ||
| 264 | + Err(e) => { | ||
| 265 | + row(path, "scrub_ttff", "ms", &[], &format!("FAIL({e})")); | ||
| 266 | + return; | ||
| 267 | + } | ||
| 268 | + }; | ||
| 269 | + let duration = session.timeline_duration(); | ||
| 270 | + let start = duration * 0.25; | ||
| 271 | + let step = (duration * 0.25) / 20.0; | ||
| 272 | + let mut samples = Vec::new(); | ||
| 273 | + let mut missed = Vec::new(); | ||
| 274 | + let mut peak = threads(); | ||
| 275 | + for i in 0..20 { | ||
| 276 | + match seek_ttff(&mut session, start + step * f64::from(i), &mut peak) { | ||
| 277 | + Ok(ms) => samples.push(ms), | ||
| 278 | + Err(why) => missed.push(why), | ||
| 279 | + } | ||
| 280 | + } | ||
| 281 | + let note = if missed.is_empty() { | ||
| 282 | + format!("backend {}", session.decode_backend().label()) | ||
| 283 | + } else { | ||
| 284 | + format!( | ||
| 285 | + "backend {}, {}/20 NO-FRAME({})", | ||
| 286 | + session.decode_backend().label(), | ||
| 287 | + missed.len(), | ||
| 288 | + missed[0] | ||
| 289 | + ) | ||
| 290 | + }; | ||
| 291 | + row(path, "scrub_ttff", "ms", &samples, ¬e); | ||
| 292 | + row(path, "scrub_threads_peak", "threads", &[peak as f64], "waited per step"); | ||
| 293 | + | ||
| 294 | + // The storm: seek, do not wait, seek again. | ||
| 295 | + let mut storm_peak = threads(); | ||
| 296 | + for i in 0..20 { | ||
| 297 | + session.seek(start + step * f64::from(i)); | ||
| 298 | + storm_peak = storm_peak.max(threads()); | ||
| 299 | + } | ||
| 300 | + row( | ||
| 301 | + path, | ||
| 302 | + "scrub_storm_threads_peak", | ||
| 303 | + "threads", | ||
| 304 | + &[storm_peak as f64], | ||
| 305 | + "20 seeks, no wait", | ||
| 306 | + ); | ||
| 307 | +} | ||
| 308 | + | ||
| 309 | +// ------------------------------------------------------------------ export | ||
| 310 | + | ||
| 311 | +/// Export throughput over a fixed 60 s span, one encoder seat per run. | ||
| 312 | +/// | ||
| 313 | +/// The span is video only: what is being compared is the picture encoder, and | ||
| 314 | +/// a sound track that refuses (a 7.1 Opus film does, at the app's door) would | ||
| 315 | +/// otherwise take the row with it. The span starts a tenth of the way in -- | ||
| 316 | +/// not on the black of a title card, which encodes at a rate no film sustains. | ||
| 317 | +/// | ||
| 318 | +/// The rate is frames the worker reported written over wall seconds, so a run | ||
| 319 | +/// stopped at the cap still reports a rate; the row says `CAPPED` when it was. | ||
| 320 | +fn export_bench(path: &Path, seat: &str, out_dir: &Path) { | ||
| 321 | + let (format, force_sw) = match seat { | ||
| 322 | + "h264sw" => (Format::Mp4, true), | ||
| 323 | + "h264hw" => (Format::Mp4, false), | ||
| 324 | + "av1" => (Format::Av1, true), | ||
| 325 | + "hevc" => (Format::Hevc, true), | ||
| 326 | + _ => usage(), | ||
| 327 | + }; | ||
| 328 | + let metric = format!("export_fps_{seat}"); | ||
| 329 | + let (meta, _) = match engine::demux::Demuxer::open(path) { | ||
| 330 | + Ok(opened) => opened, | ||
| 331 | + Err(e) => { | ||
| 332 | + row(path, &metric, "fps", &[], &format!("FAIL(open: {e})")); | ||
| 333 | + return; | ||
| 334 | + } | ||
| 335 | + }; | ||
| 336 | + let span = ((meta.frame_rate * 60.0) as u32).min(meta.frame_count.max(1)); | ||
| 337 | + let in_frame = (meta.frame_count / 10).min(meta.frame_count.saturating_sub(span)); | ||
| 338 | + let clip = Clip { | ||
| 339 | + start: 0, | ||
| 340 | + in_frame, | ||
| 341 | + out_frame: in_frame + span, | ||
| 342 | + source: 0, | ||
| 343 | + link: None, | ||
| 344 | + eq: None, | ||
| 345 | + color: None, | ||
| 346 | + fit: engine::scale::FitPolicy::default(), | ||
| 347 | + speed: Speed::NORMAL, | ||
| 348 | + }; | ||
| 349 | + let project = match Project::from_parts( | ||
| 350 | + vec![Source::new(path, 0)], | ||
| 351 | + vec![(LaneKind::Video, vec![clip])], | ||
| 352 | + Vec::new(), | ||
| 353 | + Vec::new(), | ||
| 354 | + ) { | ||
| 355 | + Ok(project) => project, | ||
| 356 | + Err(e) => { | ||
| 357 | + row(path, &metric, "fps", &[], &format!("FAIL(project: {e})")); | ||
| 358 | + return; | ||
| 359 | + } | ||
| 360 | + }; | ||
| 361 | + let settings = ExportSettings { | ||
| 362 | + force_sw, | ||
| 363 | + format, | ||
| 364 | + ..ExportSettings::default() | ||
| 365 | + }; | ||
| 366 | + let out = out_dir.join(format!("bench_{seat}.{}", format.ext())); | ||
| 367 | + let cap = Duration::from_secs_f64(env_f64("BENCH_EXPORT_CAP", 300.0)); | ||
| 368 | + let reps = env_f64("BENCH_EXPORT_REPS", 5.0) as usize; | ||
| 369 | + | ||
| 370 | + let mut samples = Vec::new(); | ||
| 371 | + let mut notes = Vec::new(); | ||
| 372 | + let budget = Instant::now(); | ||
| 373 | + for _ in 0..reps.max(1) { | ||
| 374 | + let handle = engine::export::start(project.clone(), meta, &out, &settings); | ||
| 375 | + let t = Instant::now(); | ||
| 376 | + let mut capped = false; | ||
| 377 | + while !handle.is_finished() { | ||
| 378 | + if t.elapsed() >= cap { | ||
| 379 | + capped = true; | ||
| 380 | + handle.cancel(); | ||
| 381 | + break; | ||
| 382 | + } | ||
| 383 | + std::thread::sleep(Duration::from_millis(200)); | ||
| 384 | + } | ||
| 385 | + // A cancel is answered at the worker's next checkpoint; the rate is | ||
| 386 | + // read from the progress at the moment the clock stopped either way. | ||
| 387 | + let progress = f64::from(handle.progress()); | ||
| 388 | + let elapsed = t.elapsed().as_secs_f64(); | ||
| 389 | + let done = Instant::now() + Duration::from_secs(60); | ||
| 390 | + while !handle.is_finished() && Instant::now() < done { | ||
| 391 | + std::thread::sleep(Duration::from_millis(100)); | ||
| 392 | + } | ||
| 393 | + let seats = handle.encoders().unwrap_or_else(|| "?".into()); | ||
| 394 | + let outcome = handle.result(); | ||
| 395 | + let _ = std::fs::remove_file(&out); | ||
| 396 | + if capped { | ||
| 397 | + samples.push(progress * f64::from(span) / elapsed); | ||
| 398 | + notes.push(format!("CAPPED at {:.0}s, {:.0}% done, {seats}", elapsed, progress * 100.0)); | ||
| 399 | + break; | ||
| 400 | + } | ||
| 401 | + match outcome { | ||
| 402 | + Some(Ok(())) => { | ||
| 403 | + samples.push(f64::from(span) / elapsed); | ||
| 404 | + notes.push(seats); | ||
| 405 | + } | ||
| 406 | + Some(Err(e)) => { | ||
| 407 | + notes.push(format!("FAIL({e})")); | ||
| 408 | + break; | ||
| 409 | + } | ||
| 410 | + // Finished without an outcome cannot happen (the worker settles | ||
| 411 | + // before it flips the flag); a wait that ran out of its own minute | ||
| 412 | + // does, and it is the same "no number" either way. | ||
| 413 | + None => { | ||
| 414 | + notes.push("FAIL(no outcome inside 60 s of the cap)".into()); | ||
| 415 | + break; | ||
| 416 | + } | ||
| 417 | + } | ||
| 418 | + if budget.elapsed() >= cap { | ||
| 419 | + notes.push(format!("{} rep(s) inside the {:.0}s budget", samples.len(), cap.as_secs_f64())); | ||
| 420 | + break; | ||
| 421 | + } | ||
| 422 | + } | ||
| 423 | + notes.dedup(); | ||
| 424 | + row(path, &metric, "fps", &samples, ¬es.join("; ")); | ||
| 425 | +} | ||
scripts/bench.shadded+107 -0
| @@ -0,0 +1,107 @@ | |||
| 1 | +#!/usr/bin/env bash | ||
| 2 | +# The decode/encode baseline: runs `examples/bench` over a list of real files | ||
| 3 | +# and writes one TSV of medians. Every later codec change is compared against | ||
| 4 | +# the file this produces, so it is meant to be run twice -- before and after -- | ||
| 5 | +# on an otherwise idle machine. | ||
| 6 | +# | ||
| 7 | +# scripts/bench.sh <out_dir> [file ...] | ||
| 8 | +# | ||
| 9 | +# With no files it reads them from $BENCH_LIST (one path per line, `#` comments | ||
| 10 | +# allowed), and failing that from assets/test_av.mp4. The list lives *outside* | ||
| 11 | +# this repo on purpose: what is measured here is a personal media library and | ||
| 12 | +# its file names are not this project's to record. | ||
| 13 | +# | ||
| 14 | +# Output in <out_dir>: baseline.tsv (the data) and bench.log (every run's | ||
| 15 | +# stderr, decoder panics included). | ||
| 16 | +# | ||
| 17 | +# Methodology, so a rerun means the same thing: | ||
| 18 | +# * cold = the file's pages dropped with posix_fadvise(DONTNEED) immediately | ||
| 19 | +# before the measurement -- no root, nothing else on the machine evicted, | ||
| 20 | +# and advisory, so it is the optimistic end of cold. | ||
| 21 | +# * warm = the same call repeated straight after, cache as the cold run left it. | ||
| 22 | +# * medians of 5 for open and seek, 20 steps for a scrub, and for export as | ||
| 23 | +# many reps as $BENCH_EXPORT_CAP seconds leave room for (at least one, | ||
| 24 | +# which is then marked CAPPED if it did not finish). | ||
| 25 | +# * every metric runs as its own process: a decoder that panics costs one | ||
| 26 | +# row, not the run. | ||
| 27 | +# * the media files are opened read only; only <out_dir> is written to. | ||
| 28 | +set -uo pipefail | ||
| 29 | +cd "$(dirname "$0")/.." | ||
| 30 | + | ||
| 31 | +OUT_DIR=${1:-} | ||
| 32 | +if [ -z "$OUT_DIR" ]; then | ||
| 33 | + echo "usage: scripts/bench.sh <out_dir> [file ...]" >&2 | ||
| 34 | + exit 2 | ||
| 35 | +fi | ||
| 36 | +shift | ||
| 37 | +mkdir -p "$OUT_DIR" | ||
| 38 | +TSV="$OUT_DIR/baseline.tsv" | ||
| 39 | +LOG="$OUT_DIR/bench.log" | ||
| 40 | + | ||
| 41 | +# Absolute, because $CARGO_TARGET_DIR may already be one and the plugin path | ||
| 42 | +# below must not become "$PWD/$HOME/...". | ||
| 43 | +TARGET=$(realpath -m "${CARGO_TARGET_DIR:-target}") | ||
| 44 | +cargo build --release -p engine -p engine-hw --example bench || exit 1 | ||
| 45 | +BENCH="$TARGET/release/examples/bench" | ||
| 46 | +# The plugin is looked for beside the running executable, and an example binary | ||
| 47 | +# lives one directory below the plugin: without this, hardware decode and | ||
| 48 | +# hardware encode silently fall back to software and the baseline is a lie. | ||
| 49 | +# Measured on the control file: seek to 600 s is 23 ms on the plugin and | ||
| 50 | +# 1359 ms without it, and every export seat reads "SW". | ||
| 51 | +export LD_LIBRARY_PATH="$TARGET/release${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}" | ||
| 52 | + | ||
| 53 | +files=("$@") | ||
| 54 | +if [ ${#files[@]} -eq 0 ] && [ -n "${BENCH_LIST:-}" ]; then | ||
| 55 | + while IFS= read -r line; do | ||
| 56 | + [ -z "$line" ] && continue | ||
| 57 | + case "$line" in \#*) continue ;; esac | ||
| 58 | + files+=("$line") | ||
| 59 | + done <"$BENCH_LIST" | ||
| 60 | +fi | ||
| 61 | +[ ${#files[@]} -eq 0 ] && files=(assets/test_av.mp4) | ||
| 62 | + | ||
| 63 | +{ | ||
| 64 | + echo "# bench run $(date -Is)" | ||
| 65 | + echo "# rev $(git rev-parse --short HEAD 2>/dev/null || echo '?')" | ||
| 66 | + echo "# $(uname -sr), $(nproc) cpus, $(free -g | awk '/^Mem:/ {print $2 " GiB ram"}')" | ||
| 67 | + echo "# caps: export ${BENCH_EXPORT_CAP:-300}s/${BENCH_EXPORT_REPS:-5} reps, ttff ${BENCH_TTFF_TIMEOUT:-180}s" | ||
| 68 | +} | tee -a "$LOG" | ||
| 69 | +printf 'file\tmetric\tunit\tn\tmedian\tmin\tmax\tnote\n' >"$TSV" | ||
| 70 | + | ||
| 71 | +# One metric, one process, one row -- even when the process dies. | ||
| 72 | +run() { # <timeout_s> <label> <file> <metric> [args...] | ||
| 73 | + local limit=$1 label=$2 file=$3 metric=$4 | ||
| 74 | + shift 4 | ||
| 75 | + local name=${file##*/} out rc | ||
| 76 | + echo "== $(date +%T) $metric ${*:-} $name" | tee -a "$LOG" >&2 | ||
| 77 | + out=$(timeout -k 30 "$limit" "$BENCH" "$metric" "$file" "$@" 2>>"$LOG") | ||
| 78 | + rc=$? | ||
| 79 | + [ -n "$out" ] && printf '%s\n' "$out" >>"$TSV" | ||
| 80 | + if [ $rc -ne 0 ] && [ -z "$out" ]; then | ||
| 81 | + local why="exit $rc" | ||
| 82 | + [ $rc -eq 124 ] && why="timeout after ${limit}s" | ||
| 83 | + [ $rc -ge 128 ] && why="signal $((rc - 128)) (panic/abort — see bench.log)" | ||
| 84 | + printf '%s\t%s\t\t0\t\t\t\tFAIL(%s)\n' "$name" "$label" "$why" >>"$TSV" | ||
| 85 | + fi | ||
| 86 | +} | ||
| 87 | + | ||
| 88 | +export_cap=${BENCH_EXPORT_CAP:-300} | ||
| 89 | +export_limit=$((export_cap * ${BENCH_EXPORT_REPS:-5} + 600)) | ||
| 90 | + | ||
| 91 | +for file in "${files[@]}"; do | ||
| 92 | + if [ ! -f "$file" ]; then | ||
| 93 | + printf '%s\tMISSING\t\t0\t\t\t\tFAIL(no such file)\n' "${file##*/}" >>"$TSV" | ||
| 94 | + continue | ||
| 95 | + fi | ||
| 96 | + run 3600 open "$file" open | ||
| 97 | + for t in 30 600 5400; do | ||
| 98 | + run 1800 "seek_ttff_${t}s" "$file" seek "$t" | ||
| 99 | + done | ||
| 100 | + run 3600 scrub "$file" scrub | ||
| 101 | + for seat in h264sw h264hw av1 hevc; do | ||
| 102 | + run "$export_limit" "export_fps_$seat" "$file" export "$seat" "$OUT_DIR" | ||
| 103 | + done | ||
| 104 | +done | ||
| 105 | + | ||
| 106 | +echo "# done $(date -Is)" | tee -a "$LOG" | ||
| 107 | +column -t -s $'\t' "$TSV" | ||