diff: Add highlight request render benchmark

709a007e6521dc61f6ecc604898d8767a1c59ddb
Adds benchmark coverage for collecting visible highlight requests.
Alexis Sellier committed ago 1 parent 1ac97113
crates/diff/benches/render.rs +63 -19
1 1
//! Render benchmarks for `rustik-diff`.
2 2
//!
3 3
//! The benchmark decodes deterministic unified diffs once, then exercises
4 -
//! render, scroll, resize, focus, collapse, and highlight-refresh paths.
4 +
//! render, scroll, resize, focus, collapse, highlight-request, and highlight-refresh paths.
5 5
6 6
use std::hint::black_box;
7 7
use std::io::{self, Write as _};
8 8
use std::str::FromStr;
9 9
use std::time::{Duration, Instant};
155 155
    Sticky,
156 156
    /// Focus and collapse workload across many files.
157 157
    Collapse,
158 158
    /// Visible highlight request and row-refresh workload.
159 159
    Highlight,
160 +
    /// Visible highlight request scheduling without drawing rows.
161 +
    HighlightRequests,
160 162
    /// Large file-count workload with short hunks.
161 163
    ManyFiles,
162 164
    /// Single huge file workload.
163 165
    HugeFile,
164 166
    /// Long and Unicode-heavy source line workload.
176 178
                Self::Session,
177 179
                Self::Resize,
178 180
                Self::Sticky,
179 181
                Self::Collapse,
180 182
                Self::Highlight,
183 +
                Self::HighlightRequests,
181 184
                Self::ManyFiles,
182 185
                Self::HugeFile,
183 186
                Self::LongLines,
184 187
            ],
185 188
            workload => vec![workload],
193 196
            Self::Session => "session",
194 197
            Self::Resize => "resize",
195 198
            Self::Sticky => "sticky",
196 199
            Self::Collapse => "collapse",
197 200
            Self::Highlight => "highlight",
201 +
            Self::HighlightRequests => "highlight-requests",
198 202
            Self::ManyFiles => "many-files",
199 203
            Self::HugeFile => "huge-file",
200 204
            Self::LongLines => "long-lines",
201 205
            Self::All => "all",
202 206
        }
203 207
    }
204 208
209 +
    /// Returns the rate unit measured by the workload.
210 +
    const fn rate_unit(self) -> &'static str {
211 +
        match self {
212 +
            Self::HighlightRequests => "tps",
213 +
            Self::WarmScroll
214 +
            | Self::Session
215 +
            | Self::Resize
216 +
            | Self::Sticky
217 +
            | Self::Collapse
218 +
            | Self::Highlight
219 +
            | Self::ManyFiles
220 +
            | Self::HugeFile
221 +
            | Self::LongLines
222 +
            | Self::All => "fps",
223 +
        }
224 +
    }
225 +
205 226
    /// Returns the synthetic document shape for the workload.
206 227
    fn document_shape(self, args: Args) -> DiffShape {
207 228
        match self {
208 229
            Self::ManyFiles => DiffShape {
209 230
                files: args.files.max(1_024),
227 248
                files: args.files.max(256),
228 249
                lines: 32,
229 250
                hunk_lines: 16,
230 251
                profile: LineProfile::Normal,
231 252
            },
232 -
            Self::Session | Self::Resize | Self::Highlight | Self::WarmScroll | Self::All => {
233 -
                DiffShape {
234 -
                    files: args.files,
235 -
                    lines: args.lines,
236 -
                    hunk_lines: 64,
237 -
                    profile: LineProfile::Normal,
238 -
                }
239 -
            }
253 +
            Self::Session
254 +
            | Self::Resize
255 +
            | Self::Highlight
256 +
            | Self::HighlightRequests
257 +
            | Self::WarmScroll
258 +
            | Self::All => DiffShape {
259 +
                files: args.files,
260 +
                lines: args.lines,
261 +
                hunk_lines: 64,
262 +
                profile: LineProfile::Normal,
263 +
            },
240 264
        }
241 265
    }
242 266
}
243 267
244 268
impl FromStr for Workload {
250 274
            "session" => Ok(Self::Session),
251 275
            "resize" => Ok(Self::Resize),
252 276
            "sticky" | "sticky-boundaries" => Ok(Self::Sticky),
253 277
            "collapse" => Ok(Self::Collapse),
254 278
            "highlight" | "highlights" => Ok(Self::Highlight),
279 +
            "highlight-requests" | "requests" => Ok(Self::HighlightRequests),
255 280
            "many-files" => Ok(Self::ManyFiles),
256 281
            "huge-file" => Ok(Self::HugeFile),
257 282
            "long-lines" => Ok(Self::LongLines),
258 283
            "all" => Ok(Self::All),
259 284
            _ => Err(()),
288 313
/// Result from one workload run.
289 314
#[derive(Clone, Copy, Debug)]
290 315
struct BenchReport {
291 316
    /// Workload that produced the report.
292 317
    workload: Workload,
293 -
    /// Frame-rate statistics across independent runs.
294 -
    fps: RepeatStats,
318 +
    /// Workload-rate statistics across independent runs.
319 +
    rate: RepeatStats,
295 320
    /// Combined deterministic render checksum across independent runs.
296 321
    checksum: usize,
297 322
}
298 323
299 324
/// Summary statistics for repeated benchmark runs.
300 325
#[derive(Clone, Copy, Debug)]
301 326
struct RepeatStats {
302 -
    /// Average frames per second.
327 +
    /// Average workload iterations per second.
303 328
    avg: f64,
304 -
    /// Frames-per-second standard deviation.
329 +
    /// Workload-rate standard deviation.
305 330
    stddev: f64,
306 331
}
307 332
308 333
/// Report column widths derived before workloads run.
309 334
#[derive(Clone, Copy, Debug)]
310 335
struct ReportWidths {
311 336
    /// Widest selected workload name.
312 337
    workload: usize,
313 -
    /// Width reserved for formatted frame rates.
314 -
    fps: usize,
338 +
    /// Width reserved for formatted workload rates.
339 +
    rate: usize,
315 340
}
316 341
317 -
const FPS_FORMAT: &str = "000000.0 fps";
342 +
const RATE_FORMAT: &str = "000000.0";
318 343
319 344
/// Whether a streaming row should include the table header.
320 345
#[derive(Clone, Copy, Debug)]
321 346
enum HeaderMode {
322 347
    /// Print the header before the row.
350 375
            eprintln!("failed to decode generated benchmark diff: {error}");
351 376
            std::process::exit(1);
352 377
        }
353 378
    };
