From 309dea385b2fa928527ba6c821e0328e452d2e88 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Thu, 15 Oct 2026 02:44:02 +0200 Subject: [PATCH 1/5] horizon-client --benchmark SCENARIO: the perf_bench run from inside the game, written as a report Co-Authored-By: Claude Opus 5.5 --- Cargo.lock | 1 + crates/horizon-client/Cargo.toml | 1 + crates/horizon-client/src/benchmark.rs | 393 ++++++++++++++++++++++ crates/horizon-client/src/control.rs | 24 ++ crates/horizon-client/src/crash.rs | 16 +- crates/horizon-client/src/game.rs | 45 ++- crates/horizon-client/src/game/control.rs | 32 ++ crates/horizon-client/src/lib.rs | 1 + docs/benchmark.md | 12 + 9 files changed, 519 insertions(+), 6 deletions(-) create mode 100644 crates/horizon-client/src/benchmark.rs diff --git a/Cargo.lock b/Cargo.lock index 8f3a3d546..175ee5230 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1014,6 +1014,7 @@ dependencies = [ "gilrs", "glam", "horizon-assets", + "horizon-bench", "horizon-core", "horizon-friends", "horizon-protocol", diff --git a/crates/horizon-client/Cargo.toml b/crates/horizon-client/Cargo.toml index 0b9df5a04..f13a1130b 100644 --- a/crates/horizon-client/Cargo.toml +++ b/crates/horizon-client/Cargo.toml @@ -30,6 +30,7 @@ flate2 = { version = "1.1.10", default-features = false, features = ["rust_backe horizon-theme = { path = "../horizon-theme" } # Opt-in data collection: the game keeps, the launcher sends (docs/telemetry.md). horizon-telemetry = { path = "../horizon-telemetry", default-features = false } +horizon-bench = { path = "../horizon-bench" } arboard = { version = "3.6.1", default-features = false, features = ["wayland-data-control"] } zip = { version = "2", default-features = false, features = ["deflate"] } # Controllers (evdev + libudev on Linux, IOKit on macOS, Windows.Gaming.Input). diff --git a/crates/horizon-client/src/benchmark.rs b/crates/horizon-client/src/benchmark.rs new file mode 100644 index 000000000..7c29bc9cc --- /dev/null +++ b/crates/horizon-client/src/benchmark.rs @@ -0,0 +1,393 @@ +//! `horizon-client --benchmark SCENARIO`: the run `tools/perf_bench.py` drives from outside, driven +//! from inside, so anyone can measure their machine with nothing but the game. A thread sends the +//! same control commands `horizon-ctl` would, through the game's own command queue, and writes a +//! report (docs/benchmark-report.md) to `benchmarks/` in the game folder. +//! +//! The scenes, the world (seed 1234, creative) and every step and figure match `tools/perf_bench.py`, +//! so reports from the two can be compared. + +use std::path::{Path, PathBuf}; +use std::sync::mpsc::{self, Sender}; +use std::thread; +use std::time::{Duration, Instant}; + +use horizon_bench::{App, Frames, Kind, Report}; +use serde_json::Value; + +use crate::control::Request; + +pub const SEED: i64 = 1234; +/// The world the benchmark creates under `saves/`, replaced on every run. +pub const WORLD: &str = "horizon-benchmark"; +/// Written into the world so a later run only ever replaces its own world. +const MARKER: &str = "horizon-benchmark.txt"; + +/// A straight flight over terrain: name, start, end, pitch (`tools/graphics_bench.py`'s paths). +type Scene = (&'static str, [f64; 3], [f64; 3], f64); +pub const SCENES: [Scene; 4] = [ + ( + "village", + [-960.0, 125.0, 80.0], + [-960.0, 125.0, 272.0], + 35.0, + ), + ( + "forest", + [-1100.0, 120.0, -560.0], + [-1100.0, 120.0, -368.0], + 25.0, + ), + ( + "ocean", + [-300.0, 85.0, -420.0], + [-300.0, 85.0, -228.0], + 10.0, + ), + ( + "mountain", + [560.0, 260.0, 860.0], + [752.0, 260.0, 860.0], + 30.0, + ), +]; +const FLIGHT_SECONDS: f64 = 12.0; +const SETTLE: Duration = Duration::from_secs(2); +const LOAD_LIMIT: Duration = Duration::from_secs(600); + +/// `standard` flies every scene; a scene's name flies only that one. +pub fn scenes(scenario: &str) -> Option> { + match scenario { + "standard" => Some(SCENES.to_vec()), + name => SCENES.iter().find(|s| s.0 == name).map(|s| vec![*s]), + } +} + +/// What the game knows about the run before it starts. +pub struct Setup { + pub scenario: String, + pub started: Instant, + pub app: App, + pub simulation_distance: u32, + pub vsync: bool, + /// `None`: unlimited. + pub fps_cap: Option, + pub offscreen: bool, + pub game_dir: PathBuf, +} + +/// Starts the driver; the game quits when it is done, and the report's path is printed. +pub fn start(requests: Sender, setup: Setup) { + thread::Builder::new() + .name("horizon-benchmark".into()) + .spawn(move || { + let control = Control(requests); + match run(&control, setup) { + Ok(path) => println!("Horizon: benchmark report {}", path.display()), + Err(e) => eprintln!("Horizon: benchmark failed: {e}"), + } + let _ = control.call("quit"); + }) + .expect("spawn the benchmark thread"); +} + +struct Control(Sender); + +impl Control { + fn call(&self, line: &str) -> Result { + let (reply, response) = mpsc::channel(); + self.0 + .send(Request { + line: line.to_owned(), + reply, + }) + .map_err(|_| "the game has exited".to_owned())?; + // Flights and loads reply when they finish. + let text = response + .recv_timeout(LOAD_LIMIT) + .map_err(|_| format!("{line}: no reply"))?; + let value: Value = serde_json::from_str(&text).map_err(|e| e.to_string())?; + match value.get("error").and_then(Value::as_str) { + Some(e) => Err(format!("{line}: {e}")), + None => Ok(value), + } + } + fn status(&self) -> Value { + self.call("status").unwrap_or(Value::Null) + } +} + +fn run(control: &Control, setup: Setup) -> Result { + let scenes = + scenes(&setup.scenario).ok_or_else(|| format!("unknown scenario {}", setup.scenario))?; + let mut report = Report::new(Kind::Game, setup.app.clone()); + let pid = std::process::id(); + report.conditions.load_avg_1m = horizon_bench::load_average(); + report.conditions.power = horizon_bench::power_source().map(str::to_owned); + let rss = RssSampler::start(pid); + + let title = poll(control, LOAD_LIMIT, |s| { + s["screen"] == "title" && !s["renderer"].is_null() + })?; + report.metric("startup.title_ms", &[ms(setup.started.elapsed())]); + let renderer = &title["renderer"]; + report.hardware.gpu = Some(horizon_bench::Gpu { + name: text(&renderer["gpu"]), + vendor: text(&renderer["vendor"]), + driver: text(&renderer["driver"]), + backend: text(&renderer["backend"]).to_lowercase(), + kind: text(&renderer["device_type"]), + }); + report.hardware.display = serde_json::from_value(title["display"].clone()).ok(); + + replace_world(&setup.game_dir)?; + control.call(&format!("open {WORLD} {SEED} creative"))?; + std::fs::write( + setup.game_dir.join("saves").join(WORLD).join(MARKER), + "Created by horizon-client --benchmark; replaced by the next run.\n", + ) + .map_err(|e| e.to_string())?; + let loaded = wait_loaded(control, |_| true)?; + report.metric("startup.world_ms", &[ms(loaded - setup.started)]); + + let mut steady = Vec::new(); + for (name, start, end, pitch) in scenes { + control.call("time 6000")?; + let meshed = control.status()["meshed_sections"].as_f64().unwrap_or(0.0); + let generated = control.status()["generated_chunks"].as_f64().unwrap_or(0.0); + let [x, y, z] = start; + control.call(&format!("tp {x} {y} {z}"))?; + let teleported = Instant::now(); + // A zero-length flight holds the player at the start, flying. + control.call(&format!("fly {x} {y} {z} {x} {y} {z} 0.1 {pitch}"))?; + control.call(&format!("look {:.3} {pitch}", yaw(start, end)))?; + let near = |p: &Value| { + let p: Vec = p + .as_array() + .into_iter() + .flatten() + .filter_map(Value::as_f64) + .collect(); + p.len() == 3 && (0..3).map(|i| (p[i] - start[i]).powi(2)).sum::() < 4.0 + }; + let loaded = wait_loaded(control, near)?; + let load = loaded - teleported; + report.metric(&format!("chunks.{name}.load_ms"), &[ms(load)]); + let status = control.status(); + let meshes = status["meshed_sections"].as_f64().unwrap_or(0.0) - meshed; + let chunks = status["generated_chunks"].as_f64().unwrap_or(0.0) - generated; + report.metric(&format!("chunks.{name}.meshed_count"), &[meshes]); + report.metric(&format!("chunks.{name}.generated_count"), &[chunks]); + if load.as_secs_f64() > 0.0 { + report.metric( + &format!("chunks.{name}.generated_per_s"), + &[chunks / load.as_secs_f64()], + ); + } + thread::sleep(SETTLE); + let [ex, ey, ez] = end; + let begin = Instant::now(); + let flight = control.call(&format!( + "fly {x} {y} {z} {ex} {ey} {ez} {FLIGHT_SECONDS} {pitch} raw" + ))?; + steady.push((begin, Instant::now())); + let series = |key: &str| -> Vec { + flight[key] + .as_array() + .into_iter() + .flatten() + .filter_map(Value::as_f64) + .map(|v| v as f32) + .collect() + }; + let frames = Frames::of( + name, + 0, + &series("frame_ms"), + &series("cpu_ms"), + &series("gpu_ms"), + ) + .ok_or("the flight drew no frames")?; + report.metric(&format!("fps.{name}.avg_fps"), &[frames.avg_fps]); + report.metric(&format!("fps.{name}.low_1pct_fps"), &[frames.low_1pct_fps]); + report.metric( + &format!("fps.{name}.low_01pct_fps"), + &[frames.low_01pct_fps], + ); + report.frames.push(frames); + } + + let status = control.status(); + let renderer = &status["renderer"]; + let c = &mut report.conditions; + c.scenario = Some(setup.scenario.clone()); + c.seed = Some(SEED.to_string()); + c.render_distance = status["render_distance"].as_u64().map(|d| d as u32); + c.simulation_distance = Some(setup.simulation_distance); + c.window = serde_json::from_value(renderer["size"].clone()).ok(); + c.vsync = Some(setup.vsync); + c.fps_cap = setup.fps_cap; + c.offscreen = Some(setup.offscreen); + c.repeats = Some(1); + c.warmup_s = Some(SETTLE.as_secs_f64()); + if let Some(bytes) = renderer["gpu_allocated_bytes"].as_f64() { + report.metric("memory.gpu_mb", &[bytes / f64::from(1 << 20)]); + } + let samples = rss.stop(); + if let Some(peak) = samples.iter().map(|s| s.1).max() { + report.metric("memory.rss_peak_mb", &[peak as f64 / f64::from(1 << 20)]); + } + let mut during: Vec = samples + .iter() + .filter(|(t, _)| steady.iter().any(|(a, b)| a <= t && t <= b)) + .map(|s| s.1) + .collect(); + during.sort_unstable(); + if let Some(&median) = during.get(during.len() / 2) { + report.metric( + "memory.rss_steady_mb", + &[median as f64 / f64::from(1 << 20)], + ); + } + if let Ok(meta) = std::env::current_exe().and_then(std::fs::metadata) { + report.metric("game.binary_bytes", &[meta.len() as f64]); + } + report + .write_to(&setup.game_dir.join("benchmarks")) + .map_err(|e| e.to_string()) +} + +/// Removes the previous run's world, never one the benchmark didn't create. +fn replace_world(game_dir: &Path) -> Result<(), String> { + let world = game_dir.join("saves").join(WORLD); + if !world.exists() { + return Ok(()); + } + if !world.join(MARKER).exists() { + return Err(format!( + "{} exists and was not created by the benchmark: rename it", + world.display() + )); + } + std::fs::remove_dir_all(&world).map_err(|e| e.to_string()) +} + +fn poll( + control: &Control, + limit: Duration, + ready: impl Fn(&Value) -> bool, +) -> Result { + let deadline = Instant::now() + limit; + while Instant::now() < deadline { + let status = control.status(); + if ready(&status) { + return Ok(status); + } + thread::sleep(Duration::from_millis(50)); + } + Err(format!("not ready after {} s", limit.as_secs())) +} + +/// Waits until the player is spawned where `near` says, nothing is queued and the resident and +/// generated chunk counts have not changed for a second; returns the time of the last change +/// (`tools/perf_bench.py`'s `wait_loaded`). +fn wait_loaded(control: &Control, near: impl Fn(&Value) -> bool) -> Result { + let deadline = Instant::now() + LOAD_LIMIT; + let (mut last, mut changed) = (None, Instant::now()); + while Instant::now() < deadline { + let s = control.status(); + let position = &s["player"]["position"]; + if position.is_null() + || !near(position) + || s["pending_chunks"] != 0 + || s["screen"] == "title" + { + (last, changed) = (None, Instant::now()); + } else { + let counts = (s["chunks"].as_u64(), s["generated_chunks"].as_u64()); + if last != Some(counts) { + (last, changed) = (Some(counts), Instant::now()); + } else if changed.elapsed() >= Duration::from_secs(1) { + return Ok(changed); + } + } + thread::sleep(Duration::from_millis(50)); + } + Err(format!("not loaded after {} s", LOAD_LIMIT.as_secs())) +} + +/// Vanilla's yaw facing from `start` to `end`. +fn yaw(start: [f64; 3], end: [f64; 3]) -> f64 { + (-(end[0] - start[0])).atan2(end[2] - start[2]).to_degrees() +} + +fn ms(d: Duration) -> f64 { + d.as_secs_f64() * 1000.0 +} + +fn text(v: &Value) -> String { + v.as_str().unwrap_or_default().to_owned() +} + +/// Samples the process's resident memory four times a second, as `tools/perf_bench.py` does. +struct RssSampler { + stop: Sender<()>, + thread: thread::JoinHandle>, +} + +impl RssSampler { + fn start(pid: u32) -> Self { + let (stop, stopped) = mpsc::channel(); + let thread = thread::spawn(move || { + let mut samples = Vec::new(); + while let Err(mpsc::RecvTimeoutError::Timeout) = + stopped.recv_timeout(Duration::from_millis(250)) + { + samples.extend(horizon_bench::rss_bytes(pid).map(|b| (Instant::now(), b))); + } + samples + }); + Self { stop, thread } + } + fn stop(self) -> Vec<(Instant, u64)> { + let _ = self.stop.send(()); + self.thread.join().unwrap_or_default() + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn scenarios() { + assert_eq!(scenes("standard").unwrap().len(), 4); + assert_eq!(scenes("ocean").unwrap()[0].0, "ocean"); + assert!(scenes("nether").is_none()); + } + + #[test] + fn yaw_matches_vanilla() { + // South (+Z) is 0, west (-X) 90, east (+X) -90. + assert!((yaw([0.0; 3], [0.0, 0.0, 10.0])).abs() < 1e-9); + assert!((yaw([0.0; 3], [-10.0, 0.0, 0.0]) - 90.0).abs() < 1e-9); + assert!((yaw([0.0; 3], [10.0, 0.0, 0.0]) + 90.0).abs() < 1e-9); + } + + #[test] + fn only_replaces_its_own_world() { + let dir = + std::env::temp_dir().join(format!("horizon-benchmark-test-{}", std::process::id())); + let world = dir.join("saves").join(WORLD); + std::fs::create_dir_all(&world).unwrap(); + assert!( + replace_world(&dir).is_err(), + "a player's world of that name stays" + ); + assert!(world.exists()); + std::fs::write(world.join(MARKER), "").unwrap(); + replace_world(&dir).unwrap(); + assert!(!world.exists()); + replace_world(&dir).unwrap(); + std::fs::remove_dir_all(dir).unwrap(); + } +} diff --git a/crates/horizon-client/src/control.rs b/crates/horizon-client/src/control.rs index bbf158abe..3019d8dfa 100644 --- a/crates/horizon-client/src/control.rs +++ b/crates/horizon-client/src/control.rs @@ -150,6 +150,7 @@ pub struct Request { pub struct Server { path: PathBuf, requests: mpsc::Receiver, + sender: mpsc::Sender, } /// `$HORIZON_CONTROL`, else `horizon-control.sock` in the user's runtime @@ -172,6 +173,7 @@ impl Server { std::fs::remove_file(path)?; } let (sender, requests) = mpsc::channel(); + let local = sender.clone(); if uses_tcp(path) { let listener = TcpListener::bind((Ipv4Addr::LOCALHOST, 0))?; let token = random_token(); @@ -204,8 +206,22 @@ impl Server { Ok(Self { path: path.to_owned(), requests, + sender: local, }) } + /// Commands from inside the process only (`--benchmark` without `--control`). + pub fn local() -> Self { + let (sender, requests) = mpsc::channel(); + Self { + path: PathBuf::new(), + requests, + sender, + } + } + /// Sends commands as a socket client would, from another thread. + pub fn sender(&self) -> mpsc::Sender { + self.sender.clone() + } pub fn path(&self) -> &Path { &self.path } @@ -428,6 +444,14 @@ impl FrameStats { pub fn frame_millis(&self) -> &[f32] { &self.millis } + /// CPU time of every recorded frame, in milliseconds. + pub fn cpu_millis(&self) -> &[f32] { + &self.cpu + } + /// GPU time of every frame the GPU timed, in milliseconds. + pub fn gpu_millis(&self) -> &[f32] { + &self.gpu + } pub fn reset(&mut self) { self.millis.clear(); self.cpu.clear(); diff --git a/crates/horizon-client/src/crash.rs b/crates/horizon-client/src/crash.rs index e0ece85f2..a0b68260d 100644 --- a/crates/horizon-client/src/crash.rs +++ b/crates/horizon-client/src/crash.rs @@ -105,6 +105,16 @@ impl Report { } } +/// The release tag (the launcher installs each build in a folder named after it) and the client's +/// own version, or just the latter. +pub fn version() -> String { + let version = env!("CARGO_PKG_VERSION"); + match release_tag() { + Some(tag) => format!("{tag} (client {version})"), + None => version.to_owned(), + } +} + /// The release this binary came with: launcher installs live in a folder named after the tag. pub fn release_tag() -> Option { let exe = std::env::current_exe().ok()?; @@ -228,12 +238,8 @@ fn memory() -> String { /// The report's details: Horizon's and the machine's, then what the game set as it ran. pub fn system_details() -> Vec<(String, String)> { - let mut version = env!("CARGO_PKG_VERSION").to_owned(); - if let Some(tag) = release_tag() { - version = format!("{tag} (client {version})"); - } let mut out = vec![ - ("Horizon Version".to_owned(), version), + ("Horizon Version".to_owned(), version()), ("Minecraft Version".to_owned(), "26.3".to_owned()), ("Operating System".to_owned(), operating_system()), ("CPU".to_owned(), cpu()), diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index 9221a7485..3d20e704a 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -217,6 +217,8 @@ struct Options { /// `--dedicated`: run as a dedicated server; vanilla's server arguments /// (`--nogui`, `--port`, `--world`, `--universe`, `--initSettings`) follow. dedicated: Option>, + /// `--benchmark SCENARIO`: run the benchmark, write its report and quit. + benchmark: Option, } impl Options { /// The name to join servers with: the one given, else the offline name kept @@ -265,6 +267,7 @@ impl Options { .ok() .filter(|n| !n.is_empty()), dedicated: None, + benchmark: None, }; let mut session_file = std::env::var("HORIZON_SESSION_FILE").ok(); let mut args = std::env::args().skip(1); @@ -272,7 +275,7 @@ impl Options { match arg.as_str() { "--help" | "-h" => { println!( - "Horizon — Minecraft Java Edition 26.3 in Rust\n\n --resources PATH Prepared resources (horizon-prepare)\n --render-distance N Chunks, 2..64 (default 12)\n --world NAME World folder under saves/ (default \"New World\")\n --seed N World seed for new worlds (default 0)\n --creative Creative mode (default survival)\n --test-terrain Simple test terrain instead of vanilla generation\n --frames N Exit after N frames (testing)\n --smoke-report PATH Write a JSON report on exit\n --control Accept horizon-ctl commands on a local socket\n --control-socket PATH Control socket path (default $XDG_RUNTIME_DIR/horizon-control.sock)\n --server HOST[:PORT] Join a multiplayer server (offline mode) at start\n --name NAME Player name for multiplayer (default: generated once, kept in horizon-player.txt)\n --session-file PATH Signed-in account from the launcher (read once, then deleted)\n\nControls: mouse look, WASD move, Space jump, Left Shift sneak, Left Ctrl sprint,\nleft click break, right click place/use, 1-9/scroll hotbar, E inventory,\nQ drop, F3 debug, Esc pause." + "Horizon — Minecraft Java Edition 26.3 in Rust\n\n --resources PATH Prepared resources (horizon-prepare)\n --render-distance N Chunks, 2..64 (default 12)\n --world NAME World folder under saves/ (default \"New World\")\n --seed N World seed for new worlds (default 0)\n --creative Creative mode (default survival)\n --test-terrain Simple test terrain instead of vanilla generation\n --frames N Exit after N frames (testing)\n --smoke-report PATH Write a JSON report on exit\n --benchmark SCENARIO Run a benchmark (standard, village, forest, ocean, mountain), write its report to benchmarks/ and quit\n --control Accept horizon-ctl commands on a local socket\n --control-socket PATH Control socket path (default $XDG_RUNTIME_DIR/horizon-control.sock)\n --server HOST[:PORT] Join a multiplayer server (offline mode) at start\n --name NAME Player name for multiplayer (default: generated once, kept in horizon-player.txt)\n --session-file PATH Signed-in account from the launcher (read once, then deleted)\n\nControls: mouse look, WASD move, Space jump, Left Shift sneak, Left Ctrl sprint,\nleft click break, right click place/use, 1-9/scroll hotbar, E inventory,\nQ drop, F3 debug, Esc pause." ); return Ok(None); } @@ -314,6 +317,15 @@ impl Options { "--smoke-report" => o.report = Some(value.into()), "--screenshot-after" => o.screenshot_after = Some(value.parse()?), "--world" => o.world = Some(value), + "--benchmark" => { + if horizon_client::benchmark::scenes(&value).is_none() { + return Err(format!( + "unknown benchmark {value}: standard, village, forest, ocean or mountain" + ) + .into()); + } + o.benchmark = Some(value); + } "--server" => o.server = Some(value), "--name" => o.player_name = Some(value), "--session-file" => session_file = Some(value), @@ -9607,6 +9619,14 @@ pub fn run() -> Result<()> { player_model: None, }; game.ui.vanilla_root = vanilla_root; + if game.options.benchmark.is_some() { + // The same conditions on every machine: no VSync, no frame cap, no + // pause or idle limit without focus. In memory only, never saved. + game.settings.vsync = false; + game.settings.max_fps = 260; + game.settings.inactivity_fps_limit = "minimized".into(); + game.options.no_pause = true; + } game.apply_settings(); if let Some(path) = &game.options.control { let server = horizon_client::control::Server::start(path) @@ -9614,6 +9634,29 @@ pub fn run() -> Result<()> { log::info!("Horizon: control socket {}", server.path().display()); game.control = Some(server); } + if let Some(scenario) = game.options.benchmark.clone() { + let control = game + .control + .get_or_insert_with(horizon_client::control::Server::local); + horizon_client::benchmark::start( + control.sender(), + horizon_client::benchmark::Setup { + scenario, + started, + app: horizon_bench::App { + name: "horizon-client".into(), + version: horizon_client::crash::version(), + commit: option_env!("HORIZON_COMMIT").map(str::to_owned), + mods: Vec::new(), + }, + simulation_distance: game.settings.simulation_distance as u32, + vsync: false, + fps_cap: None, + offscreen: game.options.offscreen, + game_dir: std::env::current_dir()?, + }, + ); + } if let Ok(spec) = std::env::var("HORIZON_DISPENSE_FIXTURE") { return game.dispense_fixture(&spec); } diff --git a/crates/horizon-client/src/game/control.rs b/crates/horizon-client/src/game/control.rs index 7c476dd61..affa8d464 100644 --- a/crates/horizon-client/src/game/control.rs +++ b/crates/horizon-client/src/game/control.rs @@ -81,6 +81,8 @@ impl Game<'_> { let mut summary = self.frame_stats.summary(); if flight.raw { summary["frame_ms"] = self.frame_stats.frame_millis().into(); + summary["cpu_ms"] = self.frame_stats.cpu_millis().into(); + summary["gpu_ms"] = self.frame_stats.gpu_millis().into(); } let _ = flight.reply.send(summary.to_string()); } @@ -606,6 +608,15 @@ impl Game<'_> { let memory = r.device.generate_allocator_report(); json!({ "gpu": r.info.name, + "vendor": vendor_name(r.info.vendor), + "driver": format!("{} {}", r.info.driver, r.info.driver_info).trim(), + "device_type": match r.info.device_type { + wgpu::DeviceType::IntegratedGpu => "integrated", + wgpu::DeviceType::DiscreteGpu => "discrete", + wgpu::DeviceType::VirtualGpu => "virtual", + wgpu::DeviceType::Cpu => "cpu", + wgpu::DeviceType::Other => "other", + }, "backend": format!("{:?}", r.info.backend), "size": [r.config.width, r.config.height], "present_mode": format!("{:?}", r.config.present_mode), @@ -660,12 +671,33 @@ impl Game<'_> { "generated_chunks": self.streaming.generated_total, "meshed_sections": self.streaming.meshed_total, "renderer": renderer, + "display": self.window.as_ref().and_then(|w| w.current_monitor()).map(|m| json!({ + "width": m.size().width, + "height": m.size().height, + "scale": m.scale_factor(), + "refresh_hz": m.refresh_rate_millihertz().map(|r| r.div_ceil(1000)), + })), "uptime_seconds": self.started.elapsed().as_secs_f64(), "server": self.remote_status(), }) } } +/// PCI vendor IDs of the GPU makers players have (Apple's is wgpu's Metal value). +fn vendor_name(id: u32) -> String { + match id { + 0x10de => "nvidia".into(), + 0x1002 | 0x1022 => "amd".into(), + 0x8086 => "intel".into(), + 0x106b => "apple".into(), + 0x5143 => "qualcomm".into(), + 0x13b5 => "arm".into(), + 0x10005 => "mesa".into(), + 0 => String::new(), + other => format!("{other:#06x}"), + } +} + fn key_code(name: &str) -> Option { let name = name.to_ascii_lowercase(); let letters = [ diff --git a/crates/horizon-client/src/lib.rs b/crates/horizon-client/src/lib.rs index d88d7693e..f8dd9d503 100644 --- a/crates/horizon-client/src/lib.rs +++ b/crates/horizon-client/src/lib.rs @@ -13,6 +13,7 @@ pub mod audio; pub mod auth; pub mod auto_jump; pub mod banner; +pub mod benchmark; pub mod biome; pub mod block_entity_anim; pub mod block_entity_nbt; diff --git a/docs/benchmark.md b/docs/benchmark.md index 2927f7b46..7a9bd5f13 100644 --- a/docs/benchmark.md +++ b/docs/benchmark.md @@ -32,6 +32,18 @@ One game is started per render distance (8, 16 and 32 chunks by default), each f - **World generation is the same algorithm but not the same threads.** Both generate the same terrain (the parity harnesses check it), with their own threading; the work per chunk differs, which is also part of what is being compared. - **Variance.** Flights are short (12 seconds); run each configuration more than once and compare medians before drawing conclusions from small differences. Lows are the noisiest figures. +## Benchmark your own machine + +The game runs the Horizon half of this benchmark by itself, with nothing else installed: + +```sh +horizon-client --benchmark standard --render-distance 16 +``` + +It starts at the title screen, creates the world `horizon-benchmark` (seed 1234, creative; the next run replaces it, and it never touches a world it didn't create), flies the four scenes as above, writes a report to `benchmarks/` in the game folder and quits. `--benchmark village` (or `forest`, `ocean`, `mountain`) flies one scene. For the run only, VSync is off, the frame rate unlimited and the game doesn't pause or slow down without focus; your options are not changed. Your other settings (render distance unless given, graphics, window size) are used as they are, and stored in the report, so compare reports with the same conditions. + +The report is the [benchmark report format](benchmark-report.md): startup to the title screen and to in-world, each scene's chunk load time, chunks generated and section meshes built, every flight's frame figures and histogram, resident and GPU memory, the binary's size, and the machine (OS, CPU, RAM, GPU, driver, backend, display), the load average before the run and whether it ran on battery. It is the same file the telemetry upload would send with your consent; nothing is uploaded by the benchmark itself. + ## Reproduce it You need Horizon built (`cargo build --release -p horizon-client --bins`) and its resources prepared from your own copy of 26.3 (which also provides `.local/minecraft/26.3`, the vanilla client the driver starts), Java 25 with `javac`, and Python 3.10 or newer. From 32695d46804cabb792552724ee74ce751169ff81 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Thu, 15 Oct 2026 03:17:10 +0200 Subject: [PATCH 2/5] benchmark: a load ends when meshing goes quiet too Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/benchmark.rs | 32 +++++++++++++++++++------- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/crates/horizon-client/src/benchmark.rs b/crates/horizon-client/src/benchmark.rs index 7c29bc9cc..7f3464782 100644 --- a/crates/horizon-client/src/benchmark.rs +++ b/crates/horizon-client/src/benchmark.rs @@ -287,27 +287,43 @@ fn poll( Err(format!("not ready after {} s", limit.as_secs())) } -/// Waits until the player is spawned where `near` says, nothing is queued and the resident and -/// generated chunk counts have not changed for a second; returns the time of the last change -/// (`tools/perf_bench.py`'s `wait_loaded`). +/// Fewer section meshes than this in a second is the game remeshing blocks that change (random +/// ticks), not loading. +const QUIET_MESHES_PER_S: f64 = 100.0; + +/// Waits until the player is spawned where `near` says, nothing is queued, the resident and +/// generated chunk counts have not changed for a second and meshing has gone quiet (sections wait +/// for their neighbours' light, so the last ones are drawn after the counts settle); returns when +/// the last of these happened (`tools/perf_bench.py`'s `wait_loaded`). fn wait_loaded(control: &Control, near: impl Fn(&Value) -> bool) -> Result { let deadline = Instant::now() + LOAD_LIMIT; - let (mut last, mut changed) = (None, Instant::now()); + let (mut last, mut changed, mut previous, mut meshing) = + (None, Instant::now(), None, Instant::now()); while Instant::now() < deadline { let s = control.status(); + let now = Instant::now(); let position = &s["player"]["position"]; if position.is_null() || !near(position) || s["pending_chunks"] != 0 || s["screen"] == "title" { - (last, changed) = (None, Instant::now()); + (last, changed, previous, meshing) = (None, now, None, now); } else { let counts = (s["chunks"].as_u64(), s["generated_chunks"].as_u64()); if last != Some(counts) { - (last, changed) = (Some(counts), Instant::now()); - } else if changed.elapsed() >= Duration::from_secs(1) { - return Ok(changed); + (last, changed) = (Some(counts), now); + } + let meshed = s["meshed_sections"].as_f64().unwrap_or(0.0); + if let Some((at, before)) = previous { + if meshed - before > QUIET_MESHES_PER_S * (now - at).as_secs_f64() { + meshing = now; + } + } + previous = Some((now, meshed)); + let done = changed.max(meshing); + if now - done >= Duration::from_secs(1) { + return Ok(done); } } thread::sleep(Duration::from_millis(50)); From 0f61ed6b637ddca1da38a3be793ac644d0e0d281 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Thu, 15 Oct 2026 03:29:56 +0200 Subject: [PATCH 3/5] benchmark: Apple GPUs by name (Metal has no vendor ID) Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/benchmark.rs | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/crates/horizon-client/src/benchmark.rs b/crates/horizon-client/src/benchmark.rs index 7f3464782..00b0f1076 100644 --- a/crates/horizon-client/src/benchmark.rs +++ b/crates/horizon-client/src/benchmark.rs @@ -132,7 +132,11 @@ fn run(control: &Control, setup: Setup) -> Result { let renderer = &title["renderer"]; report.hardware.gpu = Some(horizon_bench::Gpu { name: text(&renderer["gpu"]), - vendor: text(&renderer["vendor"]), + vendor: match text(&renderer["vendor"]) { + // Metal reports no vendor ID. + v if v.is_empty() && text(&renderer["gpu"]).starts_with("Apple") => "apple".into(), + v => v, + }, driver: text(&renderer["driver"]), backend: text(&renderer["backend"]).to_lowercase(), kind: text(&renderer["device_type"]), @@ -297,8 +301,8 @@ const QUIET_MESHES_PER_S: f64 = 100.0; /// the last of these happened (`tools/perf_bench.py`'s `wait_loaded`). fn wait_loaded(control: &Control, near: impl Fn(&Value) -> bool) -> Result { let deadline = Instant::now() + LOAD_LIMIT; - let (mut last, mut changed, mut previous, mut meshing) = - (None, Instant::now(), None, Instant::now()); + let (mut last, mut changed, mut meshing) = (None, Instant::now(), Instant::now()); + let mut previous: Option<(Instant, f64)> = None; while Instant::now() < deadline { let s = control.status(); let now = Instant::now(); @@ -315,10 +319,10 @@ fn wait_loaded(control: &Control, near: impl Fn(&Value) -> bool) -> Result QUIET_MESHES_PER_S * (now - at).as_secs_f64() { - meshing = now; - } + if let Some((at, before)) = previous + && meshed - before > QUIET_MESHES_PER_S * (now - at).as_secs_f64() + { + meshing = now; } previous = Some((now, meshed)); let done = changed.max(meshing); From 014b1f52678f377b8c1158f308c7323cc0b46b4f Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Thu, 15 Oct 2026 03:47:11 +0200 Subject: [PATCH 4/5] Benchmark reports go to the telemetry outbox, with the benchmark-upload consent only Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/benchmark.rs | 1 + crates/horizon-client/src/telemetry.rs | 47 ++++++++++++++++++++++++++ docs/benchmark.md | 2 +- 3 files changed, 49 insertions(+), 1 deletion(-) diff --git a/crates/horizon-client/src/benchmark.rs b/crates/horizon-client/src/benchmark.rs index 00b0f1076..05e559914 100644 --- a/crates/horizon-client/src/benchmark.rs +++ b/crates/horizon-client/src/benchmark.rs @@ -255,6 +255,7 @@ fn run(control: &Control, setup: Setup) -> Result { if let Ok(meta) = std::env::current_exe().and_then(std::fs::metadata) { report.metric("game.binary_bytes", &[meta.len() as f64]); } + crate::telemetry::keep_benchmark(&crate::logging::game_home(), &report); report .write_to(&setup.game_dir.join("benchmarks")) .map_err(|e| e.to_string()) diff --git a/crates/horizon-client/src/telemetry.rs b/crates/horizon-client/src/telemetry.rs index 5abc3bd89..9e83655fa 100644 --- a/crates/horizon-client/src/telemetry.rs +++ b/crates/horizon-client/src/telemetry.rs @@ -105,6 +105,20 @@ pub fn keep_crash(home: &Path, report: &crate::crash::Report) { } } +/// Keeps a benchmark report (`--benchmark`) for the launcher to send, if the player allows +/// benchmark uploads: the report as docs/benchmark-report.md defines it, dated to the day. +pub fn keep_benchmark(home: &Path, report: &horizon_bench::Report) { + let Ok(data) = serde_json::to_value(report.for_upload()) else { + return; + }; + if let Err(e) = outbox::push( + home, + &Item::new(Source::Game, version(), Payload::Benchmark(data)), + ) { + log::warn!("Horizon: could not keep the benchmark report to send: {e}"); + } +} + /// One play session's performance, from entering a world to leaving it: frame and tick /// times as [`Timings`] (fixed size whatever the length), never the frames themselves. #[derive(Default)] @@ -303,6 +317,39 @@ mod tests { assert!(crash.stack.lines().count() > 100); } + #[test] + fn benchmark_reports_need_their_own_consent_and_go_dated_to_the_day() { + let home = home("benchmark"); + let mut report = horizon_bench::Report::new(horizon_bench::Kind::Game, Default::default()); + report.created = "2026-10-15T14:03:27Z".into(); + report.label = Some("my machine".into()); + keep_benchmark(&home, &report); + allow( + &home, + Choices { + performance: true, + ..Choices::NONE + }, + ); + keep_benchmark(&home, &report); + assert!(outbox::pending(&home).is_empty()); + allow( + &home, + Choices { + benchmarks: true, + ..Choices::NONE + }, + ); + keep_benchmark(&home, &report); + let pending = outbox::pending(&home); + assert_eq!(pending.len(), 1); + let text = serde_json::to_string(&pending[0].1).unwrap(); + assert!( + text.contains("2026-10-15T00:00:00Z") && !text.contains("my machine"), + "{text}" + ); + } + #[test] fn crashes_stay_local_without_consent() { let home = home("crash"); diff --git a/docs/benchmark.md b/docs/benchmark.md index 7a9bd5f13..6c5c35f8d 100644 --- a/docs/benchmark.md +++ b/docs/benchmark.md @@ -42,7 +42,7 @@ horizon-client --benchmark standard --render-distance 16 It starts at the title screen, creates the world `horizon-benchmark` (seed 1234, creative; the next run replaces it, and it never touches a world it didn't create), flies the four scenes as above, writes a report to `benchmarks/` in the game folder and quits. `--benchmark village` (or `forest`, `ocean`, `mountain`) flies one scene. For the run only, VSync is off, the frame rate unlimited and the game doesn't pause or slow down without focus; your options are not changed. Your other settings (render distance unless given, graphics, window size) are used as they are, and stored in the report, so compare reports with the same conditions. -The report is the [benchmark report format](benchmark-report.md): startup to the title screen and to in-world, each scene's chunk load time, chunks generated and section meshes built, every flight's frame figures and histogram, resident and GPU memory, the binary's size, and the machine (OS, CPU, RAM, GPU, driver, backend, display), the load average before the run and whether it ran on battery. It is the same file the telemetry upload would send with your consent; nothing is uploaded by the benchmark itself. +The report is the [benchmark report format](benchmark-report.md): startup to the title screen and to in-world, each scene's chunk load time, chunks generated and section meshes built, every flight's frame figures and histogram, resident and GPU memory, the binary's size, and the machine (OS, CPU, RAM, GPU, driver, backend, display), the load average before the run and whether it ran on battery. If you allowed benchmark uploads (the launcher's data collection choices, [docs/telemetry.md](telemetry.md)), the same report, dated to the day and without local fields, is kept for the launcher to send; otherwise it stays on your machine. ## Reproduce it From 146c0fd3ce2ca6975d6d474c6a9b4a4cb97bd39d Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Thu, 15 Oct 2026 05:06:27 +0200 Subject: [PATCH 5/5] benchmark: a load ends when every streaming stage is idle Same rule as perf_bench: with status.streaming, the last sections are drawn once no stage has held work for half a second, so the remeshing that follows a load (fluids settling, random ticks) isn't counted as loading. Without it, meshing still has to go quiet. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/benchmark.rs | 35 ++++++++++++++++++-------- 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/crates/horizon-client/src/benchmark.rs b/crates/horizon-client/src/benchmark.rs index 05e559914..09989b8c9 100644 --- a/crates/horizon-client/src/benchmark.rs +++ b/crates/horizon-client/src/benchmark.rs @@ -297,9 +297,11 @@ fn poll( const QUIET_MESHES_PER_S: f64 = 100.0; /// Waits until the player is spawned where `near` says, nothing is queued, the resident and -/// generated chunk counts have not changed for a second and meshing has gone quiet (sections wait -/// for their neighbours' light, so the last ones are drawn after the counts settle); returns when -/// the last of these happened (`tools/perf_bench.py`'s `wait_loaded`). +/// generated chunk counts have not changed for a second and the last sections are drawn (they wait +/// for their neighbours' light, so after the counts settle); returns when the last of these +/// happened (`tools/perf_bench.py`'s `wait_loaded`). With `status.streaming`, the sections are +/// drawn once no stage has held work for half a second: blocks changing after the load (fluids +/// settling, random ticks) only remesh in bursts. Without it, meshing has to go quiet. fn wait_loaded(control: &Control, near: impl Fn(&Value) -> bool) -> Result { let deadline = Instant::now() + LOAD_LIMIT; let (mut last, mut changed, mut meshing) = (None, Instant::now(), Instant::now()); @@ -320,15 +322,26 @@ fn wait_loaded(control: &Control, near: impl Fn(&Value) -> bool) -> Result QUIET_MESHES_PER_S * (now - at).as_secs_f64() - { - meshing = now; - } + let quiet = if let Some(stages) = s["streaming"].as_object() { + // Protos outside the view distance wait for neighbours that are never generated. + if stages + .iter() + .any(|(k, v)| k != "protos" && v.as_u64().unwrap_or(0) > 0) + { + meshing = now; + } + Duration::from_millis(500) + } else { + if let Some((at, before)) = previous + && meshed - before > QUIET_MESHES_PER_S * (now - at).as_secs_f64() + { + meshing = now; + } + Duration::from_secs(1) + }; previous = Some((now, meshed)); - let done = changed.max(meshing); - if now - done >= Duration::from_secs(1) { - return Ok(done); + if now - changed >= Duration::from_secs(1) && now - meshing >= quiet { + return Ok(changed.max(meshing)); } } thread::sleep(Duration::from_millis(50));