const std = @import("std"); const zio = @import("zio"); const index = @import("internal/collection_index/index.zig"); const Io = std.Io; const collection = "app.bsky.feed.post"; const seeds = 8192; fn did(buf: []u8, n: usize) []const u8 { return std.fmt.bufPrint(buf, "did:plc:{d:0>24}", .{n}) catch unreachable; } fn now(io: Io) i96 { return Io.Clock.awake.now(io).nanoseconds; } const Probe = struct { ci: *index.CollectionIndex, io: Io, offset: usize, latest: usize, refresh_ns: i96 = 0, page_ns: i96 = 0, unchanged_refresh_ns: i96 = 0, task_ns: i96 = 0, fn read(self: *Probe) !void { const start = now(self.io); try self.ci.read_db.refresh(); const refreshed = now(self.io); self.refresh_ns = refreshed - start; var bytes: [4096]u8 = undefined; var spans: [100]index.Span = undefined; var cursor: [64]u8 = undefined; const page = try self.ci.listReposByCollection(collection, 100, did(&cursor, self.offset), &bytes, &spans); self.page_ns = now(self.io) - refreshed; if (page.count != 100) return error.ShortPage; var expected: [64]u8 = undefined; for (0..100) |i| { if (!std.mem.eql(u8, page.getDid(i), did(&expected, self.offset + i + 1))) return error.WrongPage; } const unchanged = now(self.io); try self.ci.read_db.refresh(); self.unchanged_refresh_ns = now(self.io) - unchanged; if (!try self.ci.hasCollectionsChecked(did(&expected, self.latest))) return error.StaleReader; } }; pub fn main(init: std.process.Init) !void { const args = try init.minimal.args.toSlice(init.arena.allocator()); if (args.len != 2) return error.ExpectedNewScratchPath; const gpa = std.heap.c_allocator; const runtime = try zio.Runtime.init(gpa, .{ .executors = .exact(1) }); defer runtime.deinit(); const io = runtime.io(); var out = Io.File.stdout().writer(init.io, &.{}); try out.interface.writeAll("case,mode,round,commits,refresh_ns,page_after_refresh_ns,unchanged_refresh_ns,task_ns,verified_pairs\n"); for ([_]bool{ false, true, true, false }, 0..) |native, case| { const path = try std.fmt.allocPrint(init.arena.allocator(), "{s}/{d}", .{ args[1], case }); const dir = try Io.Dir.cwd().createDirPathOpen(init.io, path, .{ .open_options = .{ .iterate = true } }); var entries = dir.iterate(); const first = try entries.next(init.io); dir.close(init.io); if (first != null) return error.ExistingData; var ci = try index.CollectionIndex.openWithReaderIo(gpa, init.io, io, path); var opened = true; defer if (opened) ci.deinit(); ci.maintenance_policy = .{ .max_segments = 4096, .max_memtable_bytes = 64 << 20 }; for (0..seeds / 64) |chunk| { var buffers: [64][64]u8 = undefined; var ids: [64][]const u8 = undefined; for (&buffers, &ids, 0..) |*buf, *id, i| id.* = did(buf, chunk * 64 + i); try ci.addCollectionPage(&ids, collection); } try ci.db.flush(); var rows: [8]Probe = undefined; const dispatch = if (native) runtime.blockingIo() else io; var warm: Probe = .{ .ci = &ci, .io = io, .offset = 0, .latest = seeds - 1 }; var warm_task = try dispatch.concurrent(Probe.read, .{&warm}); try warm_task.await(dispatch); for (&rows, 0..) |*probe, round| { for (0..32) |i| { var buf: [64]u8 = undefined; try ci.addCollection(did(&buf, seeds + round * 32 + i), collection); } probe.* = .{ .ci = &ci, .io = io, .offset = round * 997 % (seeds - 101), .latest = seeds + (round + 1) * 32 - 1 }; const submitted = now(init.io); var task = try dispatch.concurrent(Probe.read, .{probe}); try task.await(dispatch); probe.task_ns = now(init.io) - submitted; } ci.deinit(); opened = false; ci = try index.CollectionIndex.openWithReaderIo(gpa, init.io, io, path); opened = true; var verified: usize = 0; var cursor: ?[]const u8 = null; var cursor_buf: [64]u8 = undefined; while (true) { var bytes: [4096]u8 = undefined; var spans: [100]index.Span = undefined; const page = try ci.listReposByCollection(collection, 100, cursor, &bytes, &spans); for (0..page.count) |i| { var expected: [64]u8 = undefined; if (!std.mem.eql(u8, page.getDid(i), did(&expected, verified))) return error.WrongReopenedPage; verified += 1; } const last = page.last_did orelse break; @memcpy(cursor_buf[0..last.len], last); cursor = cursor_buf[0..last.len]; } if (verified != seeds + 256) return error.MissingReopenedPair; for (rows, 0..) |row, round| try out.interface.print("{d},{s},{d},32,{d},{d},{d},{d},{d}\n", .{ case, if (native) "whole-query-native" else "fiber", round, row.refresh_ns, row.page_ns, row.unchanged_refresh_ns, row.task_ns, verified }); } }