// SPDX-FileCopyrightText: 2026 Paula Wisnievski // SPDX-License-Identifier: AGPL-3.0-or-later // the render benchmark's results and how they're written out: a table to read, then the same as json, which // tools/compare_benchmarks.ts reads to put runs side by side // changes when the scenes or the level change, since runs of different versions don't compare export const BENCHMARK_VERSION = 2; // how fast and how smooth frames were, from the time between them. the lows are the fps of the frame that 99% (and // 99.9%) of the others were faster than export interface FrameStats { frames: number; fps: number; median_fps: number; low_1_fps: number; low_01_fps: number; p99_ms: number; max_ms: number; } export interface TimeStats { mean_ms: number; p99_ms: number; } export interface SceneResult { // frames as the display paces them paced?: FrameStats; // frames back to back, see render_debug.uncapped. only how many, the time between them says nothing since they // come in bursts between the display's refreshes when the gpu is behind uncapped_fps?: number; // the main thread's and the gpu's time per frame while paced. the gpu's only with timestamp queries cpu?: TimeStats; gpu?: TimeStats; // what the first paced frame drew, which runs of the same version should about agree on draw_calls: number; quads: number; sections_in_view: number; entities_in_view: number; } // meshing every chunk of the level that can be export interface MeshingResult { chunks: number; ms: number; paced?: FrameStats; } export interface SystemInfo { browser: string; os: string; user_agent: string; cpu_threads: number; // only chromium says, rounded down to a power of two and at most 8 memory_gb?: number; gpu: string; gpu_features: string[]; // whether the gpu times are there timestamps: boolean; canvas_format: string; // the median fps where nothing is drawn, which is the display's refresh when the browser holds frames to it refresh_hz?: number; device_pixel_ratio: number; // the browser's, the desktop app doesn't know it screen?: string; // meshing workers, see ChunkWorkerPool workers: number; } export interface BenchmarkReport { version: number; date: string; system: SystemInfo; // "id version" mods: string[]; resolution: string; // the f3 window's switch, see ClientLevel.use_render_regions render_regions?: boolean; level: { chunks: number; build_ms: number }; first_mesh: MeshingResult; remesh: MeshingResult; scenes: Record; } export function frame_stats(intervals: number[]): FrameStats | undefined { if (intervals.length === 0) return undefined; const sorted = [...intervals].sort((a, b) => a - b); const mean = sorted.reduce((sum, ms) => sum + ms, 0) / sorted.length; return { frames: sorted.length, fps: 1000 / mean, median_fps: 1000 / percentile(sorted, 0.5), low_1_fps: 1000 / percentile(sorted, 0.99), low_01_fps: 1000 / percentile(sorted, 0.999), p99_ms: percentile(sorted, 0.99), max_ms: sorted[sorted.length - 1], }; } export function time_stats(times: number[]): TimeStats | undefined { if (times.length === 0) return undefined; const sorted = [...times].sort((a, b) => a - b); return { mean_ms: sorted.reduce((sum, ms) => sum + ms, 0) / sorted.length, p99_ms: percentile(sorted, 0.99) }; } // the smallest value that at least q of them are at or under function percentile(sorted: number[], q: number) { return sorted[Math.min(sorted.length - 1, Math.max(0, Math.ceil(q * sorted.length) - 1))]; } // markdown, to paste where it's read, with the whole report as json at the end export function format_report(report: BenchmarkReport): string { const { system, first_mesh, remesh } = report; const fps = (value?: number) => value === undefined ? "-" : value.toFixed(1); const ms = (value?: number) => value === undefined ? "-" : value.toFixed(2); const seconds = (ms: number) => `${(ms / 1000).toFixed(2)} s`; const meshing = ({ chunks, ms }: MeshingResult) => `${seconds(ms)} (${Math.round(chunks / ms * 1000)} chunks/s)`; const memory = system.memory_gb ? `, ${system.memory_gb} GB` : ""; const refresh = system.refresh_hz ? `, ${system.refresh_hz} Hz` : ""; const lines = [ `## cubi render benchmark ${report.version}`, "", `${system.browser} on ${system.os}, ${system.gpu}`, `${system.cpu_threads} threads${memory}${refresh}, drawn at ${report.resolution}, ${system.workers} mesh workers` + (system.timestamps ? "" : ", no gpu timestamps") + (report.render_regions ? ", render regions" : ""), `${report.date}, mods: ${report.mods.join(", ") || "none"}`, "", "| scene | fps | 1% low | 0.1% low | uncapped fps | cpu ms | gpu ms | draw calls | quads |", "|---|---:|---:|---:|---:|---:|---:|---:|---:|", ]; for (const [name, scene] of Object.entries(report.scenes)) { const { paced } = scene; lines.push( `| ${name} | ${fps(paced?.fps)} | ${fps(paced?.low_1_fps)} | ${fps(paced?.low_01_fps)} | ` + `${fps(scene.uncapped_fps)} | ${ms(scene.cpu?.mean_ms)} | ${ms(scene.gpu?.mean_ms)} | ` + `${scene.draw_calls} | ${scene.quads.toLocaleString("en")} |`, ); } lines.push( "", `Meshing the level's ${first_mesh.chunks} chunks took ${meshing(first_mesh)} the first time and ` + `${meshing(remesh)} again. Building it took ${seconds(report.level.build_ms)} of javascript.`, "", "```json", JSON.stringify(report, (_, value) => typeof value === "number" ? Math.round(value * 100) / 100 : value), "```", ); return lines.join("\n"); } // the report in what format_report wrote, or json on its own export function parse_report(text: string): BenchmarkReport { const json = text.match(/```json\n([\s\S]*?)\n```/)?.[1] ?? text; return JSON.parse(json); }