diff options
Diffstat (limited to 'test/pdf_scroll_bench.zig')
| -rw-r--r-- | test/pdf_scroll_bench.zig | 153 |
1 files changed, 85 insertions, 68 deletions
diff --git a/test/pdf_scroll_bench.zig b/test/pdf_scroll_bench.zig index 33dfe70a..0f93068d 100644 --- a/test/pdf_scroll_bench.zig +++ b/test/pdf_scroll_bench.zig @@ -4,6 +4,7 @@ //! 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 //! zig build pdf-scroll-bench -Doptimize=Debug -- --reps 1 the diagnosis //! zig build pdf-scroll-bench -Dtracy=~/05-genizah/tracy the zones //! @@ -42,6 +43,11 @@ //! //! 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 drainEffects = bench_util.drainEffects; const libc = std.c; const pardes = @import("pardes"); const pdf = @import("mupdf"); @@ -57,7 +63,7 @@ const gpa = std.heap.smp_allocator; // is what the tty shell actually ships. const bench_cols: u16 = 120; const bench_rows: u16 = 40; -const bench_cell_pixels: pardes.CellPixels = .{ .w = 8, .h = 16 }; +const default_cell_pixels: pardes.CellPixels = .{ .w = 8, .h = 16 }; /// config.wheel_rows is one cell per notch, so a notch is `cell_pixels.h` /// display pixels. 192 notches is ~3072px — between two and three fit-width @@ -69,7 +75,7 @@ const slow_ticks_per_frame: usize = 1; /// The control row is a fixed wall of frames rather than a whole-document /// traversal: one notch per frame over a real document is tens of thousands of /// frames, and 240 already crosses several page boundaries at reading speed. -const slow_frames: usize = 240; +const default_slow_frames: usize = 240; /// Guard against a pathological document turning one row into a multi-minute /// run. Deterministic either way: the frame count is derived from layout. const max_frames: usize = 2048; @@ -86,6 +92,10 @@ const Config = struct { /// Override `max_frames` for one run: a 5000-page manual is 2000 frames of /// travel per round, which is a fine scoreboard and a poor edit-run loop. frames: ?usize = null, + /// One cell in display pixels: 16x32 is the same window on a 2x display. + cell: pardes.CellPixels = default_cell_pixels, + /// Frames of the one-notch control row. + slow_frames: usize = default_slow_frames, }; /// The bit-exact description of one scrolling run. `visual` is the invariant; @@ -115,6 +125,9 @@ const Result = struct { total_ns: u64, rasterizations: u64, rasters_retained: u64, + /// Most bytes the pane's rasters held after any frame: every slot's + /// pixels, its clean copy, and the spare buffers kept for reuse. + peak_raster_bytes: u64, identity: Identity, }; @@ -169,13 +182,13 @@ pub fn main(init: std.process.Init) !void { // One untimed probe boot supplies the strip height every plan is sized // from, so the frame counts below are a property of the document and the // viewport rather than of the machine. - const travel = probeTravel(config.path); + const travel = probeTravel(config); const cap = config.frames orelse max_frames; const results = [_]Result{ - try measure(config, planFor(.fling_down, travel, cap)), - try measure(config, planFor(.fling_reverse, travel, cap)), - try measure(config, planFor(.slow_scroll, travel, cap)), + try measure(config, planFor(.fling_down, travel, cap, config.slow_frames)), + try measure(config, planFor(.fling_reverse, travel, cap, config.slow_frames)), + try measure(config, planFor(.slow_scroll, travel, cap, config.slow_frames)), }; if (config.json) @@ -202,23 +215,25 @@ fn parseArgs(args: []const []const u8) Config { } else if (std.mem.eql(u8, args[i], "--frames") and i + 1 < args.len) { i += 1; config.frames = parseCount("--frames", args[i]); + } else if (std.mem.eql(u8, args[i], "--slow-frames") and i + 1 < args.len) { + i += 1; + config.slow_frames = parseCount("--slow-frames", args[i]); + } else if (std.mem.eql(u8, args[i], "--cell") and i + 1 < args.len) { + i += 1; + var it = std.mem.splitScalar(u8, args[i], 'x'); + const w = parseCount("--cell", it.next() orelse ""); + const h = parseCount("--cell", it.next() orelse ""); + if (w > std.math.maxInt(u16) or h > std.math.maxInt(u16)) fatal("--cell {s} is too large", .{args[i]}); + config.cell = .{ .w = @intCast(w), .h = @intCast(h) }; } else fatal( "usage: pardes-pdf-scroll-bench [--json] [--warmup N] [--reps N]" ++ - " [--path FILE] [--frames N]", + " [--path FILE] [--frames N] [--slow-frames N] [--cell WxH]", .{}, ); } return config; } -fn parseCount(flag: []const u8, text: []const u8) usize { - const value = std.fmt.parseUnsigned(usize, text, 10) catch - fatal("{s} expects a positive integer", .{flag}); - if (value == 0 or value > 10_000) - fatal("{s} must be in 1..10000", .{flag}); - return value; -} - const Travel = struct { /// Scrollable display pixels: the whole strip minus one screenful. max_scroll: f64, @@ -229,28 +244,35 @@ const Travel = struct { /// same benchmark and the report must not pretend otherwise. pages: usize, strip_px: u64, + /// The first frame of a fresh pane: opening the document and rasterizing + /// what it shows first. One sample, from the probe boot. + first_frame_ns: u64, }; -fn probeTravel(path: []const u8) Travel { - const core = bootCore(path) catch |err| +fn probeTravel(config: Config) Travel { + const path = config.path; + const core = bootCore(config) catch |err| fatal("cannot boot a PDF pane on {s}: {t}", .{ path, err }); defer core.deinit(); var frame_arena = std.heap.ArenaAllocator.init(gpa); defer frame_arena.deinit(); + const started = nowNs(); _ = core.render(frame_arena.allocator()) catch |err| fatal("first render failed: {t}", .{err}); drainEffects(core); + const first_frame_ns = nowNs() -| started; const pane = core.panes[0] orelse fatal("PDF pane disappeared", .{}); if (pane.pdf == null) fatal("{s} did not open as a PDF", .{path}); const pv = &pane.pdf.?; - if (pv.page_count < 4) - fatal("{s} has {d} pages; a fling benchmark needs at least 4", .{ path, pv.page_count }); if (!pv.layout_valid or pv.document_height == 0) fatal("the pixel strip never laid out; is native_images/cell_pixels wired?", .{}); // Exactly the clamp scrollPdfDocument enforces, so the last fling frame // lands on the document end instead of adding a no-op frame to the row. - const viewport_h: u64 = @as(u64, bench_rows -| pardes.BOX_H) * bench_cell_pixels.h; - const notch: f64 = @floatFromInt(bench_cell_pixels.h); + const viewport_h: u64 = @as(u64, bench_rows -| pardes.BOX_H) * config.cell.h; + // Four screenfuls of strip, in pages or in one tall page. + if (pv.document_height < 4 * viewport_h) + fatal("{s} is {d}px of strip; a fling benchmark needs at least four screenfuls", .{ path, pv.document_height }); + const notch: f64 = @floatFromInt(config.cell.h); if (!std.math.isFinite(notch) or notch <= 0) fatal("a notch of {d} display pixels cannot size a fling", .{notch}); const strip = @as(f64, @floatFromInt(pv.document_height -| viewport_h)); @@ -261,6 +283,7 @@ fn probeTravel(path: []const u8) Travel { .per_frame = notch * @as(f64, @floatFromInt(fling_ticks_per_frame)), .pages = pv.page_count, .strip_px = pv.document_height, + .first_frame_ns = first_frame_ns, }; } @@ -270,7 +293,7 @@ fn probeTravel(path: []const u8) Travel { /// The cap is applied before any arithmetic on the count, so a document the /// float division cannot describe still produces a runnable plan instead of an /// overflow. -fn planFor(scenario: Scenario, travel: Travel, cap: usize) Plan { +fn planFor(scenario: Scenario, travel: Travel, cap: usize, slow: usize) Plan { const wanted = travel.max_scroll / travel.per_frame; const whole: usize = if (std.math.isFinite(wanted) and wanted >= 1) @intFromFloat(@min(@as(f64, @floatFromInt(cap)), @ceil(wanted))) @@ -285,15 +308,15 @@ fn planFor(scenario: Scenario, travel: Travel, cap: usize) Plan { .frames = @max(2, down), .down_frames = @max(1, down / 2), }, - .slow_scroll => .{ .scenario = scenario, .frames = slow_frames, .down_frames = slow_frames }, + .slow_scroll => .{ .scenario = scenario, .frames = slow, .down_frames = slow }, }; } /// A pane on a real multi-page document with the native pixel-strip path live /// and a genuine pixel viewport delivered the way a shell delivers one. -fn bootCore(path: []const u8) !*pardes.Pardes { +fn bootCore(config: Config) !*pardes.Pardes { const core = try pardes.Pardes.init(gpa, .{ - .file = path, + .file = config.path, .cols = bench_cols, .rows = bench_rows, }); @@ -302,7 +325,7 @@ fn bootCore(path: []const u8) !*pardes.Pardes { core.update(.{ .resize = .{ .cols = bench_cols, .rows = bench_rows, - .cell_pixels = bench_cell_pixels, + .cell_pixels = config.cell, } }); drainEffects(core); return core; @@ -321,9 +344,10 @@ fn measure(config: Config, plan: Plan) !Result { var expected: ?Identity = null; var rasterizations: u64 = 0; var rasters_retained: u64 = 0; + var peak_raster_bytes: u64 = 0; for (0..config.warmup + config.reps) |round| { - const core = try bootCore(config.path); + const core = try bootCore(config); defer core.deinit(); var frame_arena = std.heap.ArenaAllocator.init(gpa); defer frame_arena.deinit(); @@ -358,6 +382,7 @@ fn measure(config: Config, plan: Plan) !Result { const elapsed = nowNs() -| started; total += elapsed; recorder.observe(frame, surface); + peak_raster_bytes = @max(peak_raster_bytes, rasterBytes(pv)); // The same frame boundary src/tty/tty.zig marks, so a Tracy capture // of this benchmark reads like a capture of the real shell. pardes.frameMark(); @@ -391,10 +416,19 @@ fn measure(config: Config, plan: Plan) !Result { .total_ns = totals[totals.len / 2], .rasterizations = rasterizations, .rasters_retained = rasters_retained, + .peak_raster_bytes = peak_raster_bytes, .identity = expected.?, }; } +/// What the pane's rasters hold right now (untimed: outside the frame). +fn rasterBytes(pv: anytype) u64 { + var total: u64 = 0; + for (pv.rasters[0..pv.rasters_len]) |raster| total += raster.rgba.len + raster.clean.len; + for (pv.spare[0..pv.spare_len]) |spare| total += spare.len; + return total; +} + /// Accumulates the two checksums across one run. Raster generations are /// rebased on the counter's value at run start so the trace describes the /// ORDER textures were produced in rather than absolute counter values, which @@ -441,7 +475,7 @@ const Recorder = struct { self.visual = mix(self.visual, place.w); self.visual = mix(self.visual, place.h); self.visual = mix(self.visual, place.native.page); - self.visual = mix(self.visual, @intFromEnum(place.native.fit)); + self.visual = mix(self.visual, @backingInt(place.native.fit)); self.visual = mix(self.visual, place.native.pan_x); self.visual = mix(self.visual, place.native.pan_y); self.visual = mix(self.visual, @as(u32, @bitCast(place.native.pixel_offset_y))); @@ -520,20 +554,6 @@ fn stridedHash(seed: u64, rgba: []const u8) u64 { return hash; } -fn mix(seed: u64, value: anytype) u64 { - const Value = @TypeOf(value); - const Stable = switch (@typeInfo(Value)) { - .comptime_int => u64, - else => Value, - }; - var stable: Stable = value; - return std.hash.Wyhash.hash(seed, std.mem.asBytes(&stable)); -} - -fn drainEffects(core: *pardes.Pardes) void { - while (core.nextEffect()) |_| {} -} - fn verifyIdentity( name: []const u8, expected: *?Identity, @@ -557,35 +577,29 @@ fn verifyIdentity( } else expected.* = got; } -fn nowNs() u64 { - var ts: std.c.timespec = undefined; - _ = std.c.clock_gettime(.MONOTONIC, &ts); - return @as(u64, @intCast(ts.sec)) *| 1_000_000_000 +| - @as(u64, @intCast(ts.nsec)); -} - fn reportText(config: Config, travel: Travel, results: []const Result) void { std.debug.print("pardes PDF fast-scroll benchmark ({s})\n", .{bench_config.core_optimize}); std.debug.print( "{s}: {d} pages, {d}px strip at {d}x{d} cells of {d}x{d}px\n", .{ - config.path, travel.pages, travel.strip_px, bench_cols, - bench_rows, bench_cell_pixels.w, bench_cell_pixels.h, + config.path, travel.pages, travel.strip_px, bench_cols, + bench_rows, config.cell.w, config.cell.h, }, ); + std.debug.print("first frame of a fresh pane: {d}us\n", .{travel.first_frame_ns / 1000}); std.debug.print( "warmup: {d}, sampled reps: {d}; every number is ONE FRAME (notch batch + render + drain)\n\n", .{ config.warmup, config.reps }, ); - std.debug.print("{s:<16} {s:>6} {s:>7} {s:>10} {s:>10} {s:>10} {s:>10} {s:>10} {s:>11} {s:>7} {s:>5} {s:<16} {s}\n", .{ - "scenario", "frames", "notches", "min", - "median", "p90", "p99", "max", - "total_us", "rasters", "kept", "visual", - "transport", + std.debug.print("{s:<16} {s:>6} {s:>7} {s:>10} {s:>10} {s:>10} {s:>10} {s:>10} {s:>11} {s:>7} {s:>5} {s:>9} {s:<16} {s}\n", .{ + "scenario", "frames", "notches", "min", + "median", "p90", "p99", "max", + "total_us", "rasters", "kept", "peak_kib", + "visual", "transport", }); - std.debug.print("{s}\n", .{"-" ** 160}); + std.debug.print("{s}\n", .{&@as([160]u8, @splat('-'))}); for (results) |result| std.debug.print( - "{s:<16} {d:>6} {d:>7} {d:>10} {d:>10} {d:>10} {d:>10} {d:>10} {d:>11} {d:>7} {d:>5} {x:0>16} {x:0>16}\n", + "{s:<16} {d:>6} {d:>7} {d:>10} {d:>10} {d:>10} {d:>10} {d:>10} {d:>11} {d:>7} {d:>5} {d:>9} {x:0>16} {x:0>16}\n", .{ result.name, result.frames, @@ -598,12 +612,14 @@ fn reportText(config: Config, travel: Travel, results: []const Result) void { result.total_ns / 1000, result.rasterizations, result.rasters_retained, + result.peak_raster_bytes / 1024, result.identity.visual, result.identity.transport, }, ); std.debug.print( - "\n`rasters` counts MuPDF page rasterizations over one run; `kept` is the retained raster count at the end.\n" ++ + "\n`rasters` counts MuPDF page rasterizations over one run; `kept` is the retained raster count at the end;\n" ++ + "`peak_kib` is the most the pane's rasters, clean copies and spares held after any frame.\n" ++ "`visual` pins presented pixels and placement and must never change; `transport` traces texture uploads.\n", .{}, ); @@ -616,16 +632,17 @@ fn reportJson(io: std.Io, config: Config, travel: Travel, results: []const Resul var out: std.Io.Writer = .fixed(&storage); out.print( "{{\"benchmark\":\"pardes-pdf-scroll\",\"build\":\"{s}\",\"path\":\"{s}\"," ++ - "\"pages\":{d},\"strip_px\":{d},\"warmup\":{d},\"reps\":{d},\"results\":[", + "\"pages\":{d},\"strip_px\":{d},\"first_frame_ns\":{d},\"warmup\":{d},\"reps\":{d},\"results\":[", .{ - bench_config.core_optimize, config.path, travel.pages, - travel.strip_px, config.warmup, config.reps, + bench_config.core_optimize, config.path, travel.pages, + travel.strip_px, travel.first_frame_ns, config.warmup, + config.reps, }, ) catch return; for (results, 0..) |result, 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},\"rasters_retained\":{d},\"images\":{d}," ++ + "\"rasterizations\":{d},\"rasters_retained\":{d},\"peak_raster_bytes\":{d},\"images\":{d}," ++ "\"visual\":\"{x:0>16}\",\"transport\":\"{x:0>16}\"}}", .{ if (i == 0) "" else ",", @@ -640,6 +657,7 @@ fn reportJson(io: std.Io, config: Config, travel: Travel, results: []const Resul result.total_ns, result.rasterizations, result.rasters_retained, + result.peak_raster_bytes, result.identity.images, result.identity.visual, result.identity.transport, @@ -649,13 +667,12 @@ fn reportJson(io: std.Io, config: Config, travel: Travel, results: []const Resul std.Io.File.stdout().writeStreamingAll(io, out.buffered()) catch {}; } -fn fatal(comptime format: []const u8, args: anytype) noreturn { - std.debug.print("pdf-scroll-bench: " ++ format ++ "\n", args); - std.process.exit(1); -} - comptime { // The bench links the real MuPDF wrapper the core uses; keep the import // load-bearing so a build that silently dropped it fails here. _ = pdf.Document; } + +fn parseCount(flag: []const u8, text: []const u8) usize { + return bench_util.parsePositive(fatal, flag, text, 10_000); +} |
