From 97f83717cf8be3e4eedd3f7b932766c1cf72fd37 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Thu, 29 Oct 2026 10:46:16 -0300 Subject: perf --live: keystroke-to-present through the real tty and GUI, `yes | head -c 1G` through a real pty, and a big file's open The baseline the daemon work is measured against (docs/next-daemon.md). `pardes-perf --live ` runs the real tty binary in a pty (test/e2e_harness.zig, as the snapshot harness does): FrameStats.keys's gestures (key-down, key-right, edit-char, page-down, wheel) on the 50k-line fixture, a phase each, each frame's latency from the shell taking the input off its queue to the end of its present, read back from the session's PARDES_FRAMES dump; `yes | head -c 1G` typed into a bash pane in tty mode, wall time to the mark after it and the frames drawn; and a 1,000,000-line file opened on the command line, wall time to its first line on screen. `--live-gui ` runs the same gestures as the GUI's own timedemo (PARDES_FRAMES_DEMO=keys) in a hidden test window. p50 and p99 per phase, pooled over --reps runs; text or --json. Each run has a private home (bash with a `$ ` prompt), XDG_RUNTIME_DIR and fixtures under $TMPDIR, all removed after. The frames dump now says the monotonic time of its first frame (`# t0_ns`), so a driver lines its own phase times up with the frames'. The perf gate's baselines (test/perf-baseline-{tty-debug,tty-fast,gui-debug}.json) are recorded again: the harness id is perf.zig's hash, and the medium cells came out within about 10% of the old ones (edit-char and edit-twin faster). Co-Authored-By: Claude Opus 5.5 --- test/perf-baseline-gui-debug.json | 2 +- test/perf-baseline-tty-debug.json | 2 +- test/perf-baseline-tty-fast.json | 2 +- test/perf.zig | 302 +++++++++++++++++++++++++++++++++++++- 4 files changed, 304 insertions(+), 4 deletions(-) (limited to 'test') diff --git a/test/perf-baseline-gui-debug.json b/test/perf-baseline-gui-debug.json index a41c92b2..160e7bec 100644 --- a/test/perf-baseline-gui-debug.json +++ b/test/perf-baseline-gui-debug.json @@ -1 +1 @@ -{"harness":"442f274a5da9136c","build":{"compiler":"0.17.0","target":"x86_64-linux.5.10...7.2-gnu.2.38","cpu":"x86_64","driver_optimize":"fast","core_optimize":"debug","c_optimize":"fast","terminal_optimize":"safe","platform":"gui","grammars":"full","mupdf":true,"jpx":true,"tracy":false,"quic":false,"theme_animation":true,"terminal_simd":true,"prebuilt_shaders":false},"cols":120,"rows":40,"reps":10,"only":"medium","fixtures":[{"name":"small","lines":1000,"cols":60,"bytes":62694},{"name":"medium","lines":50000,"cols":60,"bytes":3201944},{"name":"large","lines":300000,"cols":60,"bytes":19464444},{"name":"longline","lines":400,"cols":8000,"bytes":3200400},{"name":"esp32p4","lines":12,"cols":240,"bytes":2892}],"cells":[{"op":"open","fixture":"medium","min_us":2253,"med_us":2626,"p90_us":3065,"max_us":3090},{"op":"render","fixture":"medium","min_us":100,"med_us":103,"p90_us":103,"max_us":110},{"op":"key-down","fixture":"medium","min_us":102,"med_us":103,"p90_us":105,"max_us":113},{"op":"key-right","fixture":"medium","min_us":98,"med_us":104,"p90_us":108,"max_us":113},{"op":"page-down","fixture":"medium","min_us":103,"med_us":104,"p90_us":111,"max_us":114},{"op":"wheel","fixture":"medium","min_us":98,"med_us":100,"p90_us":102,"max_us":108},{"op":"edit-char","fixture":"medium","min_us":1251,"med_us":1940,"p90_us":2652,"max_us":3041},{"op":"edit-twin","fixture":"medium","min_us":2779,"med_us":3177,"p90_us":3366,"max_us":3657},{"op":"body-2m","fixture":"medium","min_us":5812,"med_us":11423,"p90_us":13681,"max_us":13758},{"op":"body-appends","fixture":"medium","min_us":153898,"med_us":241075,"p90_us":284518,"max_us":305761},{"op":"deep-sel","fixture":"medium","min_us":86,"med_us":91,"p90_us":94,"max_us":122}]} +{"harness":"418270d16a91d1f9","build":{"compiler":"0.17.0","target":"x86_64-linux.5.10...7.2-gnu.2.38","cpu":"x86_64","driver_optimize":"fast","core_optimize":"debug","c_optimize":"fast","terminal_optimize":"safe","platform":"gui","grammars":"full","mupdf":true,"jpx":true,"tracy":false,"quic":false,"theme_animation":true,"terminal_simd":true,"prebuilt_shaders":false},"cols":120,"rows":40,"reps":10,"only":"medium","fixtures":[{"name":"small","lines":1000,"cols":60,"bytes":62694},{"name":"medium","lines":50000,"cols":60,"bytes":3201944},{"name":"large","lines":300000,"cols":60,"bytes":19464444},{"name":"longline","lines":400,"cols":8000,"bytes":3200400},{"name":"esp32p4","lines":12,"cols":240,"bytes":2892}],"cells":[{"op":"open","fixture":"medium","min_us":1972,"med_us":2504,"p90_us":3483,"max_us":3564},{"op":"render","fixture":"medium","min_us":95,"med_us":98,"p90_us":100,"max_us":106},{"op":"key-down","fixture":"medium","min_us":95,"med_us":103,"p90_us":120,"max_us":124},{"op":"key-right","fixture":"medium","min_us":95,"med_us":101,"p90_us":108,"max_us":117},{"op":"page-down","fixture":"medium","min_us":94,"med_us":99,"p90_us":106,"max_us":113},{"op":"wheel","fixture":"medium","min_us":91,"med_us":95,"p90_us":97,"max_us":102},{"op":"edit-char","fixture":"medium","min_us":824,"med_us":1322,"p90_us":1456,"max_us":1774},{"op":"edit-twin","fixture":"medium","min_us":877,"med_us":1207,"p90_us":1736,"max_us":1737},{"op":"body-2m","fixture":"medium","min_us":6801,"med_us":10994,"p90_us":14230,"max_us":14251},{"op":"body-appends","fixture":"medium","min_us":149251,"med_us":237290,"p90_us":297061,"max_us":308786},{"op":"deep-sel","fixture":"medium","min_us":90,"med_us":92,"p90_us":100,"max_us":100}]} diff --git a/test/perf-baseline-tty-debug.json b/test/perf-baseline-tty-debug.json index dc37e033..eefb6c60 100644 --- a/test/perf-baseline-tty-debug.json +++ b/test/perf-baseline-tty-debug.json @@ -1 +1 @@ -{"harness":"442f274a5da9136c","build":{"compiler":"0.17.0","target":"x86_64-linux.5.10...7.2-gnu.2.38","cpu":"x86_64","driver_optimize":"fast","core_optimize":"debug","c_optimize":"fast","terminal_optimize":"safe","platform":"tty","grammars":"full","mupdf":true,"jpx":true,"tracy":false,"quic":false,"theme_animation":true,"terminal_simd":true,"prebuilt_shaders":false},"cols":120,"rows":40,"reps":10,"only":"medium","fixtures":[{"name":"small","lines":1000,"cols":60,"bytes":62694},{"name":"medium","lines":50000,"cols":60,"bytes":3201944},{"name":"large","lines":300000,"cols":60,"bytes":19464444},{"name":"longline","lines":400,"cols":8000,"bytes":3200400},{"name":"esp32p4","lines":12,"cols":240,"bytes":2892}],"cells":[{"op":"open","fixture":"medium","min_us":2181,"med_us":2495,"p90_us":2621,"max_us":2660},{"op":"render","fixture":"medium","min_us":96,"med_us":98,"p90_us":102,"max_us":112},{"op":"key-down","fixture":"medium","min_us":101,"med_us":103,"p90_us":107,"max_us":108},{"op":"key-right","fixture":"medium","min_us":100,"med_us":112,"p90_us":118,"max_us":119},{"op":"page-down","fixture":"medium","min_us":97,"med_us":101,"p90_us":105,"max_us":107},{"op":"wheel","fixture":"medium","min_us":98,"med_us":100,"p90_us":108,"max_us":110},{"op":"edit-char","fixture":"medium","min_us":1377,"med_us":1842,"p90_us":2336,"max_us":2418},{"op":"edit-twin","fixture":"medium","min_us":2655,"med_us":3200,"p90_us":3321,"max_us":3660},{"op":"body-2m","fixture":"medium","min_us":5420,"med_us":10753,"p90_us":12826,"max_us":15385},{"op":"body-appends","fixture":"medium","min_us":153569,"med_us":232519,"p90_us":281625,"max_us":309036},{"op":"deep-sel","fixture":"medium","min_us":91,"med_us":98,"p90_us":111,"max_us":111}]} +{"harness":"418270d16a91d1f9","build":{"compiler":"0.17.0","target":"x86_64-linux.5.10...7.2-gnu.2.38","cpu":"x86_64","driver_optimize":"fast","core_optimize":"debug","c_optimize":"fast","terminal_optimize":"safe","platform":"tty","grammars":"full","mupdf":true,"jpx":true,"tracy":false,"quic":false,"theme_animation":true,"terminal_simd":true,"prebuilt_shaders":false},"cols":120,"rows":40,"reps":10,"only":"medium","fixtures":[{"name":"small","lines":1000,"cols":60,"bytes":62694},{"name":"medium","lines":50000,"cols":60,"bytes":3201944},{"name":"large","lines":300000,"cols":60,"bytes":19464444},{"name":"longline","lines":400,"cols":8000,"bytes":3200400},{"name":"esp32p4","lines":12,"cols":240,"bytes":2892}],"cells":[{"op":"open","fixture":"medium","min_us":2250,"med_us":3027,"p90_us":4030,"max_us":4593},{"op":"render","fixture":"medium","min_us":99,"med_us":115,"p90_us":123,"max_us":126},{"op":"key-down","fixture":"medium","min_us":106,"med_us":117,"p90_us":128,"max_us":144},{"op":"key-right","fixture":"medium","min_us":102,"med_us":107,"p90_us":112,"max_us":149},{"op":"page-down","fixture":"medium","min_us":99,"med_us":103,"p90_us":108,"max_us":116},{"op":"wheel","fixture":"medium","min_us":101,"med_us":113,"p90_us":115,"max_us":117},{"op":"edit-char","fixture":"medium","min_us":986,"med_us":1712,"p90_us":2219,"max_us":2364},{"op":"edit-twin","fixture":"medium","min_us":1116,"med_us":1448,"p90_us":1938,"max_us":2052},{"op":"body-2m","fixture":"medium","min_us":7050,"med_us":12139,"p90_us":18239,"max_us":24078},{"op":"body-appends","fixture":"medium","min_us":220990,"med_us":347620,"p90_us":381389,"max_us":450691},{"op":"deep-sel","fixture":"medium","min_us":106,"med_us":113,"p90_us":117,"max_us":122}]} diff --git a/test/perf-baseline-tty-fast.json b/test/perf-baseline-tty-fast.json index bd8f32dc..c8d0ed78 100644 --- a/test/perf-baseline-tty-fast.json +++ b/test/perf-baseline-tty-fast.json @@ -1 +1 @@ -{"harness":"442f274a5da9136c","build":{"compiler":"0.17.0","target":"x86_64-linux.5.10...7.2-gnu.2.38","cpu":"x86_64","driver_optimize":"fast","core_optimize":"fast","c_optimize":"fast","terminal_optimize":"fast","platform":"tty","grammars":"full","mupdf":true,"jpx":true,"tracy":false,"quic":false,"theme_animation":true,"terminal_simd":true,"prebuilt_shaders":false},"cols":120,"rows":40,"reps":10,"only":"medium","fixtures":[{"name":"small","lines":1000,"cols":60,"bytes":62694},{"name":"medium","lines":50000,"cols":60,"bytes":3201944},{"name":"large","lines":300000,"cols":60,"bytes":19464444},{"name":"longline","lines":400,"cols":8000,"bytes":3200400},{"name":"esp32p4","lines":12,"cols":240,"bytes":2892}],"cells":[{"op":"open","fixture":"medium","min_us":2283,"med_us":2611,"p90_us":2812,"max_us":2917},{"op":"render","fixture":"medium","min_us":89,"med_us":97,"p90_us":105,"max_us":119},{"op":"key-down","fixture":"medium","min_us":88,"med_us":95,"p90_us":100,"max_us":116},{"op":"key-right","fixture":"medium","min_us":90,"med_us":99,"p90_us":103,"max_us":106},{"op":"page-down","fixture":"medium","min_us":86,"med_us":90,"p90_us":96,"max_us":97},{"op":"wheel","fixture":"medium","min_us":85,"med_us":91,"p90_us":94,"max_us":101},{"op":"edit-char","fixture":"medium","min_us":1229,"med_us":1823,"p90_us":1874,"max_us":2101},{"op":"edit-twin","fixture":"medium","min_us":2196,"med_us":2745,"p90_us":2928,"max_us":2972},{"op":"body-2m","fixture":"medium","min_us":5606,"med_us":10369,"p90_us":14063,"max_us":14406},{"op":"body-appends","fixture":"medium","min_us":155867,"med_us":237195,"p90_us":289128,"max_us":306283},{"op":"deep-sel","fixture":"medium","min_us":82,"med_us":84,"p90_us":86,"max_us":91}]} +{"harness":"418270d16a91d1f9","build":{"compiler":"0.17.0","target":"x86_64-linux.5.10...7.2-gnu.2.38","cpu":"x86_64","driver_optimize":"fast","core_optimize":"fast","c_optimize":"fast","terminal_optimize":"fast","platform":"tty","grammars":"full","mupdf":true,"jpx":true,"tracy":false,"quic":false,"theme_animation":true,"terminal_simd":true,"prebuilt_shaders":false},"cols":120,"rows":40,"reps":10,"only":"medium","fixtures":[{"name":"small","lines":1000,"cols":60,"bytes":62694},{"name":"medium","lines":50000,"cols":60,"bytes":3201944},{"name":"large","lines":300000,"cols":60,"bytes":19464444},{"name":"longline","lines":400,"cols":8000,"bytes":3200400},{"name":"esp32p4","lines":12,"cols":240,"bytes":2892}],"cells":[{"op":"open","fixture":"medium","min_us":2341,"med_us":2633,"p90_us":3392,"max_us":5686},{"op":"render","fixture":"medium","min_us":87,"med_us":100,"p90_us":112,"max_us":114},{"op":"key-down","fixture":"medium","min_us":89,"med_us":93,"p90_us":98,"max_us":99},{"op":"key-right","fixture":"medium","min_us":94,"med_us":101,"p90_us":110,"max_us":113},{"op":"page-down","fixture":"medium","min_us":91,"med_us":98,"p90_us":105,"max_us":106},{"op":"wheel","fixture":"medium","min_us":89,"med_us":97,"p90_us":103,"max_us":109},{"op":"edit-char","fixture":"medium","min_us":1055,"med_us":1950,"p90_us":2385,"max_us":2911},{"op":"edit-twin","fixture":"medium","min_us":1021,"med_us":1470,"p90_us":1966,"max_us":2153},{"op":"body-2m","fixture":"medium","min_us":5050,"med_us":11673,"p90_us":14865,"max_us":18117},{"op":"body-appends","fixture":"medium","min_us":209537,"med_us":316775,"p90_us":360616,"max_us":365634},{"op":"deep-sel","fixture":"medium","min_us":84,"med_us":118,"p90_us":143,"max_us":150}]} diff --git a/test/perf.zig b/test/perf.zig index ab284739..cc43abcf 100644 --- a/test/perf.zig +++ b/test/perf.zig @@ -378,6 +378,10 @@ const Options = struct { startup: bool = false, mini: 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). + live: ?[]const u8 = null, + live_gui: ?[]const u8 = null, self_test: bool = false, /// A gate: fail when a cell's fastest sample is more than this many /// times the baseline's fastest (--base's). @@ -389,7 +393,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, @"terminal-mib", @"self-test", reps, cols, rows, base, only, @"max-ratio" }, arg[2..]) orelse + const option = std.meta.stringToEnum(enum { json, creation, startup, mini, @"terminal-mib", @"self-test", reps, cols, rows, base, only, @"max-ratio", live, @"live-gui" }, arg[2..]) orelse return error.UnknownOption; switch (option) { .json => opts.json = true, @@ -409,6 +413,8 @@ const Options = struct { .only => opts.only = value, .@"terminal-mib" => opts.terminal_mib = std.fmt.parseInt(usize, value, 10) catch return error.InvalidNumber, .@"max-ratio" => opts.max_ratio = std.fmt.parseFloat(f64, value) catch return error.InvalidNumber, + .live => opts.live = value, + .@"live-gui" => opts.live_gui = value, else => unreachable, } }, @@ -452,6 +458,8 @@ 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.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); var random: [12]u8 = undefined; init.io.random(&random); @@ -1067,6 +1075,298 @@ fn measureTerminalSession(io: std.Io, mib: usize, reps: usize, json: bool) !void try std.Io.File.stdout().writeStreamingAll(io, "\n"); } +// ---- live: the real binary, in a pty (tty) or a hidden test window (gui) ---- +// +// Keystroke-to-present is FrameStats' own latency: from the shell taking an +// input off its queue to the end of the frame's present. Both frontends run +// FrameStats.keys (the tty through the pty, the gui as its timedemo) and the +// frames' dump (PARDES_FRAMES) is split by its phases. `yes | head -c 1G` is +// typed into a shell and timed to the mark after it; the big file is opened +// on the command line and timed to its first line on screen. + +const eh = @import("e2e_harness.zig"); +extern "c" fn setenv(name: [*:0]const u8, value: [*:0]const u8, overwrite: c_int) c_int; + +const live_phases = [_][]const u8{ "key-down", "key-right", "edit-char", "page-down", "wheel" }; +const yes_bytes = 1 << 30; +const big_lines = 1_000_000; + +const Live = struct { + dir: []const u8, + /// Each phase's frame latencies, µs, over every run, and the core's + /// part of those frames (update and render: the rest is the shell's + /// present, a test window's capture of the frame included). + samples: [live_phases.len]std.ArrayList(u32) = @splat(.empty), + core: [live_phases.len]std.ArrayList(u32) = @splat(.empty), + yes_ns: std.ArrayList(u64) = .empty, + yes_frames: std.ArrayList(u64) = .empty, + open_ns: std.ArrayList(u64) = .empty, + + fn path(l: *const Live, comptime name: []const u8) ![:0]const u8 { + return std.fmt.allocPrintSentinel(gpa, "{s}/" ++ name, .{l.dir}, 0); + } +}; + +/// A private home (bash with a `$ ` prompt, as the snapshot harness pins), +/// runtime directory and the two fixtures, under $TMPDIR. +fn liveSetup(io: std.Io) !Live { + var random: [8]u8 = undefined; + io.random(&random); + const tmp = if (std.c.getenv("TMPDIR")) |t| std.mem.span(t) else "/tmp"; + const dir = try std.fmt.allocPrint(gpa, "{s}/pardes-perf-live-{x}", .{ tmp, random }); + try std.Io.Dir.cwd().createDirPath(io, dir); + const l: Live = .{ .dir = dir }; + try std.Io.Dir.cwd().createDirPath(io, try l.path("home/.config/pardes")); + try writeFile(try l.path("home/.bashrc"), "PS1='$ '\nHISTFILE=\n"); + try writeFile(try l.path("home/.config/pardes/init"), "Shell bash\n"); + // A runtime directory of its own: no session of these shows up in the + // user's. Short, for the 9P socket's 108-byte path. + const run = try std.fmt.allocPrintSentinel(gpa, "/tmp/pardes-perf-run-{x}", .{random}, 0); + try std.Io.Dir.cwd().createDirPath(io, run); + _ = setenv("XDG_RUNTIME_DIR", run, 1); + _ = setenv("HOME", try l.path("home"), 1); + _ = setenv("XDG_CONFIG_HOME", try l.path("home/.config"), 1); + _ = setenv("TERM", "xterm-256color", 1); + const medium = try generate(fixtures[1]); + defer gpa.free(medium); + try writeFile(try l.path("medium.zig"), medium); + var big: std.ArrayList(u8) = .empty; + defer big.deinit(gpa); + for (0..big_lines) |n| try big.print(gpa, "line {d:0>7} the quick brown fox jumps over the lazy dog 0123456789\n", .{n + 1}); + try writeFile(try l.path("big.txt"), big.items); + return l; +} + +fn liveCleanup(io: std.Io, l: *Live) void { + std.Io.Dir.cwd().deleteTree(io, l.dir) catch {}; + if (std.c.getenv("XDG_RUNTIME_DIR")) |run| std.Io.Dir.cwd().deleteTree(io, std.mem.span(run)) catch {}; +} + +/// The bytes a terminal sends for a step of FrameStats.keys. +fn stepBytes(buf: *[32]u8, step: pardes.FrameStats.Step) []const u8 { + return switch (step.act) { + .key => |k| switch (k.cp) { + pardes.Key.escape => "\x1b", + pardes.Key.page_down => "\x1b[6~", + else => buf[0 .. std.unicode.utf8Encode(k.cp, buf) catch 0], + }, + // SGR wheel down (65) or up (64), over the file's body. + .wheel => |notches| std.fmt.bufPrint(buf, "\x1b[<{d};60;20M", .{@as(u8, if (notches > 0) 65 else 64)}) catch "", + else => "", + }; +} + +/// Feeds the harness until the app exits (reaped) or `ms` runs out (killed, +/// its screen shown). +fn liveExit(h: *eh.Harness, ms: i64) bool { + const deadline = eh.nowMs() + ms; + while (eh.nowMs() < deadline) { + var status: c_int = 0; + if (libc.waitpid(h.pid, &status, libc.W.NOHANG) == h.pid) { + if (status != 0) h.dump("live: the app failed"); + // Reaped: the harness must not signal a pid that is gone. + _ = libc.close(h.master); + h.stream.deinit(); + gpa.destroy(h.raw); + h.term.deinit(gpa); + gpa.destroy(h.term); + return true; + } + _ = h.pumpOnce(20) catch false; + } + h.dump("live: the app did not exit"); + h.deinit(); + _ = libc.waitpid(h.pid, null, 0); + return false; +} + +/// Exit, and Exit again past the question an edited file asks. +fn liveQuit(h: *eh.Harness) !bool { + try h.send("\x1b"); + try h.pump(60); + try h.send(" q"); + try h.pump(200); + try h.send(" q"); + return liveExit(h, 10_000); +} + +/// How many frames a PARDES_FRAMES dump says were drawn. +fn dumpFrames(dump_path: []const u8) !u64 { + const text = try readFileAlloc(dump_path); + defer gpa.free(text); + if (!std.mem.startsWith(u8, text, "# frames ")) return error.BadFrames; + var it = std.mem.tokenizeScalar(u8, text["# frames ".len..], ' '); + return std.fmt.parseInt(u64, it.next() orelse return error.BadFrames, 10); +} + +/// The frames of a PARDES_FRAMES dump, each latency into the phase it fell +/// in. `starts` are the phases' monotonic starts, or null to take them +/// from the dump's own `# phase` lines (the GUI's timedemo writes them). +fn liveFrames(l: *Live, dump_path: []const u8, starts: ?*const [live_phases.len + 1]u64) !void { + const text = try readFileAlloc(dump_path); + defer gpa.free(text); + var t0: u64 = 0; + // Each phase's start and, last, "done"'s: the quit's keys are no phase's. + var from: [live_phases.len + 1]u64 = if (starts) |s| s.* else @splat(std.math.maxInt(u64)); + var lines = std.mem.splitScalar(u8, text, '\n'); + while (lines.next()) |line| { + if (std.mem.startsWith(u8, line, "# t0_ns ")) { + t0 = try std.fmt.parseInt(u64, line["# t0_ns ".len..], 10); + } else if (std.mem.startsWith(u8, line, "# phase ") and starts == null) { + var it = std.mem.tokenizeScalar(u8, line["# phase ".len..], ' '); + const name = it.next() orelse continue; + const at = t0 + @as(u64, @intFromFloat(try std.fmt.parseFloat(f64, it.next() orelse continue) * std.time.ns_per_ms)); + for (live_phases ++ [_][]const u8{"done"}, 0..) |phase, i| if (std.mem.eql(u8, name, phase)) { + from[i] = at; + }; + } + } + lines = std.mem.splitScalar(u8, text, '\n'); + while (lines.next()) |line| { + if (line.len == 0 or line[0] == '#') continue; + var fields = std.mem.splitScalar(u8, line, '\t'); + const start_ms = try std.fmt.parseFloat(f64, fields.next() orelse continue); + const update = try std.fmt.parseInt(u32, fields.next() orelse continue, 10); + const render = try std.fmt.parseInt(u32, fields.next() orelse continue, 10); + for (0..6) |_| _ = fields.next(); + const latency = try std.fmt.parseInt(u32, fields.next() orelse continue, 10); + if (latency == 0) continue; + const at = t0 + @as(u64, @intFromFloat(start_ms * std.time.ns_per_ms)); + var phase: ?usize = null; + for (from, 0..) |f, i| if (at >= f) { + phase = i; + }; + if (phase) |i| if (i < live_phases.len) { + try l.samples[i].append(gpa, latency); + try l.core[i].append(gpa, update + render); + }; + } +} + +fn liveKeysTty(l: *Live, exe: [:0]const u8) !void { + const dump_path = try l.path("tty-frames.tsv"); + _ = setenv("PARDES_FRAMES", dump_path, 1); + var h = try eh.Harness.initArgv(gpa, exe, screen_rows, screen_cols, &.{(try l.path("medium.zig")).ptr}); + if (!try h.waitFor("helper_1", 20_000)) return error.LiveFileNotShown; + try h.pump(300); + var starts: [live_phases.len + 1]u64 = @splat(std.math.maxInt(u64)); + const t0 = nowNs(); + var buf: [32]u8 = undefined; + for (pardes.FrameStats.named("keys").?) |step| { + const due = t0 + @as(u64, step.at_ms) * std.time.ns_per_ms; + while (nowNs() < due) _ = try h.pumpOnce(@intCast(@max(1, (due -| nowNs()) / std.time.ns_per_ms))); + switch (step.act) { + .phase => |name| for (live_phases ++ [_][]const u8{"done"}, 0..) |phase, i| if (std.mem.eql(u8, name, phase)) { + starts[i] = nowNs(); + }, + .end => break, + else => try h.send(stepBytes(&buf, step)), + } + } + if (!try liveQuit(&h)) return error.LiveDidNotExit; + try liveFrames(l, dump_path, &starts); +} + +fn liveYes(l: *Live, exe: [:0]const u8) !void { + const dump_path = try l.path("yes-frames.tsv"); + _ = setenv("PARDES_FRAMES", dump_path, 1); + var h = try eh.Harness.initArgv(gpa, exe, screen_rows, screen_cols, &.{ "-n", "1" }); + errdefer h.dump("live yes"); + // The prompt shows in tty mode only: the shell's tag, then Ctrl-b in. + if (!try h.waitFor("Tty+bash", 20_000)) return error.LiveNoPrompt; + try h.pump(500); + try h.send("\x02"); + if (!try h.waitFor("$ ", 20_000)) return error.LiveNoPrompt; + try h.send(std.fmt.comptimePrint("yes | head -c {d}; echo DO''NE", .{yes_bytes})); + try h.pump(100); + const t0 = nowNs(); + try h.send("\r"); + if (!try h.waitFor("DONE", 600_000)) return error.LiveYesUnfinished; + try l.yes_ns.append(gpa, nowNs() - t0); + try h.pump(200); + try h.send("exit\r"); + if (!liveExit(&h, 10_000)) return error.LiveDidNotExit; + // Only the frame count: a stream's frames answer no input. + try l.yes_frames.append(gpa, try dumpFrames(dump_path)); +} + +fn liveOpen(l: *Live, exe: [:0]const u8) !void { + _ = setenv("PARDES_FRAMES", "", 1); + const t0 = nowNs(); + var h = try eh.Harness.initArgv(gpa, exe, screen_rows, screen_cols, &.{(try l.path("big.txt")).ptr}); + if (!try h.waitFor("line 0000001", 60_000)) return error.LiveFileNotShown; + try l.open_ns.append(gpa, nowNs() - t0); + if (!try liveQuit(&h)) return error.LiveDidNotExit; +} + +fn liveKeysGui(l: *Live, exe: [:0]const u8) !void { + const dump_path = try l.path("gui-frames.tsv"); + _ = setenv("PARDES_FRAMES", dump_path, 1); + _ = setenv("PARDES_FRAMES_DEMO", "keys", 1); + _ = setenv("PARDES_TEST", "1", 1); + _ = setenv("PARDES_TEST_CAPTURE_DIR", try std.fmt.allocPrintSentinel(gpa, "{s}", .{l.dir}, 0), 1); + _ = setenv("PARDES_TEST_COLS", try std.fmt.allocPrintSentinel(gpa, "{d}", .{screen_cols}, 0), 1); + _ = setenv("PARDES_TEST_ROWS", try std.fmt.allocPrintSentinel(gpa, "{d}", .{screen_rows}, 0), 1); + _ = setenv("SDL_VIDEODRIVER", "wayland", 1); + // The private runtime directory has no display: the user's, by path. + const display = std.mem.span(std.c.getenv("WAYLAND_DISPLAY") orelse "wayland-0"); + if (display.len > 0 and display[0] != '/') + _ = setenv("WAYLAND_DISPLAY", try std.fmt.allocPrintSentinel(gpa, "/run/user/{d}/{s}", .{ std.c.getuid(), display }, 0), 1); + // Under the pty only for a stdin that stays open (test mode reads it). + var h = try eh.Harness.initArgv(gpa, exe, screen_rows, screen_cols, &.{(try l.path("medium.zig")).ptr}); + if (!liveExit(&h, 120_000)) return error.LiveDidNotExit; + try liveFrames(l, dump_path, null); +} + +/// p50 and p99, sorting `s`. +fn spread(s: []u32) [2]u32 { + std.mem.sort(u32, s, {}, std.sort.asc(u32)); + return .{ s[s.len / 2], s[@min(s.len - 1, s.len * 99 / 100)] }; +} + +fn measureLive(io: std.Io, exe: []const u8, gui: bool, reps: usize, json: bool) !void { + var l = try liveSetup(io); + defer liveCleanup(io, &l); + const exe_z = try gpa.dupeSentinel(u8, exe, 0); + for (0..reps) |_| { + if (gui) { + try liveKeysGui(&l, exe_z); + continue; + } + try liveKeysTty(&l, exe_z); + try liveYes(&l, exe_z); + try liveOpen(&l, exe_z); + } + const frontend = if (gui) "gui" else "tty"; + const out = std.Io.File.stdout(); + var buf: [256]u8 = undefined; + for (live_phases, &l.samples, &l.core) |name, *samples, *core| { + if (samples.items.len == 0) return error.LivePhaseUnmeasured; + const all = spread(samples.items); + const own = spread(core.items); + const line = if (json) + try std.fmt.bufPrint(&buf, "{{\"case\":\"live-{s}-{s}\",\"n\":{d},\"p50_us\":{d},\"p99_us\":{d},\"core_p50_us\":{d},\"core_p99_us\":{d}}}\n", .{ frontend, name, samples.items.len, all[0], all[1], own[0], own[1] }) + else + try std.fmt.bufPrint(&buf, "live {s} {s:<10} keystroke-to-present n {d:>4} p50 {d:>6} us p99 {d:>6} us, core p50 {d:>6} us p99 {d:>6} us\n", .{ frontend, name, samples.items.len, all[0], all[1], own[0], own[1] }); + try out.writeStreamingAll(io, line); + } + if (gui) return; + for (l.yes_ns.items, l.yes_frames.items) |ns, frames| { + const line = if (json) + try std.fmt.bufPrint(&buf, "{{\"case\":\"live-tty-yes-1g\",\"wall_ns\":{d},\"frames\":{d}}}\n", .{ ns, frames }) + else + try std.fmt.bufPrint(&buf, "live tty yes|head -c 1G: {d} ms wall, {d} frames\n", .{ ns / std.time.ns_per_ms, frames }); + try out.writeStreamingAll(io, line); + } + for (l.open_ns.items) |ns| { + const line = if (json) + try std.fmt.bufPrint(&buf, "{{\"case\":\"live-tty-open-big\",\"lines\":{d},\"wall_ns\":{d}}}\n", .{ big_lines, ns }) + else + try std.fmt.bufPrint(&buf, "live tty open {d}-line file: {d} ms to its first line\n", .{ big_lines, ns / std.time.ns_per_ms }); + try out.writeStreamingAll(io, line); + } +} + const MiniProbe = enum { generate, highlight_generate, render }; const MiniMeasurement = struct { cold_ns: u64 = 0, -- cgit v1.3