| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | #[path = "it/common.rs"] |
| 13 | mod common; |
| 14 | |
| 15 | use common::{natural_frame, test_frame}; |
| 16 | use ec_h265::encoder::{Encoder, EncoderConfig, RateControl}; |
| 17 | use std::time::Instant; |
| 18 | |
| 19 | fn measure(width: u32, height: u32, threads: usize, frames: usize) -> f64 { |
| 20 | measure_with(width, height, threads, frames, natural_frame, None) |
| 21 | } |
| 22 | |
| 23 | |
| 24 | |
| 25 | fn load_average() -> f64 { |
| 26 | std::fs::read_to_string("/proc/loadavg") |
| 27 | .ok() |
| 28 | .and_then(|s| s.split_whitespace().next()?.parse().ok()) |
| 29 | .unwrap_or(0.0) |
| 30 | } |
| 31 | |
| 32 | fn measure_with( |
| 33 | width: u32, |
| 34 | height: u32, |
| 35 | threads: usize, |
| 36 | frames: usize, |
| 37 | make: fn(u32, u32, u32) -> ec_core::frame::VideoFrame, |
| 38 | ctb: Option<usize>, |
| 39 | ) -> f64 { |
| 40 | let mut cfg = EncoderConfig::new(width, height); |
| 41 | cfg.rate_control = RateControl::ConstantQp(27); |
| 42 | cfg.threads = threads; |
| 43 | if let Some(ctb) = ctb { |
| 44 | cfg.ctb_size = ctb; |
| 45 | } |
| 46 | let encoder = Encoder::new(cfg).expect("encoder"); |
| 47 | let pictures: Vec<_> = (0..frames) |
| 48 | .map(|i| make(width, height, i as u32 * 7)) |
| 49 | .collect(); |
| 50 | |
| 51 | |
| 52 | |
| 53 | encoder.encode_idr(&pictures[0]).expect("warm-up encode"); |
| 54 | let mut best = f64::MAX; |
| 55 | for _ in 0..3 { |
| 56 | let start = Instant::now(); |
| 57 | for picture in &pictures { |
| 58 | encoder.encode_idr(picture).expect("encode"); |
| 59 | } |
| 60 | best = best.min(start.elapsed().as_secs_f64()); |
| 61 | } |
| 62 | frames as f64 / best |
| 63 | } |
| 64 | |
| 65 | #[test] |
| 66 | fn intra_1080p_beats_the_incumbent_on_twelve_threads() { |
| 67 | let cores = std::thread::available_parallelism().map_or(1, |n| n.get()); |
| 68 | let frames = if cfg!(debug_assertions) { 1 } else { 6 }; |
| 69 | let single = measure(1920, 1080, 1, frames); |
| 70 | let many = measure(1920, 1080, cores, frames); |
| 71 | println!( |
| 72 | "1080p intra, camera-like fixture: {single:.2} fps on 1 thread, {many:.2} fps on {cores} threads, speed-up {:.2}x", |
| 73 | many / single |
| 74 | ); |
| 75 | |
| 76 | |
| 77 | let noisy = measure_with(1920, 1080, cores, frames, test_frame, None); |
| 78 | println!("1080p intra, noise fixture: {noisy:.2} fps on {cores} threads"); |
| 79 | if cfg!(debug_assertions) { |
| 80 | println!("debug build: not asserting the bar"); |
| 81 | return; |
| 82 | } |
| 83 | let load = load_average(); |
| 84 | if load > cores as f64 / 2.0 { |
| 85 | println!( |
| 86 | "load average {load:.1} on {cores} cores: the bar is not asserted against a busy machine" |
| 87 | ); |
| 88 | return; |
| 89 | } |
| 90 | assert!( |
| 91 | many > 4.30, |
| 92 | "1080p on {cores} threads: {many:.2} fps, incumbent bar is 4.30" |
| 93 | ); |
| 94 | assert!( |
| 95 | many > single * 2.0, |
| 96 | "wavefront gained only {:.2}x", |
| 97 | many / single |
| 98 | ); |
| 99 | } |
| 100 | |
| 101 | |
| 102 | |
| 103 | |
| 104 | |
| 105 | |
| 106 | #[test] |
| 107 | fn ctb_32_is_the_faster_tree_at_1080p() { |
| 108 | if cfg!(debug_assertions) { |
| 109 | println!("debug build: not measuring the tree size"); |
| 110 | return; |
| 111 | } |
| 112 | let cores = std::thread::available_parallelism().map_or(1, |n| n.get()); |
| 113 | let (mut at32, mut at64) = (0.0f64, 0.0f64); |
| 114 | for _ in 0..2 { |
| 115 | at32 = at32.max(measure_with(1920, 1080, cores, 4, natural_frame, Some(32))); |
| 116 | at64 = at64.max(measure_with(1920, 1080, cores, 4, natural_frame, Some(64))); |
| 117 | } |
| 118 | let single64 = measure_with(1920, 1080, 1, 4, natural_frame, Some(64)); |
| 119 | let single32 = measure_with(1920, 1080, 1, 4, natural_frame, Some(32)); |
| 120 | println!( |
| 121 | "1080p on {cores} threads: CTB 32 {at32:.2} fps, CTB 64 {at64:.2} fps ({:.2}x); \ |
| 122 | on 1 thread: 32 {single32:.2} fps, 64 {single64:.2} fps ({:.2}x)", |
| 123 | at32 / at64, |
| 124 | single32 / single64 |
| 125 | ); |
| 126 | let load = load_average(); |
| 127 | if load > cores as f64 / 2.0 { |
| 128 | println!("load average {load:.1} on {cores} cores: not asserting against a busy machine"); |
| 129 | return; |
| 130 | } |
| 131 | assert!( |
| 132 | at32 > at64, |
| 133 | "CTB 32 {at32:.2} fps did not beat CTB 64 {at64:.2} fps on {cores} threads" |
| 134 | ); |
| 135 | } |
| 136 | |
| 137 | #[test] |
| 138 | fn speedup_curve() { |
| 139 | if cfg!(debug_assertions) { |
| 140 | println!("debug build: skipping the speed-up curve"); |
| 141 | return; |
| 142 | } |
| 143 | let cores = std::thread::available_parallelism().map_or(1, |n| n.get()); |
| 144 | let mut base = 0.0; |
| 145 | for threads in [1usize, 2, 4, 8, cores] { |
| 146 | if threads > cores { |
| 147 | continue; |
| 148 | } |
| 149 | let fps = measure(1920, 1080, threads, 4); |
| 150 | if threads == 1 { |
| 151 | base = fps; |
| 152 | } |
| 153 | println!( |
| 154 | "threads {threads:2}: {fps:6.2} fps speed-up {:.2}x", |
| 155 | fps / base |
| 156 | ); |
| 157 | } |
| 158 | } |