//! Scoreboard for FAST scrolling of a PDF's continuous strip: a pane of the //! pdf addon (addons/pdf/pdf.c), driven through the core as a shell drives //! it. //! //! zig build pdf-scroll-bench -Doptimize=ReleaseFast the numbers //! zig build pdf-scroll-bench -Doptimize=ReleaseFast -- --json //! zig build pdf-scroll-bench -Doptimize=ReleaseFast -- --warmup 1 --reps 15 //! zig build pdf-scroll-bench -Doptimize=ReleaseFast -- --path other.pdf //! zig build pdf-scroll-bench -Doptimize=ReleaseFast -- --cell 16x32 a 2x display //! //! A shell pump drains every input the OS queued since the last present and //! then renders once, so a trackpad fling is a BATCH of wheel notches in one //! frame. Every timed region is that: `ticks_per_frame` wheel presses //! through `Pardes.update`, one `render()` into a reset arena, and the //! effect drain, the page jobs the addon asked for run there and then (a //! shell's workers run them beside the frame; here they are in it, as the //! core's own PDF code rendered on the loop before the addon). Rows are //! per-FRAME latency. //! //! `visual` hashes what the reader sees (every placement's rectangles and //! the rows of its pixels shown) and must be the same every round. //! Setup, teardown, checksumming and reporting are outside every timer. const std = @import("std"); const bench_util = @import("bench_util.zig"); const nowNs = bench_util.nowNs; const fatal = bench_util.Fatal("pdf-scroll-bench").f; const mix = bench_util.mix; const pardes = @import("pardes"); const bench_config = @import("pdf_bench_config"); pub const std_options: std.Options = .{ .log_level = .err }; const gpa = std.heap.smp_allocator; // A reading-sized window: fit-width pages taller than the viewport, so a // fling crosses page boundaries. const bench_cols: u16 = 120; const bench_rows: u16 = 40; const default_cell_pixels: pardes.CellPixels = .{ .w = 8, .h = 16 }; /// 192 notches of a cell each, ~3072px: two to three pages in a frame. const fling_ticks_per_frame: usize = 192; const slow_ticks_per_frame: usize = 1; const default_slow_frames: usize = 240; const max_frames: usize = 2048; const Config = struct { path: []const u8 = "docs/design.pdf", json: bool = false, warmup: usize = 2, reps: usize = 25, frames: ?usize = null, cell: pardes.CellPixels = default_cell_pixels, slow_frames: usize = default_slow_frames, }; const Result = struct { name: []const u8, ticks_per_frame: usize, frames: usize, min_ns: u64, median_ns: u64, p90_ns: u64, p99_ns: u64, max_ns: u64, total_ns: u64, /// Rasters the frames showed (distinct keys): pages rendered. rasterizations: u64, images: u64, visual: u64, }; const Scenario = enum { fling_down, fling_reverse, slow_scroll, fn ticks(scenario: Scenario) usize { return if (scenario == .slow_scroll) slow_ticks_per_frame else fling_ticks_per_frame; } }; const Plan = struct { scenario: Scenario, frames: usize, down_frames: usize }; pub fn main(init: std.process.Init) !void { if (!bench_config.release_fast_core) std.debug.print( "WARNING: the core is {s}, not ReleaseFast — these timings are for reading a profile, NOT for the scoreboard\n", .{bench_config.core_optimize}, ); const args = try init.minimal.args.toSlice(init.arena.allocator()); const config = parseArgs(args); workers = std.c.getenv("BENCH_WORKERS") != null; const travel = probeTravel(config); const cap = config.frames orelse max_frames; const per_frame = @as(f64, @floatFromInt(config.cell.h)) * fling_ticks_per_frame; const wanted: usize = @intFromFloat(@min(@as(f64, @floatFromInt(cap)), @ceil(travel.max_scroll / per_frame))); const down = @max(1, @min(cap, wanted)); const results = [_]Result{ try measure(config, .{ .scenario = .fling_down, .frames = down, .down_frames = down }), try measure(config, .{ .scenario = .fling_reverse, .frames = @max(2, down), .down_frames = @max(1, down / 2) }), try measure(config, .{ .scenario = .slow_scroll, .frames = config.slow_frames, .down_frames = config.slow_frames }), }; if (config.json) reportJson(init.io, config, travel, &results) else reportText(config, travel, &results); } fn parseArgs(args: []const []const u8) Config { var config: Config = .{}; var i: usize = 1; while (i < args.len) : (i += 1) { const a = args[i]; if (std.mem.eql(u8, a, "--json")) { config.json = true; continue; } if (i + 1 >= args.len) fatal("usage: pardes-pdf-scroll-bench [--json] [--warmup N] [--reps N] [--path FILE] [--frames N] [--slow-frames N] [--cell WxH]", .{}); i += 1; const v = args[i]; if (std.mem.eql(u8, a, "--warmup")) config.warmup = parseCount(a, v) else if (std.mem.eql(u8, a, "--reps")) config.reps = parseCount(a, v) else if (std.mem.eql(u8, a, "--path")) config.path = v else if (std.mem.eql(u8, a, "--frames")) config.frames = parseCount(a, v) else if (std.mem.eql(u8, a, "--slow-frames")) config.slow_frames = parseCount(a, v) else if (std.mem.eql(u8, a, "--cell")) { var it = std.mem.splitScalar(u8, v, 'x'); config.cell = .{ .w = @intCast(parseCount(a, it.next() orelse "")), .h = @intCast(parseCount(a, it.next() orelse "")) }; } else fatal("unknown flag {s}", .{a}); } return config; } fn parseCount(flag: []const u8, text: []const u8) usize { return bench_util.parsePositive(fatal, flag, text, 10_000); } /// Its jobs: run here, at once; or (BENCH_WORKERS set) on threads of /// their own as a shell's workers run them, each landing at the first /// drain after it is done, the frame never waiting on one. fn drain(core: *pardes.Pardes) void { if (!workers) { while (core.nextEffect()) |e| if (e == .job) core.perform(e); return; } var i: usize = 0; while (i < running.items.len) { const r = running.items[i]; if (!r.done.load(.acquire)) { i += 1; continue; } r.thread.join(); r.job.land(core); gpa.destroy(r); _ = running.swapRemove(i); } while (core.nextEffect()) |e| if (e == .job) { const j = pardes.host_io.KindJob.take(core, e.job.pane, e.job.serial, e.job.token) orelse continue; const r = gpa.create(Running) catch continue; r.* = .{ .job = j, .thread = undefined }; r.thread = std.Thread.spawn(.{}, Running.run, .{r}) catch { gpa.destroy(r); j.run(); j.land(core); continue; }; running.append(gpa, r) catch {}; }; } const Running = struct { job: *pardes.host_io.KindJob, thread: std.Thread, done: std.atomic.Value(bool) = .init(false), fn run(r: *Running) void { r.job.run(); r.done.store(true, .release); } }; var running: std.ArrayList(*Running) = .empty; var workers = false; /// Every job still out, landed: before its core goes. fn settle(core: *pardes.Pardes) void { while (running.items.len > 0) drain(core); } const Travel = struct { max_scroll: f64, pages: u32, first_frame_ns: u64 }; /// When bootCore's Look began: the first frame is from there to the first /// page landed (the document opened, sized and drawn). var look_started: u64 = 0; /// A core showing `config.path` as the addon's pane, laid out and its /// first page landed. fn bootCore(config: Config) !*pardes.Pardes { const core = try pardes.Pardes.init(gpa, .{ .cols = bench_cols, .rows = bench_rows, .tty_only = true }); errdefer core.deinit(); core.view().frontend.native_images = true; core.update(.{ .resize = .{ .cols = bench_cols, .rows = bench_rows, .cell_pixels = config.cell } }); pardes.Addon.perform(core, .load, 0, bench_config.pdf_addon); drain(core); var line: [4200]u8 = undefined; const real = try std.Io.Dir.cwd().realPathFileAlloc(std.Io.Threaded.global_single_threaded.io(), config.path, gpa); defer gpa.free(real); look_started = nowNs(); if (!core.executeBuiltinLine(core.view().active, std.fmt.bufPrint(&line, "Look {s}", .{real}) catch unreachable)) return error.NoLook; // The text pane it was looked from goes: the PDF alone in the window. const pdf = core.view().active; for (core.panes, 0..) |slot, id| if (slot != null and id != pdf) try core.removePane(id, null); core.view().active = pdf; drain(core); var arena = std.heap.ArenaAllocator.init(gpa); defer arena.deinit(); for (0..4) |_| { _ = arena.reset(.retain_capacity); _ = try core.render(arena.allocator()); core.update(.tick); drain(core); settle(core); } if ((core.panes[pdf] orelse return error.NoPane).addon == null) return error.NotAPdf; return core; } fn pdfPane(core: *pardes.Pardes) *pardes.Pane { return core.panes[core.view().active].?; } fn probeTravel(config: Config) Travel { const core = bootCore(config) catch |err| fatal("cannot open {s} as a PDF pane: {t}", .{ config.path, err }); defer core.deinit(); defer settle(core); const first_frame_ns = nowNs() -| look_started; const slot = pdfPane(core).addon.?; if (slot.thumb_span <= 0) fatal("{s}: the strip never laid out", .{config.path}); // The strip is the window over the share of it shown. const window_px = @as(f64, @floatFromInt(pardes.Kind.viewOf(core, core.live, core.view().active).rows)) * @as(f64, @floatFromInt(config.cell.h)); var pages: u32 = 0; const prefix = slot.prefix.slice(); if (std.mem.indexOfScalar(u8, prefix, '/')) |slash| pages = std.fmt.parseInt(u32, prefix[slash + 1 .. prefix.len - 1], 10) catch 0; return .{ .max_scroll = window_px / slot.thumb_span - window_px, .pages = pages, .first_frame_ns = first_frame_ns }; } fn measure(config: Config, plan: Plan) !Result { const ticks = plan.scenario.ticks(); const pool = try gpa.alloc(u64, config.reps * plan.frames); defer gpa.free(pool); const totals = try gpa.alloc(u64, config.reps); defer gpa.free(totals); var n: usize = 0; var expected: ?u64 = null; var rasters: u64 = 0; var images: u64 = 0; for (0..config.warmup + config.reps) |round| { const core = try bootCore(config); defer core.deinit(); defer settle(core); var arena = std.heap.ArenaAllocator.init(gpa); defer arena.deinit(); const id = core.view().active; const rect = core.view().rects[id]; var keys: std.AutoHashMap(u64, void) = .init(gpa); defer keys.deinit(); var visual: u64 = 0x7061_7264_6573_5343; var shown: u64 = 0; var total: u64 = 0; for (0..plan.frames) |frame| { const button: pardes.Mouse.Button = if (plan.scenario == .fling_reverse and frame >= plan.down_frames) .wheel_up else .wheel_down; const started = nowNs(); for (0..ticks) |_| core.update(.{ .mouse = .{ .button = button, .kind = .press, .col = rect.x + rect.w / 2, .row = rect.y + rect.h / 2 } }); _ = arena.reset(.retain_capacity); const surface = try core.render(arena.allocator()); drain(core); const elapsed = nowNs() -| started; total += elapsed; for (surface.placements.items) |pl| { shown += 1; try keys.put(pl.key, {}); visual = mix(visual, [_]u64{ pl.dst.x, pl.dst.y, pl.dst.w, pl.dst.h, pl.src.x, pl.src.y, pl.src.w, pl.src.h }); if (pl.at) |at| visual = mix(visual, [_]u64{ at.x, at.y, at.w, at.h }); const bytes = pl.raster.bytes(); const stride: usize = pl.raster.stride; var row: u32 = 0; while (row < pl.src.h and (@as(usize, pl.src.y) + row + 1) * stride <= bytes.len) : (row += 7) visual = std.hash.Wyhash.hash(visual, bytes[(@as(usize, pl.src.y) + row) * stride ..][0..stride]); } if (round >= config.warmup) { pool[n] = @max(1, elapsed); n += 1; } } if (expected) |want| { if (want != visual and !workers) return error.UnstableVisual; } else expected = visual; if (round >= config.warmup) { totals[round - config.warmup] = total; rasters = keys.count(); images = shown; } } std.mem.sort(u64, pool, {}, std.sort.asc(u64)); std.mem.sort(u64, totals, {}, std.sort.asc(u64)); return .{ .name = @tagName(plan.scenario), .ticks_per_frame = ticks, .frames = plan.frames, .min_ns = pool[0], .median_ns = pool[pool.len / 2], .p90_ns = pool[(pool.len * 9) / 10 -| 1], .p99_ns = pool[(pool.len * 99) / 100 -| 1], .max_ns = pool[pool.len - 1], .total_ns = totals[totals.len / 2], .rasterizations = rasters, .images = images, .visual = expected.?, }; } fn reportText(config: Config, travel: Travel, results: []const Result) void { std.debug.print("pardes PDF fast-scroll benchmark ({s}): {s}, {d} pages, {d:.0}px of travel, cells {d}x{d}px\n", .{ bench_config.core_optimize, config.path, travel.pages, travel.max_scroll, config.cell.w, config.cell.h }); std.debug.print("first frame of a fresh pane: {d}us; every number is ONE FRAME (notches + render + drain)\n", .{travel.first_frame_ns / 1000}); std.debug.print("{s:<14} {s:>6} {s:>7} {s:>10} {s:>10} {s:>10} {s:>10} {s:>10} {s:>8} {s}\n", .{ "scenario", "frames", "notches", "min", "median", "p90", "p99", "max", "rasters", "visual" }); for (results) |r| std.debug.print("{s:<14} {d:>6} {d:>7} {d:>10} {d:>10} {d:>10} {d:>10} {d:>10} {d:>8} {x:0>16}\n", .{ r.name, r.frames, r.ticks_per_frame, r.min_ns, r.median_ns, r.p90_ns, r.p99_ns, r.max_ns, r.rasterizations, r.visual }); } fn reportJson(io: std.Io, config: Config, travel: Travel, results: []const Result) void { var storage: [16 * 1024]u8 = undefined; var out: std.Io.Writer = .fixed(&storage); out.print("{{\"benchmark\":\"pardes-pdf-scroll\",\"build\":\"{s}\",\"path\":\"{s}\",\"pages\":{d},\"strip_px\":{d:.0},\"first_frame_ns\":{d},\"warmup\":{d},\"reps\":{d},\"results\":[", .{ bench_config.core_optimize, config.path, travel.pages, travel.max_scroll, travel.first_frame_ns, config.warmup, config.reps }) catch return; for (results, 0..) |r, i| out.print("{s}{{\"scenario\":\"{s}\",\"frames\":{d},\"notches_per_frame\":{d},\"min_ns\":{d},\"median_ns\":{d},\"p90_ns\":{d},\"p99_ns\":{d},\"max_ns\":{d},\"total_ns\":{d},\"rasterizations\":{d},\"images\":{d},\"visual\":\"{x:0>16}\"}}", .{ if (i == 0) "" else ",", r.name, r.frames, r.ticks_per_frame, r.min_ns, r.median_ns, r.p90_ns, r.p99_ns, r.max_ns, r.total_ns, r.rasterizations, r.images, r.visual }) catch return; out.writeAll("]}\n") catch return; std.Io.File.stdout().writeStreamingAll(io, out.buffered()) catch {}; }