summaryrefslogtreecommitdiff
path: root/test/pdf_scroll_bench.zig
blob: bb4640f7f013975297c63601f6946e560879ee23 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
//! Scoreboard for FAST scrolling of a PDF's continuous strip: a pane of the
//! pdf addon (addons/pdf/pdf.zig), 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 -- --addon other/pdf.so   another build's
//!   zig build pdf-scroll-bench -Doptimize=ReleaseFast -- --turn 0.999   a touchpad's turns, not notches
//!   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",
    addon: []const u8 = bench_config.pdf_addon,
    /// Each wheel event's turn: 0, a whole notch (a terminal's; a kind may
    /// glide it), else a touchpad's fraction, moved at once.
    turn: f32 = 0,
    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] [--addon LIB] [--turn F] [--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, "--addon")) config.addon = v else if (std.mem.eql(u8, a, "--turn")) {
            config.turn = std.fmt.parseFloat(f32, v) catch fatal("--turn {s}: not a number", .{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, settled_ns: u64 };

/// When bootCore's Look began, and when its first frame showed a page's
/// pixels (the first frame); `settled` is its end, the page ahead landed too.
var first_page: u64 = 0;
/// 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, config.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();
    first_page = 0;
    for (0..4) |_| {
        core.update(.tick);
        // A frame as each job lands (a host's), until the first page is shown.
        while (true) {
            drain(core);
            _ = arena.reset(.retain_capacity);
            const shown = try core.render(arena.allocator());
            for (shown.placements.items) |pl| if (pl.raster.w > 1 and first_page == 0) {
                first_page = nowNs() -| look_started;
            };
            if (first_page != 0 or running.items.len == 0) break;
        }
        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 settled_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_page, .settled_ns = settled_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, .amount = config.turn } });
            _ = 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 (its first page shown): {d}us, the page ahead landed too: {d}us; every number is ONE FRAME (notches + render + drain)\n", .{ travel.first_frame_ns / 1000, travel.settled_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},\"settled_ns\":{d},\"warmup\":{d},\"reps\":{d},\"results\":[", .{ bench_config.core_optimize, config.path, travel.pages, travel.max_scroll, travel.first_frame_ns, travel.settled_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 {};
}