//! Pointer gestures over a pane of the pdf addon: hovering words (the Look //! preview highlight), and dragging a selection to its release. //! //! zig build pdf-pointer-bench -Doptimize=ReleaseFast -- [--label L] [--reps N] [--path FILE] //! [--cell WxH] [--scroll NOTCHES] //! //! Every frame is timed the way a shell pump spends it: the event through //! `Pardes.update`, one `render()` into a reset arena, the effect drain (the //! addon's page jobs run there). A hover step is its motion frame plus the //! tick frames, 16 ms apart, until the kind asks for no more: its word's //! highlight lands on one. Stdout is CSV, `label,scenario:kind,round,ns`, a //! row a frame and one a step (`kind=step`). Stderr says how many raster //! rows the hover uploaded (a frontend holding a picture patches the rows a //! placement says changed, `from`; any other picture is uploaded whole). //! //! `visual` hashes every presented raster in full, with its placement, on //! the frame each step settles on: the same every round. const std = @import("std"); const bench_util = @import("bench_util.zig"); const nowNs = bench_util.nowNs; 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; const cols: u16 = 120; const rows: u16 = 40; const Config = struct { path: []const u8 = "docs/design.pdf", label: []const u8 = "run", reps: usize = 5, /// Wheel notches before the gestures, off the sparse title page. scroll: usize = 40, /// 8x16 is the Kitty policy's cell; the SDL shell's is closer to 16x35. cell_w: u16 = 8, cell_h: u16 = 16, }; pub fn main(init: std.process.Init) !void { const args = try init.minimal.args.toSlice(init.arena.allocator()); var config: Config = .{}; var i: usize = 1; while (i + 1 < args.len) : (i += 2) { const value = args[i + 1]; if (std.mem.eql(u8, args[i], "--path")) config.path = value else if (std.mem.eql(u8, args[i], "--label")) config.label = value else if (std.mem.eql(u8, args[i], "--reps")) config.reps = try std.fmt.parseUnsigned(usize, value, 10) else if (std.mem.eql(u8, args[i], "--scroll")) config.scroll = try std.fmt.parseUnsigned(usize, value, 10) else if (std.mem.eql(u8, args[i], "--cell")) { var it = std.mem.splitScalar(u8, value, 'x'); config.cell_w = try std.fmt.parseUnsigned(u16, it.next() orelse "", 10); config.cell_h = try std.fmt.parseUnsigned(u16, it.next() orelse "", 10); } else return error.UnknownOption; } if (i != args.len) return error.MissingValue; var visual: ?u64 = null; for (0..config.reps) |round| { const got = try measure(config, round); if (visual) |want| if (want != got) { std.debug.print("pdf-pointer-bench: visual changed between rounds: {x} vs {x}\n", .{ want, got }); return error.Unstable; }; visual = got; } std.debug.print("pdf-pointer-bench {s}: visual={x:0>16}\n", .{ config.label, visual.? }); } const Run = struct { core: *pardes.Pardes, arena: std.heap.ArenaAllocator, config: Config, round: usize, visual: u64 = 0x7061_7264_6573_5050, step_ns: u64 = 0, frames: usize = 0, steps: usize = 0, last: ?*const pardes.Surface = null, now: u64 = std.time.ns_per_s, held: [64]u64 = undefined, nheld: usize = 0, hover_rows: u64 = 0, hover_uploads: u64 = 0, page_rows: u64 = 0, fn frame(run: *Run, scenario: []const u8, kind: []const u8, event: pardes.Event) !void { const started = nowNs(); run.core.update(event); drain(run.core); _ = run.arena.reset(.retain_capacity); const surface = try run.core.render(run.arena.allocator()); drain(run.core); const elapsed = nowNs() - started; run.step_ns += elapsed; run.frames += 1; print("{s},{s}:{s},{d},{d}\n", .{ run.config.label, scenario, kind, run.round, elapsed }); run.last = surface; run.uploads(scenario, surface); } /// The step, then the tick frames its animation asks for (a hover's /// preview arrives on one of them), as a pump with nothing else queued. fn step(run: *Run, scenario: []const u8, kind: []const u8, event: pardes.Event) !void { run.step_ns = 0; try run.frame(scenario, kind, event); var ticks: usize = 0; while (pardes.Kind.wake(run.core) != null and ticks < 32) : (ticks += 1) { run.now += 16 * std.time.ns_per_ms; run.core.advance(run.now); try run.frame(scenario, "tick", .tick); } print("{s},{s}:step,{d},{d}\n", .{ run.config.label, scenario, run.round, run.step_ns }); // Only the settled frame is the invariant: how many frames a preview // takes to arrive is a setting (look_preview_delay_frames). run.observe(run.last.?); run.steps += 1; } /// The rows a frontend uploads for this frame: a key it held last /// frame none, a patch of a key it held its dirty rows, else every row. fn uploads(run: *Run, scenario: []const u8, surface: *const pardes.Surface) void { var next: [64]u64 = undefined; var n: usize = 0; for (surface.placements.items) |place| { const held = std.mem.indexOfScalar(u64, run.held[0..run.nheld], place.key) != null; const up: u64 = if (held) 0 else if (place.dirty) |d| (if (std.mem.indexOfScalar(u64, run.held[0..run.nheld], d.from) != null) d.h else place.raster.h) else place.raster.h; if (std.mem.eql(u8, scenario, "hover")) { run.hover_rows += up; if (up > 0) run.hover_uploads += 1; run.page_rows = @max(run.page_rows, place.raster.h); } if (n < next.len) { next[n] = place.key; n += 1; } } @memcpy(run.held[0..n], next[0..n]); run.nheld = n; } fn observe(run: *Run, surface: *const pardes.Surface) void { for (surface.placements.items) |place| { run.visual = mix(run.visual, [_]u64{ place.pane, place.dst.x, place.dst.y, place.dst.w, place.dst.h, place.raster.w, place.raster.h }); const g = place.src; run.visual = mix(run.visual, [_]u64{ g.x, g.y, g.w, g.h }); if (place.at) |at| run.visual = mix(run.visual, [_]u64{ at.x, at.y, at.w, at.h }); run.visual = std.hash.Wyhash.hash(run.visual, place.raster.bytes()); } } }; /// Its jobs run here, at once, as a shell's workers would. fn drain(core: *pardes.Pardes) void { while (core.nextEffect()) |e| if (e == .job) core.perform(e); } fn measure(config: Config, round: usize) !u64 { const core = try pardes.Pardes.init(gpa, .{ .cols = cols, .rows = rows, .tty_only = true }); defer core.deinit(); core.view().frontend.native_images = true; var run: Run = .{ .core = core, .arena = .init(gpa), .config = config, .round = round }; defer run.arena.deinit(); const cell: pardes.CellPixels = .{ .w = config.cell_w, .h = config.cell_h }; core.advance(run.now); core.update(.{ .resize = .{ .cols = cols, .rows = rows, .cell_pixels = cell } }); pardes.Addon.perform(core, .load, 0, bench_config.pdf_addon); drain(core); const real = try std.Io.Dir.cwd().realPathFileAlloc(std.Io.Threaded.global_single_threaded.io(), config.path, gpa); defer gpa.free(real); var line: [4200]u8 = undefined; if (!core.executeBuiltinLine(core.view().active, try std.fmt.bufPrint(&line, "Look {s}", .{real}))) return error.NoLook; const id = core.view().active; for (core.panes, 0..) |slot, other| if (slot != null and other != id) try core.removePane(other, null); core.view().active = id; drain(core); for (0..4) |_| { _ = try core.render(run.arena.allocator()); core.update(.tick); drain(core); } const pane = core.panes[id] orelse return error.NoPane; if (pane.addon == null) return error.NotPdf; const rect = core.view().rects[id]; const mid_col = rect.x + rect.w / 2; const mid_row = rect.y + rect.h / 2; for (0..config.scroll) |_| { core.update(.{ .mouse = .{ .button = .wheel_down, .kind = .press, .col = mid_col, .row = mid_row } }); drain(core); _ = run.arena.reset(.retain_capacity); _ = try core.render(run.arena.allocator()); drain(core); } const top = rect.y + 4; const bottom = rect.y + rect.h -| 3; const left = rect.x + 4; const right = rect.x + rect.w -| 4; // Hover: walk four rows of the body, a word-ish stride at a time. var row: u16 = top; while (row < bottom) : (row += (bottom - top) / 4) { var col: u16 = left; while (col < right) : (col += 3) try run.step("hover", "motion", .{ .mouse = .{ .button = .none, .kind = .motion, .col = col, .row = row } }); } // Leave the page so the last preview is withdrawn like a real exit. try run.step("hover", "leave", .pointer_leave); // Select: press, drag across three lines, release; five times down the page. row = top; while (row + 3 < bottom) : (row += (bottom - top) / 5) { try run.step("select", "press", .{ .mouse = .{ .button = .left, .kind = .press, .col = left, .row = row } }); var n: u16 = 1; while (n <= 24) : (n += 1) { const col = left + (right - left) * (n % 8) / 8; try run.step("select", "drag", .{ .mouse = .{ .button = .left, .kind = .drag, .col = col, .row = row + n / 8 } }); } try run.step("select", "release", .{ .mouse = .{ .button = .left, .kind = .release, .col = right, .row = row + 3 } }); } std.debug.print("pdf-pointer-bench uploads: hover {d} rows in {d} uploads over {d} steps (a page is {d} rows)\n", .{ run.hover_rows, run.hover_uploads, run.steps, run.page_rows }); return mix(run.visual, @as(u64, run.steps)); } fn print(comptime format: []const u8, args: anytype) void { var buf: [256]u8 = undefined; const line = std.fmt.bufPrint(&buf, format, args) catch return; _ = std.c.write(1, line.ptr, line.len); }