summaryrefslogtreecommitdiff
path: root/test/perf.zig
diff options
context:
space:
mode:
Diffstat (limited to 'test/perf.zig')
-rw-r--r--test/perf.zig302
1 files changed, 301 insertions, 1 deletions
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,