forge
Explore
Log in

Don't you dare go Hollow

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

Browse files at this commit

cd757bd289bc81ae03a5206447fe852483eaaa01Ahmet Kaan GümüşParent: be88845
crates/ec-opus/tests/conformance.rsmodified+1 -200
@@ -1,5 +1,5 @@
1 //! RFC 6716 conformance: the official test vectors, the `opus_compare` quality1 //! RFC 6716 conformance: the official test vectors, the `opus_compare` quality
2-//! metric, real-file oracles against ffmpeg, and the decode-speed floor.2+//! metric, real-file oracles against ffmpeg.
3 //!3 //!
4 //! All of it lives in one test binary on purpose — a file per feature costs a4 //! All of it lives in one test binary on purpose — a file per feature costs a
5 //! serial link-and-run for every build.5 //! serial link-and-run for every build.
@@ -454,7 +454,6 @@ fn run_rfc8251(name: &str, reference: &str) -> (usize, usize, f64) {
454 /// shift) and the `m` set (without it) are both run and both reported.454 /// shift) and the `m` set (without it) are both run and both reported.
455 #[test]455 #[test]
456 fn rfc8251_test_vectors() {456 fn rfc8251_test_vectors() {
457- let _timing = timing_shared();
458 let mut dec_ok = true;457 let mut dec_ok = true;
459 let mut m_ok = true;458 let mut m_ok = true;
460 for name in VECTORS {459 for name in VECTORS {
@@ -491,7 +490,6 @@ const VECTORS: [&str; 12] = [
491 /// MUST stay within the `opus_compare` threshold (quality >= 0).490 /// MUST stay within the `opus_compare` threshold (quality >= 0).
492 #[test]491 #[test]
493 fn rfc6716_test_vectors() {492 fn rfc6716_test_vectors() {
494- let _timing = timing_shared();
495 let mut table = Vec::new();493 let mut table = Vec::new();
496 for name in VECTORS {494 for name in VECTORS {
497 let path =495 let path =
@@ -680,7 +678,6 @@ fn tone_power(x: &[f32], channels: usize, ch: usize, freq: f64) -> f64 {
680 678
681 #[test]679 #[test]
682 fn ogg_opus_fixtures_match_ffmpeg() {680 fn ogg_opus_fixtures_match_ffmpeg() {
683- let _timing = timing_shared();
684 // Mapping family 1 puts 5.1 in Vorbis order; ffmpeg hands its own order681 // Mapping family 1 puts 5.1 in Vorbis order; ffmpeg hands its own order
685 // back, so the comparison permutes one into the other.682 // back, so the comparison permutes one into the other.
686 const VORBIS_TO_FFMPEG_5_1: [usize; 6] = [0, 2, 1, 4, 5, 3];683 const VORBIS_TO_FFMPEG_5_1: [usize; 6] = [0, 2, 1, 4, 5, 3];
@@ -717,7 +714,6 @@ fn ogg_opus_fixtures_match_ffmpeg() {
717 714
718 #[test]715 #[test]
719 fn five_one_channels_land_in_the_right_places() {716 fn five_one_channels_land_in_the_right_places() {
720- let _timing = timing_shared();
721 // The fixture carries one distinct sine per channel, so a channel-order717 // The fixture carries one distinct sine per channel, so a channel-order
722 // bug shows up as a channel playing the wrong tone. The tones themselves718 // bug shows up as a channel playing the wrong tone. The tones themselves
723 // come from the reference decode rather than from an assumption about how719 // 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() {
753 }749 }
754 }750 }
755 751
756-/// Every speed-floor test takes this exclusively and every other test in this
757-/// binary takes it shared, so a floor is never measured while another
758-/// conformance test is loading the same host.
759-static TIMING: std::sync::RwLock<()> = std::sync::RwLock::new(());
760-
761-fn timing_shared() -> std::sync::RwLockReadGuard<'static, ()> {
762- TIMING.read().unwrap_or_else(|e| e.into_inner())
763-}
764-
765-fn timing_exclusive() -> std::sync::RwLockWriteGuard<'static, ()> {
766- TIMING.write().unwrap_or_else(|e| e.into_inner())
767-}
768-
769-/// Seconds of CPU time consumed by the calling thread, or `None` where the
770-/// clock is unavailable. Used by the speed floors: a loaded host that
771-/// deschedules the test thread (or runs the other conformance tests beside it)
772-/// must not count against the floor, so the floors measure work, not wall time.
773-fn thread_cpu_secs() -> Option<f64> {
774- #[cfg(target_os = "linux")]
775- {
776- if let Ok(t) = cpu_time::ThreadTime::try_now() {
777- return Some(t.as_duration().as_secs_f64());
778- }
779- }
780- None
781-}
782-
783-/// Time one run of `f`: thread CPU time where available, wall time elsewhere.
784-fn timed_once<F: FnMut()>(f: &mut F) -> f64 {
785- let cpu0 = thread_cpu_secs();
786- let wall = std::time::Instant::now();
787- f();
788- match (cpu0, thread_cpu_secs()) {
789- (Some(a), Some(b)) => b - a,
790- _ => wall.elapsed().as_secs_f64(),
791- }
792-}
793-
794-/// Best of three runs. The fastest run is the one least disturbed by the host,
795-/// which is the usual benchmark practice for a throughput floor.
796-fn best_of_3<F: FnMut()>(mut f: F) -> f64 {
797- (0..3)
798- .map(|_| timed_once(&mut f))
799- .fold(f64::INFINITY, f64::min)
800-}
801-
802-#[test]
803-fn decode_speed() {
804- let _timing = timing_exclusive();
805- if cfg!(debug_assertions) {
806- eprintln!("decode speed is only meaningful in release; skipped");
807- return;
808- }
809- for (name, channels) in [
810- ("opus-ogg-stereo-48000.opus", 2usize),
811- ("opus-ogg-5.1-48000.opus", 6),
812- ] {
813- let path = fixture(name);
814- let data = fs::read(&path).unwrap();
815- let packets = ogg_packets(&data);
816- let head = parse_opus_head(&packets[0]);
817- let audio: Vec<&Vec<u8>> = packets.iter().skip(2).filter(|p| !p.is_empty()).collect();
818- let mut dec =
819- MultistreamDecoder::with_rate(48000, head.streams, head.coupled, &head.mapping);
820- let mut out = vec![0.0f32; 5760 * channels];
821- // Warm up, then measure enough passes to cover several seconds of audio.
822- let mut samples = 0usize;
823- for p in &audio {
824- samples += dec.decode_float(p, &mut out).unwrap();
825- }
826- let passes = 20;
827- let elapsed = best_of_3(|| {
828- for _ in 0..passes {
829- dec.reset();
830- for p in &audio {
831- dec.decode_float(p, &mut out).unwrap();
832- }
833- }
834- });
835- let audio_secs = (samples * passes) as f64 / 48000.0;
836- let realtime = audio_secs / elapsed;
837- println!(
838- "{name}: {realtime:.0}x realtime ({channels} channels, {audio_secs:.1} s in {elapsed:.3} s)"
839- );
840- let floor = if channels == 6 { 104.0 } else { 135.0 };
841- assert!(
842- realtime >= floor,
843- "{name}: {realtime:.0}x realtime is below the {floor}x floor"
844- );
845- }
846-
847- // The fixtures are sine tones, which are cheap; testvector01 is real music
848- // at a high rate and is the honest stereo number.
849- let path = ec_testkit::fixture("vectors/opus-rfc6716/opus_testvectors/testvector01.bit");
850- let packets = read_vector(&path);
851- let mut out = vec![0.0f32; 5760 * 2];
852- let mut samples = 0usize;
853- let elapsed = best_of_3(|| {
854- let mut dec = Decoder::new(48000, 2).unwrap();
855- samples = 0;
856- for p in &packets {
857- samples += dec.decode_float(&p.payload, &mut out).unwrap();
858- }
859- });
860- let realtime = samples as f64 / 48000.0 / elapsed;
861- println!("testvector01 (stereo music): {realtime:.0}x realtime");
862- assert!(realtime >= 135.0, "stereo music decodes at {realtime:.0}x");
863-}
864-
865 // ---------------------------------------------------------------------------752 // ---------------------------------------------------------------------------
866 // Encoder: self-consistency (range-exact against our own decoder)753 // Encoder: self-consistency (range-exact against our own decoder)
867 // ---------------------------------------------------------------------------754 // ---------------------------------------------------------------------------
@@ -952,7 +839,6 @@ fn delayed_corr(source: &[f32], decoded: &[f32], channels: usize, delay: usize)
952 /// the world (the incumbent's exact disease).839 /// the world (the incumbent's exact disease).
953 #[test]840 #[test]
954 fn encoder_roundtrips_at_every_rate() {841 fn encoder_roundtrips_at_every_rate() {
955- let _timing = timing_shared();
956 for channels in [1usize, 2] {842 for channels in [1usize, 2] {
957 let pcm = test_signal(channels, 2.0);843 let pcm = test_signal(channels, 2.0);
958 for kbps in [32u32, 64, 96, 128, 160, 165, 192, 256, 320, 510] {844 for kbps in [32u32, 64, 96, 128, 160, 165, 192, 256, 320, 510] {
@@ -978,7 +864,6 @@ fn encoder_roundtrips_at_every_rate() {
978 /// It is on by default except for `Application::LowDelay`, as in libopus.864 /// It is on by default except for `Application::LowDelay`, as in libopus.
979 #[test]865 #[test]
980 fn libopus_input_alignment_delays_and_still_decodes() {866 fn libopus_input_alignment_delays_and_still_decodes() {
981- let _timing = timing_shared();
982 let pcm = test_signal(2, 1.0);867 let pcm = test_signal(2, 1.0);
983 let mut enc = Encoder::new(48000, 2, Application::Audio).unwrap();868 let mut enc = Encoder::new(48000, 2, Application::Audio).unwrap();
984 enc.set_bitrate(128_000);869 enc.set_bitrate(128_000);
@@ -1006,7 +891,6 @@ fn libopus_input_alignment_delays_and_still_decodes() {
1006 // voice/music bandwidth tables blended by voice_est) since lane opus-mode;891 // voice/music bandwidth tables blended by voice_est) since lane opus-mode;
1007 // matched against ffmpeg libopus on real speech in lanes/opus-mode-r1.sweep.txt.892 // matched against ffmpeg libopus on real speech in lanes/opus-mode-r1.sweep.txt.
1008 fn encoder_mode_follows_application_bitrate_channels_and_frame_size() {893 fn encoder_mode_follows_application_bitrate_channels_and_frame_size() {
1009- let _timing = timing_shared();
1010 #[derive(Clone, Copy, Debug)]894 #[derive(Clone, Copy, Debug)]
1011 struct Case {895 struct Case {
1012 app: Application,896 app: Application,
@@ -1367,7 +1251,6 @@ fn encoder_mode_follows_application_bitrate_channels_and_frame_size() {
1367 /// stays decodable and lands near its target.1251 /// stays decodable and lands near its target.
1368 #[test]1252 #[test]
1369 fn encoder_frame_sizes_and_vbr() {1253 fn encoder_frame_sizes_and_vbr() {
1370- let _timing = timing_shared();
1371 let pcm = test_signal(2, 1.0);1254 let pcm = test_signal(2, 1.0);
1372 for frame in [120usize, 240, 480, 960] {1255 for frame in [120usize, 240, 480, 960] {
1373 let mut enc = Encoder::new(48000, 2, Application::Audio).unwrap();1256 let mut enc = Encoder::new(48000, 2, Application::Audio).unwrap();
@@ -1488,7 +1371,6 @@ fn temp_path(name: &str) -> PathBuf {
1488 /// bars here are correlation >= 0.9 at 96/165/256 kbps.1371 /// bars here are correlation >= 0.9 at 96/165/256 kbps.
1489 #[test]1372 #[test]
1490 fn oracle_decodes_our_packets_across_the_rate_table() {1373 fn oracle_decodes_our_packets_across_the_rate_table() {
1491- let _timing = timing_shared();
1492 for channels in [1usize, 2] {1374 for channels in [1usize, 2] {
1493 let pcm = test_signal(channels, 2.0);1375 let pcm = test_signal(channels, 2.0);
1494 let total = pcm.len() / channels;1376 let total = pcm.len() / channels;
@@ -1598,7 +1480,6 @@ fn oracle_decodes_our_packets_across_the_rate_table() {
1598 /// back in its place (order-preserved) at correlation >= 0.9.1480 /// back in its place (order-preserved) at correlation >= 0.9.
1599 #[test]1481 #[test]
1600 fn five_one_encode_end_to_end() {1482 fn five_one_encode_end_to_end() {
1601- let _timing = timing_shared();
1602 const VORBIS_TO_FFMPEG_5_1: [usize; 6] = [0, 2, 1, 4, 5, 3];1483 const VORBIS_TO_FFMPEG_5_1: [usize; 6] = [0, 2, 1, 4, 5, 3];
1603 const TONES: [f64; 6] = [220.0, 440.0, 660.0, 55.0, 880.0, 1320.0];1484 const TONES: [f64; 6] = [220.0, 440.0, 660.0, 55.0, 880.0, 1320.0];
1604 let total = 96000usize;1485 let total = 96000usize;
@@ -1650,7 +1531,6 @@ fn five_one_encode_end_to_end() {
1650 /// libopus hands back is exactly the input length.1531 /// libopus hands back is exactly the input length.
1651 #[test]1532 #[test]
1652 fn ogg_opus_round_trip_duration_is_exact() {1533 fn ogg_opus_round_trip_duration_is_exact() {
1653- let _timing = timing_shared();
1654 let pcm = test_signal(2, 2.0);1534 let pcm = test_signal(2, 2.0);
1655 let total = pcm.len() / 2;1535 let total = pcm.len() / 2;
1656 let mut enc = Encoder::new(48000, 2, Application::Audio).unwrap();1536 let mut enc = Encoder::new(48000, 2, Application::Audio).unwrap();
@@ -1714,7 +1594,6 @@ fn ogg_opus_round_trip_duration_is_exact() {
1714 #[test]1594 #[test]
1715 fn steady_state_encode_loop_zero_alloc() {1595 fn steady_state_encode_loop_zero_alloc() {
1716 install_counter();1596 install_counter();
1717- let _timing = timing_shared();
1718 let pcm = test_signal(2, 0.5);1597 let pcm = test_signal(2, 0.5);
1719 let pcm6 = {1598 let pcm6 = {
1720 let mono = test_signal(1, 0.5);1599 let mono = test_signal(1, 0.5);
@@ -1765,80 +1644,11 @@ fn steady_state_encode_loop_zero_alloc() {
1765 // those is a separate, much larger effort than this defect's scope.1644 // those is a separate, much larger effort than this defect's scope.
1766 }1645 }
1767 1646
1768-/// The throughput headline: encode speed in multiples of realtime, per
1769-/// configuration, at the product's rates. Floors are deliberately far under
1770-/// the measured numbers — they catch a regression class (an accidental
1771-/// O(n^2), a debug path left on), not machine variance.
1772-#[test]
1773-fn encode_speed() {
1774- let _timing = timing_exclusive();
1775- if cfg!(debug_assertions) {
1776- eprintln!("encode speed is only meaningful in release; skipped");
1777- return;
1778- }
1779- let secs = 4.0;
1780- for (name, channels, kbps, floor) in [
1781- ("mono 96k", 1usize, 96u32, 100.0f64),
1782- ("stereo 128k", 2, 128, 70.0),
1783- ("stereo 256k", 2, 256, 70.0),
1784- ("stereo 510k", 2, 510, 50.0),
1785- ] {
1786- let pcm = test_signal(channels, secs);
1787- let mut enc = Encoder::new(48000, channels, Application::Audio).unwrap();
1788- enc.set_bitrate(kbps * 1000);
1789- let mut out = vec![0u8; 1500];
1790- // Warm up one pass, then measure.
1791- for block in pcm.chunks_exact(960 * channels) {
1792- enc.encode_float(block, 960, &mut out).unwrap();
1793- }
1794- let passes = 4;
1795- let elapsed = best_of_3(|| {
1796- for _ in 0..passes {
1797- for block in pcm.chunks_exact(960 * channels) {
1798- enc.encode_float(block, 960, &mut out).unwrap();
1799- }
1800- }
1801- });
1802- let realtime = secs * passes as f64 / elapsed;
1803- println!("{name}: {realtime:.0}x realtime");
1804- assert!(
1805- realtime >= floor,
1806- "{name}: {realtime:.0}x is below {floor}x"
1807- );
1808- }
1809- // 5.1 multistream.
1810- let mono = test_signal(1, secs);
1811- let mut pcm6 = Vec::with_capacity(mono.len() * 6);
1812- for &s in &mono {
1813- for c in 0..6 {
1814- pcm6.push(s * (1.0 + c as f32 * 0.01));
1815- }
1816- }
1817- let mut ms = MultistreamEncoder::surround_5_1(48000).unwrap();
1818- ms.set_bitrate(384_000);
1819- let mut out = vec![0u8; 8 * 1500];
1820- for block in pcm6.as_chunks::<{ 960 * 6 }>().0 {
1821- ms.encode_float(block, 960, &mut out).unwrap();
1822- }
1823- let passes = 4;
1824- let elapsed = best_of_3(|| {
1825- for _ in 0..passes {
1826- for block in pcm6.as_chunks::<{ 960 * 6 }>().0 {
1827- ms.encode_float(block, 960, &mut out).unwrap();
1828- }
1829- }
1830- });
1831- let realtime = secs * passes as f64 / elapsed;
1832- println!("5.1 384k: {realtime:.0}x realtime");
1833- assert!(realtime >= 25.0, "5.1: {realtime:.0}x is below 25x");
1834-}
1835-
1836 /// Malformed input must come back as an error, never a panic: the decoders1647 /// Malformed input must come back as an error, never a panic: the decoders
1837 /// index tables with values the bitstream chooses, so this throws well-formed1648 /// index tables with values the bitstream chooses, so this throws well-formed
1838 /// TOC bytes at random payloads to make sure every mode is actually entered.1649 /// TOC bytes at random payloads to make sure every mode is actually entered.
1839 #[test]1650 #[test]
1840 fn garbage_payloads_never_panic() {1651 fn garbage_payloads_never_panic() {
1841- let _timing = timing_shared();
1842 let mut state = 0x12345678u32;1652 let mut state = 0x12345678u32;
1843 let mut rand = move || {1653 let mut rand = move || {
1844 state ^= state << 13;1654 state ^= state << 13;
@@ -2022,7 +1832,6 @@ fn aligned_corr(reference: &[f32], decoded: &[f32], max_lag: usize) -> (f64, usi
2022 1832
2023 #[test]1833 #[test]
2024 fn silk_mono_nb_wb_roundtrip() {1834 fn silk_mono_nb_wb_roundtrip() {
2025- let _timing = timing_shared();
2026 let wav = fixture("wav16-mono-48000.wav");1835 let wav = fixture("wav16-mono-48000.wav");
2027 let mut sources = vec![("synthetic", speech_like())];1836 let mut sources = vec![("synthetic", speech_like())];
2028 let w = read_wav_mono(&wav);1837 let w = read_wav_mono(&wav);
@@ -2067,7 +1876,6 @@ fn silk_mono_nb_wb_roundtrip() {
2067 1876
2068 #[test]1877 #[test]
2069 fn silk_mediumband_and_10ms_roundtrip() {1878 fn silk_mediumband_and_10ms_roundtrip() {
2070- let _timing = timing_shared();
2071 let pcm = speech_like();1879 let pcm = speech_like();
2072 for (tag, bandwidth, frame_ms, config, cutoff, bps, floor) in [1880 for (tag, bandwidth, frame_ms, config, cutoff, bps, floor) in [
2073 (1881 (
@@ -2103,7 +1911,6 @@ fn silk_mediumband_and_10ms_roundtrip() {
2103 1911
2104 #[test]1912 #[test]
2105 fn silk_multiframe_packets_roundtrip() {1913 fn silk_multiframe_packets_roundtrip() {
2106- let _timing = timing_shared();
2107 let full = speech_like();1914 let full = speech_like();
2108 let pcm = &full[..46_080];1915 let pcm = &full[..46_080];
2109 for (tag, bandwidth, frame_ms, config, cutoff, bps, floor) in [1916 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:
2180 1987
2181 #[test]1988 #[test]
2182 fn silk_stereo_speech_roundtrip() {1989 fn silk_stereo_speech_roundtrip() {
2183- let _timing = timing_shared();
2184 let pcm = stereo_speech_pair();1990 let pcm = stereo_speech_pair();
2185 let total = pcm.len() / 2;1991 let total = pcm.len() / 2;
2186 for (tag, bandwidth, per_channel_bps, cutoff, frame, config, code, frames) in [1992 for (tag, bandwidth, per_channel_bps, cutoff, frame, config, code, frames) in [
@@ -2331,7 +2137,6 @@ fn silk_sources() -> Vec<(&'static str, Vec<f32>)> {
2331 /// path it dispatches to.2137 /// path it dispatches to.
2332 #[test]2138 #[test]
2333 fn silk_packets_track_encoder_bitrate() {2139 fn silk_packets_track_encoder_bitrate() {
2334- let _timing = timing_shared();
2335 let pcm = test_signal(1, 2.0);2140 let pcm = test_signal(1, 2.0);
2336 for kbps_target in [8u32, 12, 16] {2141 for kbps_target in [8u32, 12, 16] {
2337 let mut enc = Encoder::new(48000, 1, Application::Voip).unwrap();2142 let mut enc = Encoder::new(48000, 1, Application::Voip).unwrap();
@@ -2399,7 +2204,6 @@ fn hybrid_delay(bps: u32) -> usize {
2399 2204
2400 #[test]2205 #[test]
2401 fn hybrid_layers_align() {2206 fn hybrid_layers_align() {
2402- let _timing = timing_shared();
2403 let mut click = vec![0f32; 48000];2207 let mut click = vec![0f32; 48000];
2404 // Well inside the stream so both layers' predictors are running.2208 // Well inside the stream so both layers' predictors are running.
2405 let at = 960 * 10 + 300;2209 let at = 960 * 10 + 300;
@@ -2438,7 +2242,6 @@ fn hybrid_layers_align() {
2438 /// [`Encoder::look_ahead`] of that same configuration, never a constant.2242 /// [`Encoder::look_ahead`] of that same configuration, never a constant.
2439 #[test]2243 #[test]
2440 fn hybrid_fb_roundtrip() {2244 fn hybrid_fb_roundtrip() {
2441- let _timing = timing_shared();
2442 for (name, pcm) in silk_sources() {2245 for (name, pcm) in silk_sources() {
2443 let (decoded, packets) = hybrid_roundtrip(&pcm, 32_000, None, (false, false));2246 let (decoded, packets) = hybrid_roundtrip(&pcm, 32_000, None, (false, false));
2444 let toc = ec_opus::Toc::new(packets[0][0]);2247 let toc = ec_opus::Toc::new(packets[0][0]);
@@ -2528,7 +2331,6 @@ fn hybrid_roundtrip_shape(
2528 2331
2529 #[test]2332 #[test]
2530 fn hybrid_shapes_roundtrip() {2333 fn hybrid_shapes_roundtrip() {
2531- let _timing = timing_shared();
2532 for (tag, channels, frame, bps, bandwidth, config, floor) in [2334 for (tag, channels, frame, bps, bandwidth, config, floor) in [
2533 (2335 (
2534 "mono-swb10",2336 "mono-swb10",
@@ -2623,7 +2425,6 @@ fn hybrid_shapes_roundtrip() {
2623 2425
2624 #[test]2426 #[test]
2625 fn opus_compare_err_pinned_against_c() {2427 fn opus_compare_err_pinned_against_c() {
2626- let _timing = timing_shared();
2627 const N: usize = 48000 * 4;2428 const N: usize = 48000 * 4;
2628 let t: Vec<f64> = (0..N).map(|i| i as f64 / 48000.0).collect();2429 let t: Vec<f64> = (0..N).map(|i| i as f64 / 48000.0).collect();
2629 2430