Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

test(editor): add an audio output idle and play-latency benchmark

Richie McIlroya1789e57e11b
diff --git a/crates/editor/Cargo.toml b/crates/editor/Cargo.toml
index 0ac88092f..e0bc091d4 100644
--- a/crates/editor/Cargo.toml
+++ b/crates/editor/Cargo.toml
@@ -18,6 +18,10 @@ path = "examples/playback-pipeline-benchmark.rs"
name = "editor-playback-benchmark"
path = "examples/editor-playback-benchmark.rs"
+[[example]]
+name = "audio-output-idle-benchmark"
+path = "examples/audio-output-idle-benchmark.rs"
+
[[example]]
name = "editor-startup-benchmark"
path = "examples/editor-startup-benchmark.rs"
diff --git a/crates/editor/examples/audio-output-idle-benchmark.rs b/crates/editor/examples/audio-output-idle-benchmark.rs
new file mode 100644
index 000000000..3c6ee1daf
--- /dev/null
+++ b/crates/editor/examples/audio-output-idle-benchmark.rs
@@ -0,0 +1,202 @@
+use std::{
+ sync::Arc,
+ time::{Duration, Instant},
+};
+
+use cap_audio::{AudioData, DecodedAudio};
+use cap_editor::{
+ AudioOutput, MusicTracks, PlaySpec, SegmentAudioTimingRepair, audio_segment_from_decoded,
+};
+use cap_project::ProjectConfiguration;
+use tokio::sync::watch;
+
+const CLIP_SECS: f64 = 4.0;
+const AUDIBLE_SECS: f64 = 0.3;
+
+fn usage() -> ! {
+ eprintln!(
+ "usage:\n audio-output-idle-benchmark idle <secs>\n audio-output-idle-benchmark latency <reps> <warm_gap_secs> <idle_gap_secs>\n audio-output-idle-benchmark cycles <count> <idle_gap_secs>\n audio-output-idle-benchmark presses <secs,secs,...>"
+ );
+ std::process::exit(2);
+}
+
+fn quiet_tone() -> Arc<AudioData> {
+ let sample_rate = AudioData::SAMPLE_RATE as f64;
+ let frames = (CLIP_SECS * sample_rate) as usize;
+ let mut samples = Vec::with_capacity(frames * 2);
+ for frame in 0..frames {
+ let value =
+ (std::f64::consts::TAU * 440.0 * frame as f64 / sample_rate).sin() as f32 * 0.02;
+ samples.push(value);
+ samples.push(value);
+ }
+ Arc::new(AudioData::from_raw_f32(samples, 2))
+}
+
+fn spec(tone: &Arc<AudioData>) -> PlaySpec {
+ let project = ProjectConfiguration::default();
+ let (_, playhead_rx) = watch::channel(0.0);
+ PlaySpec {
+ segments: vec![audio_segment_from_decoded(
+ None,
+ Some(Arc::new(DecodedAudio::from(tone.clone()))),
+ SegmentAudioTimingRepair::default(),
+ )],
+ music: MusicTracks::new(),
+ project,
+ duration_secs: CLIP_SECS,
+ start_playhead_secs: 0.0,
+ playhead_rx,
+ }
+}
+
+fn press(output: &AudioOutput, tone: &Arc<AudioData>) -> f64 {
+ let spec = spec(tone);
+ let started = Instant::now();
+ let generation = output.play(spec);
+ let elapsed_ms = started.elapsed().as_secs_f64() * 1000.0;
+ let Some(generation) = generation else {
+ eprintln!("play returned no audio");
+ std::process::exit(1);
+ };
+ std::thread::sleep(Duration::from_secs_f64(AUDIBLE_SECS));
+ output.stop_playback(generation);
+ elapsed_ms
+}
+
+fn median(values: &mut [f64]) -> f64 {
+ values.sort_by(f64::total_cmp);
+ values[values.len() / 2]
+}
+
+fn main() {
+ tracing_subscriber::fmt()
+ .with_writer(std::io::stderr)
+ .with_target(false)
+ .init();
+ let _ = ffmpeg::init();
+ let args: Vec<String> = std::env::args().collect();
+ match args.get(1).map(String::as_str) {
+ Some("idle") => {
+ let secs: f64 = args
+ .get(2)
+ .and_then(|v| v.parse().ok())
+ .unwrap_or_else(|| usage());
+ let output = AudioOutput::new();
+ output.prewarm();
+ std::thread::sleep(Duration::from_secs_f64(secs));
+ drop(output);
+ }
+ Some("latency") => {
+ let parse = |index: usize| -> f64 {
+ args.get(index)
+ .and_then(|v| v.parse().ok())
+ .unwrap_or_else(|| usage())
+ };
+ let reps = parse(2) as usize;
+ let warm_gap = Duration::from_secs_f64(parse(3));
+ let idle_gap = Duration::from_secs_f64(parse(4));
+ let tone = quiet_tone();
+ let mut cold = Vec::new();
+ let mut prewarmed = Vec::new();
+ let mut warm = Vec::new();
+ let mut after_idle = Vec::new();
+ for rep in 0..reps {
+ {
+ let output = AudioOutput::new();
+ cold.push(press(&output, &tone));
+ }
+ std::thread::sleep(Duration::from_millis(500));
+ let output = AudioOutput::new();
+ output.prewarm();
+ std::thread::sleep(Duration::from_secs(1));
+ prewarmed.push(press(&output, &tone));
+ std::thread::sleep(warm_gap);
+ warm.push(press(&output, &tone));
+ std::thread::sleep(idle_gap);
+ after_idle.push(press(&output, &tone));
+ println!(
+ "rep {rep}: cold {:.2} ms, prewarmed {:.2} ms, warm {:.2} ms, after_idle {:.2} ms",
+ cold[rep], prewarmed[rep], warm[rep], after_idle[rep]
+ );
+ }
+ for (name, values) in [
+ ("cold", &mut cold),
+ ("prewarmed", &mut prewarmed),
+ ("warm", &mut warm),
+ ("after_idle", &mut after_idle),
+ ] {
+ let min = values.iter().copied().fold(f64::INFINITY, f64::min);
+ let max = values.iter().copied().fold(0.0, f64::max);
+ println!(
+ "{name}: median {:.2} ms (min {min:.2}, max {max:.2}, n {})",
+ median(values),
+ values.len()
+ );
+ }
+ }
+ Some("cycles") => {
+ let parse = |index: usize| -> f64 {
+ args.get(index)
+ .and_then(|v| v.parse().ok())
+ .unwrap_or_else(|| usage())
+ };
+ let cycles = parse(2) as usize;
+ let idle_gap = Duration::from_secs_f64(parse(3));
+ let tone = quiet_tone();
+ let output = AudioOutput::new();
+ output.prewarm();
+ let mut direct = Vec::new();
+ let mut hinted = Vec::new();
+ for cycle in 0..cycles {
+ std::thread::sleep(idle_gap);
+ if cycle % 2 == 0 {
+ direct.push(press(&output, &tone));
+ } else {
+ output.prewarm();
+ std::thread::sleep(Duration::from_millis(100));
+ hinted.push(press(&output, &tone));
+ }
+ }
+ for (name, values) in [
+ ("after_idle", &mut direct),
+ ("after_idle_prewarm_100ms", &mut hinted),
+ ] {
+ if values.is_empty() {
+ continue;
+ }
+ let min = values.iter().copied().fold(f64::INFINITY, f64::min);
+ let max = values.iter().copied().fold(0.0, f64::max);
+ println!(
+ "{name}: median {:.2} ms (min {min:.2}, max {max:.2}, n {})",
+ median(values),
+ values.len()
+ );
+ }
+ }
+ Some("presses") => {
+ let times: Vec<f64> = args
+ .get(2)
+ .map(|list| {
+ list.split(',')
+ .filter_map(|v| v.trim().parse().ok())
+ .collect()
+ })
+ .unwrap_or_else(|| usage());
+ let tone = quiet_tone();
+ let started = Instant::now();
+ let output = AudioOutput::new();
+ output.prewarm();
+ for at in times {
+ let wait = Duration::from_secs_f64(at).saturating_sub(started.elapsed());
+ std::thread::sleep(wait);
+ let ms = press(&output, &tone);
+ println!(
+ "t={:.1}s press: audio consumed after {ms:.2} ms",
+ started.elapsed().as_secs_f64()
+ );
+ }
+ }
+ _ => usage(),
+ }
+}
diff --git a/crates/editor/examples/editor-playback-benchmark.rs b/crates/editor/examples/editor-playback-benchmark.rs
index 14305d6c6..6c6c70e04 100644
--- a/crates/editor/examples/editor-playback-benchmark.rs
+++ b/crates/editor/examples/editor-playback-benchmark.rs
@@ -583,7 +583,18 @@ async fn main() {
let mut summary = BenchmarkSummary::default();
let mut measured_elapsed = 0.0f64;
+ // `--press-gap <secs>` idles between presses, e.g. to exercise the audio
+ // output's idle pause before the next play.
+ let press_gap = arg_value(&args, "--press-gap")
+ .and_then(|s| s.parse::<f64>().ok())
+ .map(Duration::from_secs_f64);
+
for (press_idx, (press_start_frame, press_frames)) in presses.iter().copied().enumerate() {
+ if press_idx > 0
+ && let Some(gap) = press_gap
+ {
+ tokio::time::sleep(gap).await;
+ }
let playback = Playback {
renderer: renderer.clone(),
render_constants: render_constants.clone(),
diff --git a/crates/editor/src/lib.rs b/crates/editor/src/lib.rs
index 56025c826..612c25cb4 100644
--- a/crates/editor/src/lib.rs
+++ b/crates/editor/src/lib.rs
@@ -17,6 +17,7 @@ mod thumbnail;
pub use audio::{AudioRenderer, AudioSegment, MusicTracks};
pub use audio_output::{
AudioOutput, HEADLESS_BLOCK_FRAMES, HEADLESS_CHANNELS, HEADLESS_SAMPLE_RATE, HeadlessAudioTap,
+ PlaySpec,
};
pub use cap_audio::{
TranscriptionAudioSource, TranscriptionAudioTake, append_transcription_audio,