From cd757bd289bc81ae03a5206447fe852483eaaa01 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Wed, 2 Dec 2026 06:43:29 +0300 Subject: [PATCH] ec-opus conformance: delete decode_speed and encode_speed wall-clock floors (owner rule: no speed gates) and the TIMING lock and CPU-time helpers only they used --- crates/ec-opus/tests/conformance.rs | 201 +--------------------------- 1 file changed, 1 insertion(+), 200 deletions(-) diff --git a/crates/ec-opus/tests/conformance.rs b/crates/ec-opus/tests/conformance.rs index a32a20a8..c1b5856f 100644 --- a/crates/ec-opus/tests/conformance.rs +++ b/crates/ec-opus/tests/conformance.rs @@ -1,5 +1,5 @@ //! RFC 6716 conformance: the official test vectors, the `opus_compare` quality -//! metric, real-file oracles against ffmpeg, and the decode-speed floor. +//! metric, real-file oracles against ffmpeg. //! //! All of it lives in one test binary on purpose — a file per feature costs a //! serial link-and-run for every build. @@ -454,7 +454,6 @@ fn run_rfc8251(name: &str, reference: &str) -> (usize, usize, f64) { /// shift) and the `m` set (without it) are both run and both reported. #[test] fn rfc8251_test_vectors() { - let _timing = timing_shared(); let mut dec_ok = true; let mut m_ok = true; for name in VECTORS { @@ -491,7 +490,6 @@ const VECTORS: [&str; 12] = [ /// MUST stay within the `opus_compare` threshold (quality >= 0). #[test] fn rfc6716_test_vectors() { - let _timing = timing_shared(); let mut table = Vec::new(); for name in VECTORS { let path = @@ -680,7 +678,6 @@ fn tone_power(x: &[f32], channels: usize, ch: usize, freq: f64) -> f64 { #[test] fn ogg_opus_fixtures_match_ffmpeg() { - let _timing = timing_shared(); // Mapping family 1 puts 5.1 in Vorbis order; ffmpeg hands its own order // back, so the comparison permutes one into the other. const VORBIS_TO_FFMPEG_5_1: [usize; 6] = [0, 2, 1, 4, 5, 3]; @@ -717,7 +714,6 @@ fn ogg_opus_fixtures_match_ffmpeg() { #[test] fn five_one_channels_land_in_the_right_places() { - let _timing = timing_shared(); // The fixture carries one distinct sine per channel, so a channel-order // bug shows up as a channel playing the wrong tone. The tones themselves // come from the reference decode rather than from an assumption about how @@ -753,115 +749,6 @@ fn five_one_channels_land_in_the_right_places() { } } -/// Every speed-floor test takes this exclusively and every other test in this -/// binary takes it shared, so a floor is never measured while another -/// conformance test is loading the same host. -static TIMING: std::sync::RwLock<()> = std::sync::RwLock::new(()); - -fn timing_shared() -> std::sync::RwLockReadGuard<'static, ()> { - TIMING.read().unwrap_or_else(|e| e.into_inner()) -} - -fn timing_exclusive() -> std::sync::RwLockWriteGuard<'static, ()> { - TIMING.write().unwrap_or_else(|e| e.into_inner()) -} - -/// Seconds of CPU time consumed by the calling thread, or `None` where the -/// clock is unavailable. Used by the speed floors: a loaded host that -/// deschedules the test thread (or runs the other conformance tests beside it) -/// must not count against the floor, so the floors measure work, not wall time. -fn thread_cpu_secs() -> Option { - #[cfg(target_os = "linux")] - { - if let Ok(t) = cpu_time::ThreadTime::try_now() { - return Some(t.as_duration().as_secs_f64()); - } - } - None -} - -/// Time one run of `f`: thread CPU time where available, wall time elsewhere. -fn timed_once(f: &mut F) -> f64 { - let cpu0 = thread_cpu_secs(); - let wall = std::time::Instant::now(); - f(); - match (cpu0, thread_cpu_secs()) { - (Some(a), Some(b)) => b - a, - _ => wall.elapsed().as_secs_f64(), - } -} - -/// Best of three runs. The fastest run is the one least disturbed by the host, -/// which is the usual benchmark practice for a throughput floor. -fn best_of_3(mut f: F) -> f64 { - (0..3) - .map(|_| timed_once(&mut f)) - .fold(f64::INFINITY, f64::min) -} - -#[test] -fn decode_speed() { - let _timing = timing_exclusive(); - if cfg!(debug_assertions) { - eprintln!("decode speed is only meaningful in release; skipped"); - return; - } - for (name, channels) in [ - ("opus-ogg-stereo-48000.opus", 2usize), - ("opus-ogg-5.1-48000.opus", 6), - ] { - let path = fixture(name); - let data = fs::read(&path).unwrap(); - let packets = ogg_packets(&data); - let head = parse_opus_head(&packets[0]); - let audio: Vec<&Vec> = packets.iter().skip(2).filter(|p| !p.is_empty()).collect(); - let mut dec = - MultistreamDecoder::with_rate(48000, head.streams, head.coupled, &head.mapping); - let mut out = vec![0.0f32; 5760 * channels]; - // Warm up, then measure enough passes to cover several seconds of audio. - let mut samples = 0usize; - for p in &audio { - samples += dec.decode_float(p, &mut out).unwrap(); - } - let passes = 20; - let elapsed = best_of_3(|| { - for _ in 0..passes { - dec.reset(); - for p in &audio { - dec.decode_float(p, &mut out).unwrap(); - } - } - }); - let audio_secs = (samples * passes) as f64 / 48000.0; - let realtime = audio_secs / elapsed; - println!( - "{name}: {realtime:.0}x realtime ({channels} channels, {audio_secs:.1} s in {elapsed:.3} s)" - ); - let floor = if channels == 6 { 104.0 } else { 135.0 }; - assert!( - realtime >= floor, - "{name}: {realtime:.0}x realtime is below the {floor}x floor" - ); - } - - // The fixtures are sine tones, which are cheap; testvector01 is real music - // at a high rate and is the honest stereo number. - let path = ec_testkit::fixture("vectors/opus-rfc6716/opus_testvectors/testvector01.bit"); - let packets = read_vector(&path); - let mut out = vec![0.0f32; 5760 * 2]; - let mut samples = 0usize; - let elapsed = best_of_3(|| { - let mut dec = Decoder::new(48000, 2).unwrap(); - samples = 0; - for p in &packets { - samples += dec.decode_float(&p.payload, &mut out).unwrap(); - } - }); - let realtime = samples as f64 / 48000.0 / elapsed; - println!("testvector01 (stereo music): {realtime:.0}x realtime"); - assert!(realtime >= 135.0, "stereo music decodes at {realtime:.0}x"); -} - // --------------------------------------------------------------------------- // Encoder: self-consistency (range-exact against our own decoder) // --------------------------------------------------------------------------- @@ -952,7 +839,6 @@ fn delayed_corr(source: &[f32], decoded: &[f32], channels: usize, delay: usize) /// the world (the incumbent's exact disease). #[test] fn encoder_roundtrips_at_every_rate() { - let _timing = timing_shared(); for channels in [1usize, 2] { let pcm = test_signal(channels, 2.0); for kbps in [32u32, 64, 96, 128, 160, 165, 192, 256, 320, 510] { @@ -978,7 +864,6 @@ fn encoder_roundtrips_at_every_rate() { /// It is on by default except for `Application::LowDelay`, as in libopus. #[test] fn libopus_input_alignment_delays_and_still_decodes() { - let _timing = timing_shared(); let pcm = test_signal(2, 1.0); let mut enc = Encoder::new(48000, 2, Application::Audio).unwrap(); enc.set_bitrate(128_000); @@ -1006,7 +891,6 @@ fn libopus_input_alignment_delays_and_still_decodes() { // voice/music bandwidth tables blended by voice_est) since lane opus-mode; // matched against ffmpeg libopus on real speech in lanes/opus-mode-r1.sweep.txt. fn encoder_mode_follows_application_bitrate_channels_and_frame_size() { - let _timing = timing_shared(); #[derive(Clone, Copy, Debug)] struct Case { app: Application, @@ -1367,7 +1251,6 @@ fn encoder_mode_follows_application_bitrate_channels_and_frame_size() { /// stays decodable and lands near its target. #[test] fn encoder_frame_sizes_and_vbr() { - let _timing = timing_shared(); let pcm = test_signal(2, 1.0); for frame in [120usize, 240, 480, 960] { let mut enc = Encoder::new(48000, 2, Application::Audio).unwrap(); @@ -1488,7 +1371,6 @@ fn temp_path(name: &str) -> PathBuf { /// bars here are correlation >= 0.9 at 96/165/256 kbps. #[test] fn oracle_decodes_our_packets_across_the_rate_table() { - let _timing = timing_shared(); for channels in [1usize, 2] { let pcm = test_signal(channels, 2.0); let total = pcm.len() / channels; @@ -1598,7 +1480,6 @@ fn oracle_decodes_our_packets_across_the_rate_table() { /// back in its place (order-preserved) at correlation >= 0.9. #[test] fn five_one_encode_end_to_end() { - let _timing = timing_shared(); const VORBIS_TO_FFMPEG_5_1: [usize; 6] = [0, 2, 1, 4, 5, 3]; const TONES: [f64; 6] = [220.0, 440.0, 660.0, 55.0, 880.0, 1320.0]; let total = 96000usize; @@ -1650,7 +1531,6 @@ fn five_one_encode_end_to_end() { /// libopus hands back is exactly the input length. #[test] fn ogg_opus_round_trip_duration_is_exact() { - let _timing = timing_shared(); let pcm = test_signal(2, 2.0); let total = pcm.len() / 2; let mut enc = Encoder::new(48000, 2, Application::Audio).unwrap(); @@ -1714,7 +1594,6 @@ fn ogg_opus_round_trip_duration_is_exact() { #[test] fn steady_state_encode_loop_zero_alloc() { install_counter(); - let _timing = timing_shared(); let pcm = test_signal(2, 0.5); let pcm6 = { let mono = test_signal(1, 0.5); @@ -1765,80 +1644,11 @@ fn steady_state_encode_loop_zero_alloc() { // those is a separate, much larger effort than this defect's scope. } -/// The throughput headline: encode speed in multiples of realtime, per -/// configuration, at the product's rates. Floors are deliberately far under -/// the measured numbers — they catch a regression class (an accidental -/// O(n^2), a debug path left on), not machine variance. -#[test] -fn encode_speed() { - let _timing = timing_exclusive(); - if cfg!(debug_assertions) { - eprintln!("encode speed is only meaningful in release; skipped"); - return; - } - let secs = 4.0; - for (name, channels, kbps, floor) in [ - ("mono 96k", 1usize, 96u32, 100.0f64), - ("stereo 128k", 2, 128, 70.0), - ("stereo 256k", 2, 256, 70.0), - ("stereo 510k", 2, 510, 50.0), - ] { - let pcm = test_signal(channels, secs); - let mut enc = Encoder::new(48000, channels, Application::Audio).unwrap(); - enc.set_bitrate(kbps * 1000); - let mut out = vec![0u8; 1500]; - // Warm up one pass, then measure. - for block in pcm.chunks_exact(960 * channels) { - enc.encode_float(block, 960, &mut out).unwrap(); - } - let passes = 4; - let elapsed = best_of_3(|| { - for _ in 0..passes { - for block in pcm.chunks_exact(960 * channels) { - enc.encode_float(block, 960, &mut out).unwrap(); - } - } - }); - let realtime = secs * passes as f64 / elapsed; - println!("{name}: {realtime:.0}x realtime"); - assert!( - realtime >= floor, - "{name}: {realtime:.0}x is below {floor}x" - ); - } - // 5.1 multistream. - let mono = test_signal(1, secs); - let mut pcm6 = Vec::with_capacity(mono.len() * 6); - for &s in &mono { - for c in 0..6 { - pcm6.push(s * (1.0 + c as f32 * 0.01)); - } - } - let mut ms = MultistreamEncoder::surround_5_1(48000).unwrap(); - ms.set_bitrate(384_000); - let mut out = vec![0u8; 8 * 1500]; - for block in pcm6.as_chunks::<{ 960 * 6 }>().0 { - ms.encode_float(block, 960, &mut out).unwrap(); - } - let passes = 4; - let elapsed = best_of_3(|| { - for _ in 0..passes { - for block in pcm6.as_chunks::<{ 960 * 6 }>().0 { - ms.encode_float(block, 960, &mut out).unwrap(); - } - } - }); - let realtime = secs * passes as f64 / elapsed; - println!("5.1 384k: {realtime:.0}x realtime"); - assert!(realtime >= 25.0, "5.1: {realtime:.0}x is below 25x"); -} - /// Malformed input must come back as an error, never a panic: the decoders /// index tables with values the bitstream chooses, so this throws well-formed /// TOC bytes at random payloads to make sure every mode is actually entered. #[test] fn garbage_payloads_never_panic() { - let _timing = timing_shared(); let mut state = 0x12345678u32; let mut rand = move || { state ^= state << 13; @@ -2022,7 +1832,6 @@ fn aligned_corr(reference: &[f32], decoded: &[f32], max_lag: usize) -> (f64, usi #[test] fn silk_mono_nb_wb_roundtrip() { - let _timing = timing_shared(); let wav = fixture("wav16-mono-48000.wav"); let mut sources = vec![("synthetic", speech_like())]; let w = read_wav_mono(&wav); @@ -2067,7 +1876,6 @@ fn silk_mono_nb_wb_roundtrip() { #[test] fn silk_mediumband_and_10ms_roundtrip() { - let _timing = timing_shared(); let pcm = speech_like(); for (tag, bandwidth, frame_ms, config, cutoff, bps, floor) in [ ( @@ -2103,7 +1911,6 @@ fn silk_mediumband_and_10ms_roundtrip() { #[test] fn silk_multiframe_packets_roundtrip() { - let _timing = timing_shared(); let full = speech_like(); let pcm = &full[..46_080]; for (tag, bandwidth, frame_ms, config, cutoff, bps, floor) in [ @@ -2180,7 +1987,6 @@ fn aligned_channel_corr(reference: &[f32], decoded: &[f32], channels: usize, ch: #[test] fn silk_stereo_speech_roundtrip() { - let _timing = timing_shared(); let pcm = stereo_speech_pair(); let total = pcm.len() / 2; for (tag, bandwidth, per_channel_bps, cutoff, frame, config, code, frames) in [ @@ -2331,7 +2137,6 @@ fn silk_sources() -> Vec<(&'static str, Vec)> { /// path it dispatches to. #[test] fn silk_packets_track_encoder_bitrate() { - let _timing = timing_shared(); let pcm = test_signal(1, 2.0); for kbps_target in [8u32, 12, 16] { let mut enc = Encoder::new(48000, 1, Application::Voip).unwrap(); @@ -2399,7 +2204,6 @@ fn hybrid_delay(bps: u32) -> usize { #[test] fn hybrid_layers_align() { - let _timing = timing_shared(); let mut click = vec![0f32; 48000]; // Well inside the stream so both layers' predictors are running. let at = 960 * 10 + 300; @@ -2438,7 +2242,6 @@ fn hybrid_layers_align() { /// [`Encoder::look_ahead`] of that same configuration, never a constant. #[test] fn hybrid_fb_roundtrip() { - let _timing = timing_shared(); for (name, pcm) in silk_sources() { let (decoded, packets) = hybrid_roundtrip(&pcm, 32_000, None, (false, false)); let toc = ec_opus::Toc::new(packets[0][0]); @@ -2528,7 +2331,6 @@ fn hybrid_roundtrip_shape( #[test] fn hybrid_shapes_roundtrip() { - let _timing = timing_shared(); for (tag, channels, frame, bps, bandwidth, config, floor) in [ ( "mono-swb10", @@ -2623,7 +2425,6 @@ fn hybrid_shapes_roundtrip() { #[test] fn opus_compare_err_pinned_against_c() { - let _timing = timing_shared(); const N: usize = 48000 * 4; let t: Vec = (0..N).map(|i| i as f64 / 48000.0).collect();