diff options
Diffstat (limited to 'test/perf.zig')
| -rw-r--r-- | test/perf.zig | 138 |
1 files changed, 137 insertions, 1 deletions
diff --git a/test/perf.zig b/test/perf.zig index 7920dedf..8c88ef01 100644 --- a/test/perf.zig +++ b/test/perf.zig @@ -391,6 +391,8 @@ const Options = struct { creation: bool = false, startup: bool = false, mini: bool = false, + /// The scans a frame's slice bounds, a call at a time (`measureSearch`). + search: 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). @@ -407,13 +409,14 @@ 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, @"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, @"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, .creation => opts.creation = true, .startup => opts.startup = true, .mini => opts.mini = true, + .search => opts.search = true, .@"self-test" => opts.self_test = true, else => { i += 1; @@ -475,6 +478,7 @@ pub fn main(init: std.process.Init) !void { if (opts.creation) return measureCreation(init.io, reps, opts.json); 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.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 @@ -1742,6 +1746,138 @@ fn measureMini(io: std.Io, fallback: std.mem.Allocator, reps: usize, json: bool) } } +/// `--search`: the two scans a frame's slice bounds, each call timed and +/// its instructions counted (user space, this thread): a find's names +/// scored again as a query is typed into it over 200,000 paths +/// (core/Names.zig `rescore`, Names' cap), and a search's places found a +/// tick at a time over the large fixture, the view in its middle +/// (core/Places.zig `settle` on a tick). The longest call of each is the +/// slice a key or a frame can take. +fn measureSearch(io: std.Io) !void { + var random: [12]u8 = undefined; + io.random(&random); + const dir = try std.fmt.allocPrint(gpa, "/tmp/pardes-perf-{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(); + + const Names = @FieldType(pardes.panes.Query, "names"); + var names: Names = .{}; + defer names.deinit(gpa); + 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" }; + var row: [160]u8 = undefined; + for (0..200_000) |i| { + const path = try std.fmt.bufPrint(&row, "{s}/module_{d}/{s}_{d}.zig", .{ dirs[i % dirs.len], i / 64 % 997, words[i * 7 % words.len], i }); + try names.add(gpa, path, 0, path.len, Names.baseOf(path), 0); + } + // Typed a key at a time, as into a find: each key's call, then the + // calls of the frames after it while it is busy. + for ([_][]const u8{ "m", "tape", "places zig", "core/module_9/replica_1", "zig-pkg/ghostty/src/terminal/module_99/kitty" }) |query| { + var slow: Slowest = .{}; + names.typed(gpa, ""); + for (1..query.len + 1) |n| { + var calls: usize = 0; + while (true) : (calls += 1) { + const was = counter.read(); + const t0 = nowNs(); + if (calls == 0) names.typed(gpa, query[0..n]) else names.rescore(gpa); + slow.add(nowNs() -| t0, counter.read() -| was, names.hits.items.len); + if (!names.busy()) break; + } + } + slow.report("rescore", query, names.hits.items.len); + } + + const fx = fixtures[2]; + const path = try std.fmt.allocPrint(gpa, "{s}/{s}.zig", .{ dir, fx.name }); + defer gpa.free(path); + const text = try generate(fx); + try writeFile(path, text); + gpa.free(text); + for ([_][]const u8{ "e", "lorem.*amet", "helper_[0-9]+", "qqq" }) |pattern| { + const core = try boot(); + defer core.deinit(); + pardes.look.lookAt(core, 0, path); + pump(core); + const id = filePane(core) orelse return error.OpenDidNotOpenFile; + const pane = core.panes[id].?; + seek(pane, fx, 0); + pane.file.?.scroll = fx.lines / 2; + pane.body.cur_row = @intCast(fx.lines / 2); + _ = try frame(core); + const set = &pane.places; + set.search(core, pane, .typed, pane.serial, pattern, false, null, false); + var slow: Slowest = .{}; + while (set.busy(pane) and slow.calls < 100_000) { + const was = counter.read(); + const t0 = nowNs(); + set.settle(core, pane, true); + slow.add(nowNs() -| t0, counter.read() -| was, 0); + } + slow.report("fill", pattern, set.hits.items.len); + } +} + +/// The longest of a scan's calls. +const Slowest = struct { + calls: usize = 0, + total_ns: u64 = 0, + ns: u64 = 0, + /// The instructions of the longest call (by time), and the most of any. + ins: u64 = 0, + most_ins: u64 = 0, + /// The same of the calls that left under 1,000 hits to sort: the scan + /// alone, nearly (`Names.rescore` sorts every hit at each call). + few_ns: u64 = 0, + few_ins: u64 = 0, + + fn add(s: *Slowest, ns: u64, ins: u64, hits: usize) void { + s.calls += 1; + if (hits < 1000 and ns > s.few_ns) { + s.few_ns = ns; + s.few_ins = ins; + } + s.total_ns += ns; + if (ns > s.ns) { + s.ns = ns; + s.ins = ins; + } + s.most_ins = @max(s.most_ins, ins); + } + + fn report(s: *const Slowest, what: []const u8, of: []const u8, hits: usize) void { + std.debug.print("{s} \"{s}\": {d} calls, {d} hits, {d:.2} ms in all; longest {d} us ({d} instructions); most instructions {d}; longest with under 1,000 hits {d} us ({d} instructions)\n", .{ + what, of, s.calls, hits, @as(f64, @floatFromInt(s.total_ns)) / 1e6, s.ns / 1000, s.ins, s.most_ins, s.few_ns / 1000, s.few_ins, + }); + } +}; + +/// This thread's instructions in user space (perf_event_open). +const Instructions = struct { + fd: std.posix.fd_t, + + fn open() !Instructions { + const linux = std.os.linux; + var attr: linux.perf_event_attr = .{ .type = .HARDWARE, .config = @intFromEnum(linux.PERF.COUNT.HW.INSTRUCTIONS), .flags = .{ .exclude_kernel = true, .exclude_hv = true } }; + const rc = linux.perf_event_open(&attr, 0, -1, -1, 0); + if (linux.errno(rc) != .SUCCESS) return error.NoInstructionCounter; + return .{ .fd = @intCast(rc) }; + } + + fn read(c: Instructions) u64 { + var n: u64 = 0; + _ = std.os.linux.read(c.fd, std.mem.asBytes(&n), 8); + return n; + } + + fn close(c: Instructions) void { + _ = std.os.linux.close(c.fd); + } +}; + fn seek(pane: *pardes.Pane, fx: Fixture, n: usize) void { const f = &pane.file.?; const span = @max(1, fx.lines * 8 / 10); |
