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
|
//! 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] [--addon LIB]
//! [--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",
addon: []const u8 = bench_config.pdf_addon,
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], "--addon")) config.addon = 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, config.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);
}
|