summaryrefslogtreecommitdiff
path: root/test/lspbench.zig
diff options
context:
space:
mode:
Diffstat (limited to 'test/lspbench.zig')
-rw-r--r--test/lspbench.zig196
1 files changed, 188 insertions, 8 deletions
diff --git a/test/lspbench.zig b/test/lspbench.zig
index 59cac155..08df9f4c 100644
--- a/test/lspbench.zig
+++ b/test/lspbench.zig
@@ -22,7 +22,7 @@ const Anchor = struct {
};
const anchors = [_]Anchor{
- .{ .file = "src/pardes.zig", .needle = "modal.cursorLineCount(text)", .at = 6, .kind = .definition, .expect = "modal.zig" },
+ .{ .file = "src/edit.zig", .needle = "modal.cursorLineCount(text)", .at = 6, .kind = .definition, .expect = "modal.zig" },
.{ .file = "src/lsp/lsp.zig", .needle = "lineCol(source", .at = 0, .kind = .definition, .expect = "lsp.zig" },
.{ .file = "src/lsp/lsp.zig", .needle = "std.mem.count(u8", .at = 8, .kind = .definition, .expect = "mem.zig" },
.{ .file = "src/lsp/lsp.zig", .needle = "lineCol(source", .at = 0, .kind = .hover, .expect = "" },
@@ -40,8 +40,7 @@ const anchors = [_]Anchor{
.{ .file = "src/lsp/lsp.zig", .needle = "pub const Kind", .at = 11, .kind = .workspace_symbols, .expect = "" },
.{ .file = "test/lspfixture/broken.zig", .needle = "unused_local", .at = 0, .kind = .workspace_diagnostics, .expect = "broken.zig" },
.{ .file = "src/lsp/lsp.zig", .needle = "std.mem.count(u8", .at = 4, .kind = .completion, .expect = "std.zig" },
- .{ .file = "src/lsp/lsp_zls.zig", .needle = ".declaration => try rowForToken", .at = 1, .kind = .completion, .expect = "type_definition" },
- .{ .file = "src/pardes.zig", .needle = ".definition => .definition,", .at = 1, .kind = .completion, .expect = "" },
+ .{ .file = "src/normal.zig", .needle = ".definition => .definition,", .at = 1, .kind = .completion, .expect = "" },
// The repeated name proves the result includes the declaration, not just the cursor line.
.{ .file = "test/lspfixture/dotcomplete.zig", .needle = "return switch (s) {\n .", .at = 29, .kind = .completion, .expect = "verdigris verdigris," },
// Indentation keeps the fixture header from becoming the first needle match.
@@ -111,10 +110,16 @@ test "lspbench options reject unknown flags and multiple roots before probing" {
try std.testing.expectError(error.MultipleRoots, Options.parse(&.{ "first", "--check", "second" }));
}
+/// The process's Io, for the queries: their waits are woken by the client's
+/// reader threads, which a single-threaded Io cannot be.
+var query_io: std.Io = undefined;
+
pub fn main(init: std.process.Init) !void {
const gpa = std.heap.page_allocator;
+ query_io = init.io;
const args = try init.minimal.args.toSlice(init.arena.allocator());
+ if (args.len > 1 and std.mem.eql(u8, args[1], "probe")) return probeCommand(init);
const options = try Options.parse(args[1..]);
const rss0 = rssKib();
@@ -171,13 +176,19 @@ fn checkResults(results: []const Result, required: usize) !void {
for (results) |r| if (r.empty or !r.correct) return error.IncorrectProbe;
}
-fn runOnce(gpa: std.mem.Allocator, req: lsp.Req, expected: []const u8, r: *Result) !u64 {
- var arena: std.heap.ArenaAllocator = .init(gpa);
- defer arena.deinit();
+fn runOnce(gpa: std.mem.Allocator, req_in: lsp.Req, expected: []const u8, r: *Result) !u64 {
+ // The editor's own path (host_io.Lsp.work): the query in the fixed
+ // region reserved at startup, not a heap arena that maps its memory anew.
+ const memory = @import("pardes").memory;
+ const region = memory.takeLspQuery() orelse return error.QueryRegionBusy;
+ var arena: std.heap.FixedBufferAllocator = .init(region);
+ defer memory.giveLspQuery(arena.end_index);
+ var req = req_in;
+ req.budget = &arena;
var out: std.Io.Writer.Allocating = .init(gpa);
defer out.deinit();
const t0 = nowNs();
- try lsp.query(gpa, arena.allocator(), req, &out.writer);
+ try lsp.query(query_io, gpa, arena.allocator(), req, &out.writer);
const ns = nowNs() -| t0;
r.accept(out.written(), expected);
return ns / 1000;
@@ -216,7 +227,7 @@ fn reportText(rs: []const Result, rss: u64) void {
const o = std.debug.print;
o("backend: {s}\n\n", .{lsp.backend_name});
o("{s:<24} {s:>9} {s:>7} {s:>10} {s:>10} {s}\n", .{ "kind", "supports", "rows", "cold(us)", "warm(us)", "verdict" });
- o("{s}\n", .{"-" ** 78});
+ o("{s}\n", .{&@as([78]u8, @splat('-'))});
var claimed: usize = 0;
var working: usize = 0;
var lies: usize = 0;
@@ -296,3 +307,172 @@ fn rssKib() u64 {
}
return 0;
}
+
+// ---- probe: one query from the command line ----
+
+fn nowUs() u64 {
+ var ts: std.c.timespec = undefined;
+ _ = std.c.clock_gettime(.MONOTONIC, &ts);
+ return @as(u64, @intCast(ts.sec)) *| 1_000_000 +| @as(u64, @intCast(ts.nsec)) / 1000;
+}
+
+const aliases = .{
+ .{ "gd", lsp.Kind.definition },
+ .{ "gD", lsp.Kind.declaration },
+ .{ "gy", lsp.Kind.type_definition },
+ .{ "gi", lsp.Kind.implementation },
+ .{ "gr", lsp.Kind.references },
+};
+
+fn probeUsage() noreturn {
+ std.debug.print(
+ \\usage: pardes-lspbench probe <kind> <file> <line>:<col> [arg] [--reps N] [--warm S]
+ \\ kind: gd gD gy gi gr, or any lsp.Kind tag (hover, references,
+ \\ document_symbols, workspace_symbols, incoming_calls, ...)
+ \\ arg: the new name for rename, the query for workspace_symbols
+ \\ --warm sleep S seconds after the first (cold) query — long enough
+ \\ for an indexing server to reach ready, narrated on stderr
+ \\
+ , .{});
+ std.process.exit(2);
+}
+
+/// The editor's message row, here: the client's reader threads narrate server
+/// state through the same sink the shells register, printed to stderr so the
+/// rows on stdout stay clean.
+fn statusSink(ctx: ?*anyopaque, text: []const u8) void {
+ _ = ctx;
+ std.debug.print("# status: {s}\n", .{text});
+}
+
+extern "c" fn usleep(usec: c_uint) c_int;
+
+/// `lspbench probe`: one language query against the real seam, from the
+/// command line (the old lspprobe): `probe <kind> <file> <line>:<col> [arg]
+/// [--reps N] [--warm S]`, rows on stdout, the timing on stderr.
+fn probeCommand(init: std.process.Init) !void {
+ const gpa = std.heap.page_allocator;
+ query_io = init.io;
+ const arena = init.arena.allocator();
+ const io = std.Io.Threaded.global_single_threaded.io();
+
+ // args[1] is `probe`: the rest are where lspprobe's were.
+ const all = try init.minimal.args.toSlice(arena);
+ const args = all[1..];
+ if (args.len < 4) probeUsage();
+
+ // comma-separated kinds share ONE server instance and its index — the
+ // whole battery against a big workspace pays the cold cost once
+ var kinds: [16]lsp.Kind = undefined;
+ var nkinds: usize = 0;
+ var kit = std.mem.tokenizeScalar(u8, args[1], ',');
+ while (kit.next()) |word| {
+ if (nkinds == kinds.len) probeUsage();
+ kinds[nkinds] = blk: {
+ inline for (aliases) |a| if (std.mem.eql(u8, word, a[0])) break :blk a[1];
+ break :blk std.meta.stringToEnum(lsp.Kind, word) orelse probeUsage();
+ };
+ nkinds += 1;
+ }
+ if (nkinds == 0) probeUsage();
+
+ var pz: [4096:0]u8 = undefined;
+ const pz_s = std.fmt.bufPrintSentinel(&pz, "{s}", .{args[2]}, 0) catch probeUsage();
+ var realbuf: [4096]u8 = undefined;
+ const path = std.mem.span(realpath(pz_s.ptr, &realbuf) orelse {
+ std.debug.print("cannot resolve {s}\n", .{args[2]});
+ std.process.exit(1);
+ });
+ const source = std.Io.Dir.cwd().readFileAllocOptions(
+ io,
+ path,
+ arena,
+ .limited(64 << 20),
+ .of(u8),
+ 0,
+ ) catch {
+ std.debug.print("cannot read {s}\n", .{path});
+ std.process.exit(1);
+ };
+
+ const colon = std.mem.indexOfScalar(u8, args[3], ':') orelse probeUsage();
+ const line = std.fmt.parseInt(u32, args[3][0..colon], 10) catch probeUsage();
+ const col = std.fmt.parseInt(u32, args[3][colon + 1 ..], 10) catch probeUsage();
+ if (line == 0 or col == 0) probeUsage();
+
+ var arg: []const u8 = "";
+ var reps: u32 = 1;
+ var warm_s: u32 = 0;
+ var i: usize = 4;
+ while (i < args.len) : (i += 1) {
+ if (std.mem.eql(u8, args[i], "--reps")) {
+ i += 1;
+ if (i >= args.len) probeUsage();
+ reps = @max(1, std.fmt.parseInt(u32, args[i], 10) catch probeUsage());
+ } else if (std.mem.eql(u8, args[i], "--warm")) {
+ i += 1;
+ if (i >= args.len) probeUsage();
+ warm_s = std.fmt.parseInt(u32, args[i], 10) catch probeUsage();
+ } else arg = args[i];
+ }
+
+ lsp.setStatusSink(null, statusSink);
+
+ // 1-based line:col -> byte offset
+ var off: usize = 0;
+ var ln: u32 = 1;
+ while (ln < line and off < source.len) : (off += 1) {
+ if (source[off] == '\n') ln += 1;
+ }
+ off = @min(off + col - 1, source.len);
+
+ const root = std.fs.path.dirname(path) orelse "/";
+ const timings = try arena.alloc(u64, reps);
+ var stdout_buf: [64 * 1024]u8 = undefined;
+ var stdout = std.Io.File.stdout().writer(io, &stdout_buf);
+ var first_query = true;
+
+ for (kinds[0..nkinds]) |kind| {
+ var rows: []const u8 = "";
+ for (0..reps) |r| {
+ // one warm pause for the whole battery, right after the very
+ // first (cold, server-spawning) query — the status lines on
+ // stderr narrate what the server does with the time
+ var out: std.Io.Writer.Allocating = .init(gpa);
+ defer out.deinit();
+ var scratch: std.heap.ArenaAllocator = .init(gpa);
+ defer scratch.deinit();
+ const t0 = nowUs();
+ try lsp.query(query_io, gpa, scratch.allocator(), .{
+ .kind = kind,
+ .path = path,
+ .source = source,
+ .offset = @intCast(off),
+ .arg = arg,
+ .root = root,
+ }, &out.writer);
+ timings[r] = nowUs() -| t0;
+ rows = try arena.dupe(u8, out.written());
+ if (first_query) {
+ first_query = false;
+ if (warm_s > 0) for (0..warm_s * 10) |_| {
+ _ = usleep(100_000);
+ };
+ }
+ }
+
+ try stdout.interface.print("## {t}\n", .{kind});
+ try stdout.interface.writeAll(rows);
+ try stdout.interface.flush();
+
+ const n = std.mem.count(u8, rows, "\n");
+ const cold = timings[0];
+ std.mem.sort(u64, timings[0..reps], {}, std.sort.asc(u64));
+ if (reps == 1)
+ std.debug.print("# {t}: {d} row(s) in {d}us\n", .{ kind, n, cold })
+ else
+ std.debug.print("# {t}: {d} row(s); first {d}us, min {d}us, median {d}us ({d} reps)\n", .{
+ kind, n, cold, timings[0], timings[reps / 2], reps,
+ });
+ }
+}