forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB9b67700Tahinli3 days ago4021 commits

perf.rs158 lines, 5.7 KiBHistoryRawDownload
1//! The speed bar, measured the way the claim is made: one `encode_idr` call per
2//! picture, zero threads on the caller's side.
3//!
4//! The incumbent H.265 encoder this replaces has no threads at all inside its
5//! encoder — edith fans whole access units out across twelve cores to reach 4.30
6//! fps at 1080p. The bar here is to beat that from *inside* one call, so the
7//! measurement below hands one picture at a time to one encoder.
8//!
9//! Run under `--release`; in a debug build this prints the numbers and skips the
10//! assertion, because a debug build measures the compiler, not the encoder.
11
12#[path = "it/common.rs"]
13mod common;
14
15use common::{natural_frame, test_frame};
16use ec_h265::encoder::{Encoder, EncoderConfig, RateControl};
17use std::time::Instant;
18
19fn measure(width: u32, height: u32, threads: usize, frames: usize) -> f64 {
20 measure_with(width, height, threads, frames, natural_frame, None)
21}
22
23/// The 1-minute load average, which decides whether a number measured here
24/// means anything.
25fn 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
32fn 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 // One untimed picture so the allocator and the caches are warm, then the
51 // best of three passes: this machine runs other work, and the fastest pass
52 // is the one that measures the encoder rather than the neighbours.
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]
66fn 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 // The same measurement on the worst-case noise fixture, printed rather than
76 // asserted: it is not what the 4.30 fps bar was measured on.
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/// Why [`EncoderConfig::new`] defaults to 32x32 trees: the wavefront hands one
102/// CTB *row* to a worker, so 1080 lines are 34 rows at 32 against 17 at 64, and
103/// 17 rows dealt round-robin over twelve workers leaves seven of them with half
104/// the work of the other five. Sizes are measured alternately so a machine whose
105/// load drifts during the run cannot favour whichever went first.
106#[test]
107fn 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]
138fn 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}