pce_bench/
pce-bench.rs

1//! The benchmark: frames per second of this board on a card, with a checksum of every picture,
2//! every audio sample and the snapshot at the end, so a faster build that changes the output
3//! or the state is caught here too.
4//!
5//!     pce-bench IMAGE [--frames N] [options of trace::Setup]
6//!
7//! The time is this thread's time on the CPU (`/proc/thread-self/schedstat`, nanoseconds run),
8//! not the wall clock: on a shared machine the wall clock counts the time other processes had
9//! the CPU. Only `run_frame` and `take_audio` are timed; the checksum is taken outside.
10//! `scripts/bench.py` times the reference; `scripts/ab.py` compares two builds of this.
11
12use pcengine_rust::trace::Setup;
13
14/// Nanoseconds this thread has run on a CPU (the wall clock where there is no /proc).
15fn cpu_ns(began: &std::time::Instant) -> u64 {
16    match std::fs::read_to_string("/proc/thread-self/schedstat") {
17        Ok(text) => text.split_whitespace().next().unwrap().parse().unwrap(),
18        Err(_) => began.elapsed().as_nanos() as u64,
19    }
20}
21
22fn fnv(hash: &mut u64, value: u64) {
23    *hash = (*hash ^ value).wrapping_mul(0x0000_0100_0000_01B3);
24}
25
26fn main() {
27    let args: Vec<String> = std::env::args().skip(1).collect();
28    let setup = match Setup::parse(&args) {
29        Ok(setup) => setup,
30        Err(error) => {
31            eprintln!("pce-bench: {error}");
32            std::process::exit(1);
33        }
34    };
35    let name = args
36        .iter()
37        .find(|arg| !arg.starts_with("--"))
38        .map(|path| {
39            std::path::Path::new(path)
40                .file_stem()
41                .unwrap()
42                .to_string_lossy()
43                .into_owned()
44        })
45        .unwrap_or_default();
46    let mut system = setup.build().expect("a board");
47    let clock = std::time::Instant::now();
48    let mut check = 0xCBF2_9CE4_8422_2325u64;
49    let mut spent = 0u64;
50    for _ in 0..setup.frames {
51        setup.apply_presses(&mut system);
52        let began = cpu_ns(&clock);
53        system.run_frame();
54        let samples = system.board_mut().take_audio();
55        spent += cpu_ns(&clock) - began;
56        for (left, right) in samples {
57            fnv(&mut check, (left as u16 as u64) << 16 | right as u16 as u64);
58        }
59        if let Some(frame) = system.board().capture.last_frame.as_ref() {
60            for (row, divider) in frame.rows.iter().zip(&frame.dividers) {
61                fnv(&mut check, *divider as u64);
62                for value in row {
63                    fnv(&mut check, *value as u64);
64                }
65            }
66        }
67    }
68    fnv(&mut check, system.board().mclk as u64);
69    // the whole machine as a snapshot's bytes: a shortcut may not leave the state another
70    // one than the base board's, even where no picture or sample shows it yet
71    for byte in pcengine_rust::state::save_snapshot(&system) {
72        fnv(&mut check, byte as u64);
73    }
74    let seconds = spent as f64 / 1e9;
75    println!(
76        "{name}: {} frames in {seconds:.3} s, {:.1} frames/s, check {:08X}",
77        setup.frames,
78        setup.frames as f64 / seconds,
79        (check ^ (check >> 32)) as u32
80    );
81}