diff options
Diffstat (limited to 'test/perf.zig')
| -rw-r--r-- | test/perf.zig | 261 |
1 files changed, 256 insertions, 5 deletions
diff --git a/test/perf.zig b/test/perf.zig index 4ba061e1..8e622680 100644 --- a/test/perf.zig +++ b/test/perf.zig @@ -70,6 +70,31 @@ const Op = enum { /// the window: a kind's frame costs its window's cells, never its /// picture (docs: architecture ยง16.1, its trigger 5% of a frame). image_frame, + /// The walk UI's tag states (TagState.zig), each key pumped and the + /// frame that draws it: SPC struck at rest, its level shown when its + /// delay is up, Esc back to rest (three frames, two keys). + spc, + /// A Down in SPC's unrolled level: its cursor a row on, the band drawn. + spc_down, + /// A character typed in Help's field and a Backspace: its rows + /// filtered again each time (two keys). + help_type, + /// describe-key's `g d` replayed: the level and the leaf's doc under the + /// tag (two keys). + describe, + /// `j` and `k` at rest in the same loop (two keys, two frames): what + /// the tag states' keys compare with (key-down's frames each draw + /// another place of the file). + rest_keys, + + /// Run only when named (`--op`): a full run, its cells and the sums a + /// gate takes over them (perfops' "terminal on all") are as before. + fn named(o: Op) bool { + return switch (o) { + .spc, .spc_down, .help_type, .describe, .rest_keys => true, + else => false, + }; + } fn label(o: Op) []const u8 { return switch (o) { @@ -88,6 +113,11 @@ const Op = enum { .body_appends => "body-appends", .deep_selection => "deep-sel", .image_frame => "image-frame", + .spc => "spc", + .spc_down => "spc-down", + .help_type => "help-type", + .describe => "describe", + .rest_keys => "rest-keys", }; } }; @@ -394,6 +424,10 @@ const Options = struct { mini: bool = false, /// The scans a frame's slice bounds, a call at a time (`measureSearch`). search: bool = false, + /// A picker's key and frame (`measurePick`). + pick: bool = false, + /// The walk UI's line F: registers, painted targets, themes (`measureWalkF`). + walkf: bool = false, terminal_mib: ?usize = null, /// The real binary's keystroke-to-present, `yes` stream and big open /// (a tty pardes), or its keystrokes alone (a pardes-gui). @@ -410,7 +444,7 @@ const Options = struct { while (i < args.len) : (i += 1) { const arg = args[i]; if (!std.mem.startsWith(u8, arg, "--")) return error.UnknownOption; - const option = std.meta.stringToEnum(enum { json, creation, startup, mini, search, @"terminal-mib", @"self-test", reps, cols, rows, base, only, op, @"max-ratio", live, @"live-gui" }, arg[2..]) orelse + const option = std.meta.stringToEnum(enum { json, creation, startup, mini, search, pick, walkf, @"terminal-mib", @"self-test", reps, cols, rows, base, only, op, @"max-ratio", live, @"live-gui" }, arg[2..]) orelse return error.UnknownOption; switch (option) { .json => opts.json = true, @@ -418,6 +452,8 @@ const Options = struct { .startup => opts.startup = true, .mini => opts.mini = true, .search => opts.search = true, + .pick => opts.pick = true, + .walkf => opts.walkf = true, .@"self-test" => opts.self_test = true, else => { i += 1; @@ -480,6 +516,8 @@ pub fn main(init: std.process.Init) !void { if (opts.terminal_mib) |mib| return measureTerminalSession(init.io, mib, reps, opts.json); if (opts.mini) return measureMini(init.io, init.gpa, reps, opts.json); if (opts.search) return measureSearch(init.io); + if (opts.pick) return measurePick(init.io); + if (opts.walkf) return measureWalkF(init.io); if (opts.live) |exe| return measureLive(init.io, exe, false, reps, opts.json); if (opts.live_gui) |exe| return measureLive(init.io, exe, true, reps, opts.json); // The image-frame cells load the image addon by the path the build baked @@ -524,6 +562,7 @@ pub fn main(init: std.process.Init) !void { for (fixtures, 0..) |fx, fi| { if (only) |name| if (!std.mem.eql(u8, name, fx.name)) continue; if (opts.op) |o| if (o != op) continue; + if (op.named() and opts.op == null) continue; cells[oi][fi] = try measure(op, fx, paths[fi], bytes[fi], reps); } } @@ -538,7 +577,7 @@ pub fn main(init: std.process.Init) !void { } } - if (opts.json) try reportJson(init.io, &cells, &term_cells, &bytes, opts) else reportText(&cells, &term_cells, &bytes, reps, opts.base, base); + if (opts.json) try reportJson(init.io, &cells, &term_cells, &bytes, opts) else reportText(&cells, &term_cells, &bytes, reps, opts.op, opts.base, base); if (opts.max_ratio) |ratio| try gate(&cells, &base.?, ratio); } @@ -583,6 +622,7 @@ fn measure(op: Op, fx: Fixture, path: []const u8, expected_bytes: usize, reps: u switch (op) { .image_frame => return measureImage(std.fs.path.dirname(path).?, samples, warmup), .client_key_down, .client_edit_char, .client_delete_undo => return measureClient(op, path, reps), + .spc, .spc_down, .help_type, .describe, .rest_keys => return measureTag(op, path, reps), else => {}, } if (op == .open) { @@ -637,7 +677,7 @@ fn measure(op: Op, fx: Fixture, path: []const u8, expected_bytes: usize, reps: u const wheel_row = rect.y + pardes.BOX_H; const t0 = nowNs(); switch (op) { - .open, .image_frame, .client_key_down, .client_edit_char, .client_delete_undo => unreachable, + .open, .image_frame, .client_key_down, .client_edit_char, .client_delete_undo, .spc, .spc_down, .help_type, .describe, .rest_keys => unreachable, .render, .deep_selection => {}, .key_down => core.update(.{ .key = .{ .cp = 'j', .text = "j" } }), .key_right => core.update(.{ .key = .{ .cp = 'l', .text = "l" } }), @@ -651,7 +691,7 @@ fn measure(op: Op, fx: Fixture, path: []const u8, expected_bytes: usize, reps: u _ = try frame(core); const dt = nowNs() -| t0; switch (op) { - .open, .image_frame, .client_key_down, .client_edit_char, .client_delete_undo => unreachable, + .open, .image_frame, .client_key_down, .client_edit_char, .client_delete_undo, .spc, .spc_down, .help_type, .describe, .rest_keys => unreachable, .body_write => if (pane.file.?.content.len != before.len + 8 * body_chunk.len) return error.BodyWriteWasNotAppended, .body_appends => if (pane.file.?.content.len != before.len + append_count * append_chunk_len) return error.BodyAppendsWereNotAppended, .render, .deep_selection => if (pane.body.cur_row != before.row or pane.body.cur_col != before.col or pane.file.?.content.len != before.len) @@ -736,6 +776,68 @@ fn clientKey(a: *pardes.replica.Authority, r: *pardes.replica.Replica, ev: parde r.settle(); } +/// A tag state's keys on the fixture's pane, each pumped and its frame +/// drawn, from the state the op starts in (entered untimed); the state +/// checked after each repetition. +fn measureTag(op: Op, path: []const u8, reps: usize) !Cell { + const Key = pardes.Key; + const warmup = 3; + const samples = try gpa.alloc(u64, reps); + defer gpa.free(samples); + const core = try boot(); + defer core.deinit(); + pardes.look.lookAt(core, 0, path); + pump(core); + const id = filePane(core) orelse return error.OpenDidNotOpenFile; + core.view().active = id; + const enter: []const Key = switch (op) { + .spc, .rest_keys => &.{}, + .spc_down => &.{ .{ .cp = ' ', .text = " " }, .{ .cp = Key.down } }, + .help_type => &.{ .{ .cp = ' ', .text = " " }, .{ .cp = '?', .text = "?" } }, + .describe => &.{ .{ .cp = ' ', .text = " " }, .{ .cp = 'h', .text = "h" }, .{ .cp = 'k', .text = "k" } }, + else => unreachable, + }; + for (enter) |k| core.update(.{ .key = k }); + pump(core); + _ = try frame(core); + const keys: []const Key = switch (op) { + .spc => &.{ .{ .cp = ' ', .text = " " }, .{ .cp = Key.escape } }, + .spc_down => &.{.{ .cp = Key.down }}, + .help_type => &.{ .{ .cp = 'e', .text = "e" }, .{ .cp = Key.backspace } }, + .describe => &.{ .{ .cp = 'g', .text = "g" }, .{ .cp = 'd', .text = "d" } }, + .rest_keys => &.{ .{ .cp = 'j', .text = "j" }, .{ .cp = 'k', .text = "k" } }, + else => unreachable, + }; + const s = &core.view().tag_state; + for (0..warmup + samples.len) |n| { + const t0 = nowNs(); + for (keys) |k| { + core.update(.{ .key = k }); + pump(core); + _ = try frame(core); + // SPC's level when its delay is up: the frame that shows it. + if (op == .spc and k.cp == ' ') { + core.advance(core.clock.now_ns); + core.clock.now_ns = s.due_ns; + if (!pardes.TagState.shown(core, core.view())) return error.LevelNotShown; + _ = try frame(core); + } + } + const dt = nowNs() -| t0; + const held = switch (op) { + .spc => s.kind == .rest and s.root == null, + .spc_down => s.unrolled and core.panes[id].?.band_rows > 0, + .help_type => s.kind == .help and s.field.len == 0 and s.nhits > 0, + .describe => s.kind == .describe and s.found, + .rest_keys => s.kind == .rest and s.root == null and core.panes[id].?.body.cur_row == 0, + else => unreachable, + }; + if (!held) return error.TagStateLost; + if (n >= warmup) samples[n - warmup] = dt / 1000; + } + return summarize(samples); +} + fn measureImage(dir: []const u8, samples: []u64, warmup: usize) !Cell { const path = try std.fmt.allocPrint(gpa, "{s}/picture.ppm", .{dir}); defer gpa.free(path); @@ -1856,6 +1958,152 @@ fn measureSearch(io: std.Io) !void { }; } +/// `--pick`: a picker's hot paths (core/Pick.zig, walk UI line D), each +/// a step and the frame that draws it, timed and its instructions counted +/// (user space, this thread): a key typed into a Find of 50,000 paths and +/// the frames after it while its names are scored; a cursor move over rows +/// that are files, and the frame after it that loads the preview into the +/// landing pane's body (a 2,000-line file read from the disk). +fn measurePick(io: std.Io) !void { + var random: [12]u8 = undefined; + io.random(&random); + const dir = try std.fmt.allocPrint(gpa, "/tmp/pardes-pick-{x}", .{random}); + defer gpa.free(dir); + try std.Io.Dir.createDirAbsolute(io, dir, .default_dir); + defer std.Io.Dir.cwd().deleteTree(io, dir) catch {}; + var counter: Instructions = try .open(); + defer counter.close(); + var body: std.ArrayList(u8) = .empty; + defer body.deinit(gpa); + for (0..2000) |i| try body.print(gpa, " const value_{d} = compute(value_{d}, \"a line of the preview\");\n", .{ i, i -| 1 }); + var name: [160]u8 = undefined; + for (0..20) |i| try writeFile(try std.fmt.bufPrint(&name, "{s}/pickme_{d}.zig", .{ dir, i }), body.items); + const start = try std.fmt.allocPrint(gpa, "{s}/start.zig", .{dir}); + defer gpa.free(start); + try writeFile(start, "const start = 1;\n"); + const core = try boot(); + defer core.deinit(); + core.settings.jumps = .walk; + pardes.look.lookAt(core, 0, start); + pump(core); + const id = filePane(core) orelse return error.OpenDidNotOpenFile; + pardes.Pick.open(core, id, .find, "", "", ""); + const pk = pardes.Pick.live(core) orelse return error.NoPicker; + // The listing as the disk engine would give it: 50,000 paths, the + // real files among them. + _ = core.stream.stop(); + pk.names.reset(); + const dirs = [_][]const u8{ "core", "dev/gui", "dev/tui", "runtime", "test/behaviour", "addons/pdf", "zig-pkg/ghostty/src/terminal" }; + const words = [_][]const u8{ "layout", "places", "names", "query", "stream", "surface", "replica", "tape", "kitty", "render" }; + for (0..50_000) |i| { + const path = if (i % 2500 == 0) try std.fmt.bufPrint(&name, "pickme_{d}.zig", .{i / 2500}) else try std.fmt.bufPrint(&name, "{s}/module_{d}/{s}_{d}.zig", .{ dirs[i % dirs.len], i / 64 % 997, words[i * 7 % words.len], i }); + try pk.names.add(gpa, path, 0, path.len, @TypeOf(pk.names).baseOf(path), 0); + } + pk.names.typed(gpa, ""); + while (pk.names.busy()) pk.names.rescore(gpa); + try frame(core); + const Step = struct { + fn run(c: *pardes.Pardes, ev: pardes.Event, n: *Instructions, s: *Slowest, hits: usize) !void { + const was = n.read(); + const t0 = nowNs(); + c.update(ev); + try frame(c); + s.add(nowNs() -| t0, n.read() -| was, hits); + } + }; + var keys: Slowest = .{}; + var after: Slowest = .{}; + for ("pickme") |ch| { + try Step.run(core, .{ .key = .{ .cp = ch, .text = &[_]u8{ch} } }, &counter, &keys, pk.names.hits.items.len); + while (pk.names.busy()) try Step.run(core, .tick, &counter, &after, pk.names.hits.items.len); + } + keys.report("pick key + frame", "pickme", pk.names.hits.items.len); + after.report("pick tick + frame while scoring", "pickme", pk.names.hits.items.len); + // The first preview, then the cursor moved and the preview after it. + try Step.run(core, .tick, &counter, &after, 0); + var moves: Slowest = .{}; + var previews: Slowest = .{}; + for (0..19) |_| { + try Step.run(core, .{ .key = .{ .cp = pardes.Key.down } }, &counter, &moves, 0); + try Step.run(core, .tick, &counter, &previews, 0); + } + moves.report("pick Down + frame", "pickme", pk.names.hits.items.len); + previews.report("pick preview tick + frame", "pickme", pk.names.hits.items.len); + const landing = core.panes[core.paneBySerial(pk.landing) orelse return error.NoLanding].?; + if (!std.mem.endsWith(u8, landing.file.?.path, ".zig") or std.mem.indexOf(u8, landing.file.?.path, "pickme_") == null) return error.NoPreview; +} + +/// `--walkf`: line F's hot paths in a 2,000-line file, each a step and +/// the frame that draws it, its instructions counted: a key in the +/// registers' level unrolled (Down, its value step read again); a frame +/// with `]`'s targets painted against one at rest; a cursor move in the +/// themes picker (the window wearing the next theme). +fn measureWalkF(io: std.Io) !void { + var random: [12]u8 = undefined; + io.random(&random); + const dir = try std.fmt.allocPrint(gpa, "/tmp/pardes-walkf-{x}", .{random}); + defer gpa.free(dir); + try std.Io.Dir.createDirAbsolute(io, dir, .default_dir); + defer std.Io.Dir.cwd().deleteTree(io, dir) catch {}; + const path = try std.fmt.allocPrint(gpa, "{s}/walkf.zig", .{dir}); + defer gpa.free(path); + var body: std.ArrayList(u8) = .empty; + defer body.deinit(gpa); + for (0..250) |i| try body.print(gpa, "/// The {d}th.\nfn f{d}(a: u32) u32 {{\n const b = a + {d};\n return b;\n}}\n\ntest \"f{d}\" {{\n}}\n", .{ i, i, i, i }); + try writeFile(path, body.items); + var counter: Instructions = try .open(); + defer counter.close(); + const core = try boot(); + defer core.deinit(); + core.settings.jumps = .walk; + pardes.look.lookAt(core, 0, path); + pump(core); + _ = filePane(core) orelse return error.OpenDidNotOpenFile; + for ("abcdefgh", 0..) |r, i| try core.registers.put(gpa, r, body.items[i * 40 ..][0..200], 0, 1, true); + try frame(core); + const Step = struct { + fn run(c: *pardes.Pardes, ev: pardes.Event, n: *Instructions, s: *Slowest) !void { + const was = n.read(); + const t0 = nowNs(); + c.update(ev); + try frame(c); + s.add(nowNs() -| t0, n.read() -| was, 0); + } + }; + const shown = struct { + fn wait(c: *pardes.Pardes) !void { + c.advance(c.clock.now_ns); + c.clock.now_ns = c.view().tag_state.due_ns; + try frame(c); + } + }; + // Registers: `"`, its level shown, unrolled; then Down over its rows. + core.update(.{ .key = .{ .cp = '"' } }); + try shown.wait(core); + core.update(.{ .key = .{ .cp = pardes.Key.down } }); + var regs: Slowest = .{}; + for (0..8) |_| try Step.run(core, .{ .key = .{ .cp = pardes.Key.down } }, &counter, ®s); + regs.report("registers Down + frame", "\"", 0); + core.update(.{ .key = .{ .cp = pardes.Key.escape } }); + // A frame at rest, then frames with `]`'s targets painted. + var rest: Slowest = .{}; + var painted: Slowest = .{}; + for (0..8) |_| try Step.run(core, .tick, &counter, &rest); + core.update(.{ .key = .{ .cp = ']' } }); + try shown.wait(core); + for (0..8) |_| try Step.run(core, .tick, &counter, &painted); + rest.report("tick + frame at rest", "", 0); + painted.report("tick + frame, ] painted", "]", 0); + core.update(.{ .key = .{ .cp = pardes.Key.escape } }); + // The themes picker: Down, the window wearing the next. + pardes.Pick.open(core, core.view().active, .themes, "", "", ""); + try frame(core); + var themes: Slowest = .{}; + for (0..8) |_| try Step.run(core, .{ .key = .{ .cp = pardes.Key.down } }, &counter, &themes); + themes.report("themes Down + frame", "", 0); + if (core.theme_preview == null) return error.NoThemePreview; +} + /// The longest of a scan's calls. const Slowest = struct { calls: usize = 0, @@ -2020,6 +2268,7 @@ fn reportText( term_cells: *const [std.enums.values(TermOp).len][term_fixtures.len]Cell, bytes: *const [fixtures.len]usize, reps: usize, + named: ?Op, base_path: ?[]const u8, base: ?Base, ) void { @@ -2043,6 +2292,7 @@ fn reportText( o("\n{s}\n", .{if (base != null) &@as([12 + fixtures.len * 20]u8, @splat('-')) else &@as([12 + fixtures.len * 13]u8, @splat('-'))}); var worst_jitter: f64 = 0; for (std.enums.values(Op), 0..) |op, oi| { + if (op.named() and named != op) continue; o("{s:<12}", .{op.label()}); for (0..fixtures.len) |fi| { const c = cells[oi][fi]; @@ -2132,6 +2382,7 @@ fn writeJson( for (fixtures, 0..) |fx, fi| { if (opts.only) |name| if (!std.mem.eql(u8, name, fx.name)) continue; if (opts.op) |o| if (o != op) continue; + if (op.named() and opts.op == null) continue; const c = cells[oi][fi]; try out.print("{s}{{\"op\":\"{s}\",\"fixture\":\"{s}\",\"min_us\":{d},\"med_us\":{d},\"p90_us\":{d},\"max_us\":{d}}}", .{ if (first) "" else ",", op.label(), fx.name, c.min_us, c.med_us, c.p90_us, c.max_us, @@ -2236,7 +2487,7 @@ fn parseBase(src: []const u8, opts: Options, bytes: *const [fixtures.len]usize) } for (fixtures, 0..) |fixture, fi| { if (opts.only) |name| if (!std.mem.eql(u8, name, fixture.name)) continue; - for (file_seen) |row| if (!row[fi]) return error.BaselineCoverage; + for (file_seen, std.enums.values(Op)) |row, op| if (!row[fi] and !op.named()) return error.BaselineCoverage; } if (opts.only == null) for (term_seen) |row| { for (row) |seen| if (!seen) return error.BaselineCoverage; |