354 379
    let rows = DiffViewer::new(document.clone(), DiffOptions::default()).content_rows(AREA.cols);
355 -
    let mut fps = Vec::with_capacity(args.runs);
380 +
    let mut rates = Vec::with_capacity(args.runs);
356 381
    let mut checksum = rows;
357 382
358 383
    for run in 0..args.runs {
359 384
        let (run_elapsed, run_checksum) = run_once(workload, document.clone(), frames);
360 -
        fps.push(frames as f64 / run_elapsed.as_secs_f64());
385 +
        rates.push(frames as f64 / run_elapsed.as_secs_f64());
361 386
        checksum ^= run_checksum.rotate_left((run % usize::BITS as usize) as u32);
362 387
    }
363 388
364 389
    BenchReport {
365 390
        workload,
366 -
        fps: RepeatStats::from_values(&fps),
391 +
        rate: RepeatStats::from_values(&rates),
367 392
        checksum,
368 393
    }
369 394
}
370 395
371 396
/// Runs one independent timed pass for a workload.
375 400
        Workload::Session => run_session(document, frames),
376 401
        Workload::Resize => run_resize(document, frames),
377 402
        Workload::Sticky => run_sticky(document, frames),
378 403
        Workload::Collapse => run_collapse(document, frames),
379 404
        Workload::Highlight => run_highlight(document, frames),
405 +
        Workload::HighlightRequests => run_highlight_requests(document, frames),
380 406
        Workload::ManyFiles => run_session(document, frames),
381 407
        Workload::HugeFile => run_sticky(document, frames),
382 408
        Workload::LongLines => run_resize(document, frames),
383 409
        Workload::All => unreachable!("run_once: all is expanded before dispatch"),
384 410
    }
504 530
        );
505 531
        AREA
506 532
    })
507 533
}
508 534
535 +
/// Schedules visible highlight requests without drawing rendered rows.
536 +
fn run_highlight_requests(document: DiffDocument, frames: usize) -> (Duration, usize) {
537 +
    let mut viewer = DiffViewer::new(document, DiffOptions::default());
538 +
    let start = Instant::now();
539 +
    let mut checksum = 0usize;
540 +
541 +
    for frame in 0..frames {
542 +
        let requests = viewer.visible_highlight_requests(AREA);
543 +
        checksum ^= sample_requests(&requests, frame);
544 +
        viewer.apply(
545 +
            DiffAction::Scroll(Scroll::Lines((frame % 5 + 1) as isize)),
546 +
            AREA,
547 +
        );
548 +
    }
549 +
550 +
    (start.elapsed(), checksum)
551 +
}
552 +
509 553
/// Renders frames using a caller-provided action script.
510 554
fn run_scripted(
511 555
    document: DiffDocument,
512 556
    frames: usize,
513 557
    mut script: impl FnMut(&mut DiffViewer, usize) -> Rect,
src/file_{file:04}.rs\n" +0 -0
crates/tui/benches/render.rs +0 -4
55 55
    for frame in 0..frames {
56 56
        buffer.clear();
57 57
        checksum ^= bench.frame(frame, AREA, &mut buffer);
58 58
        buffer.reset_dirty();
59 59
    }
60 -
61 60
    (start.elapsed(), checksum)
62 61
}
63 62
64 63
struct Args {
65 64
    frames: usize,
99 98
                        frames = value;
100 99
                    }
101 100
                }
102 101
            }
103 102
        }
104 -
105 103
        Self { frames, check }
106 104
    }
107 105
}
108 106
109 107
fn parse_hex(value: &str) -> Option<usize> {
139 137
            &self.files,
140 138
            &self.logs,
141 139
            area,
142 140
            buffer,
143 141
        );
144 -
145 142
        sample_buffer(buffer, frame)
146 143
    }
147 144
}
148 145
149 146
fn render_dashboard(
232 229
        ]);
233 230
        if row % 40 == 39 {
234 231
            table.divider_with_sigil('┄');
235 232
        }
236 233
    }
237 -
238 234
    table
239 235
}
240 236
241 237
fn labels<const N: usize>(items: [&str; N]) -> [Label; N] {
242 238
    items.map(Label::new)