1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
const std = @import("std");
const index = @import("internal/collection_index/index.zig");
fn did(buf: []u8, n: usize) []const u8 {
return std.fmt.bufPrint(buf, "did:plc:{d:0>24}", .{n}) catch unreachable;
}
fn report(io: std.Io, label: []const u8, n: usize, start: std.Io.Timestamp) void {
const elapsed = start.durationTo(std.Io.Clock.awake.now(io)).toNanoseconds();
std.debug.print("{s},{d},{d}\n", .{ label, n, elapsed });
}
pub fn main(init: std.process.Init) !void {
const args = try init.minimal.args.toSlice(init.arena.allocator());
if (args.len < 2) return error.ExpectedScratchPath;
var batched = false;
var mixed = false;
var sync = false;
for (args[2..]) |arg| {
if (std.mem.eql(u8, arg, "--batch64")) batched = true else if (std.mem.eql(u8, arg, "--mixed")) mixed = true else if (std.mem.eql(u8, arg, "--sync")) sync = true else return error.InvalidOption;
}
const io = init.io;
var ci = try index.CollectionIndex.open(init.gpa, io, args[1]);
defer ci.deinit();
if (sync) return error.AlreadySynchronous;
const primary = "app.bsky.feed.post";
const secondary = "app.bsky.feed.like";
var key: [64]u8 = undefined;
var start = std.Io.Clock.awake.now(io);
if (batched) {
for (0..64) |chunk| {
var keys: [64][64]u8 = undefined;
var dids: [64][]const u8 = undefined;
for (&keys, &dids, 0..) |*bytes, *id, i| id.* = did(bytes, chunk * 64 + i);
try ci.addCollectionPage(&dids, primary);
try ci.addCollectionPage(&dids, secondary);
}
} else for (0..4096) |i| {
const id = did(&key, i);
try ci.addCollection(id, primary);
try ci.addCollection(id, secondary);
}
report(io, "add", 8192, start);
start = std.Io.Clock.awake.now(io);
if (batched) {
for (0..64) |chunk| {
var keys: [64][64]u8 = undefined;
var dids: [64][]const u8 = undefined;
for (&keys, &dids, 0..) |*bytes, *id, i| id.* = did(bytes, chunk * 64 + i);
try ci.addCollectionPage(&dids, primary);
}
} else for (0..4096) |i| try ci.addCollection(did(&key, i), primary);
report(io, "duplicate", 4096, start);
var buffer: [4096]u8 = undefined;
var spans: [100]index.Span = undefined;
start = std.Io.Clock.awake.now(io);
for (0..3000) |i| {
const offset = i * 997 % (4096 - 100);
const result = try ci.listReposByCollection(primary, spans.len, did(&key, offset), &buffer, &spans);
if (result.count != spans.len) return error.WrongCount;
var want: [64]u8 = undefined;
for (0..result.count) |j| {
if (!std.mem.eql(u8, result.getDid(j), did(&want, offset + 1 + j))) return error.WrongDid;
}
}
report(io, "page100", 3000, start);
if (mixed) try runMixed(&ci, io);
start = std.Io.Clock.awake.now(io);
for (0..512) |i| try ci.removeAll(did(&key, i * 8));
report(io, "removeAll", 512, start);
for (0..4096) |i| {
if (ci.hasCollections(did(&key, i)) != (i % 8 != 0)) return error.BadRemoval;
}
}
const Reader = struct {
ci: *index.CollectionIndex,
io: std.Io,
ready: *std.Io.Event,
samples: *[1000]i64,
outcome: anyerror!void = {},
fn run(self: *Reader) void {
self.ready.waitUncancelable(self.io);
self.outcome = self.sample();
}
fn sample(self: *Reader) !void {
var buffer: [4096]u8 = undefined;
var offsets: [100]index.Span = undefined;
var key: [64]u8 = undefined;
var want: [64]u8 = undefined;
for (self.samples, 0..) |*elapsed, i| {
const cursor = i * 997 % (4096 - 100);
const start = std.Io.Clock.awake.now(self.io);
const result = try self.ci.listReposByCollection("app.bsky.feed.post", 100, did(&key, cursor), &buffer, &offsets);
if (result.count != 100) return error.WrongCount;
for (0..100) |j| if (!std.mem.eql(u8, result.getDid(j), did(&want, cursor + 1 + j))) return error.WrongDid;
elapsed.* = @intCast(start.durationTo(std.Io.Clock.awake.now(self.io)).toNanoseconds());
}
}
};
fn runMixed(ci: *index.CollectionIndex, io: std.Io) !void {
var ready: std.Io.Event = .unset;
var scan_samples: [1000]i64 = undefined;
var writer_samples: [1024]i64 = undefined;
var reader: Reader = .{ .ci = ci, .io = io, .ready = &ready, .samples = &scan_samples };
const thread = try std.Thread.spawn(.{}, Reader.run, .{&reader});
var key: [64]u8 = undefined;
ready.set(io);
{
defer thread.join();
for (&writer_samples, 0..) |*elapsed, i| {
const start = std.Io.Clock.awake.now(io);
try ci.addCollection(did(&key, 4096 + i), "app.bsky.feed.post");
elapsed.* = @intCast(start.durationTo(std.Io.Clock.awake.now(io)).toNanoseconds());
}
}
try reader.outcome;
for (0..1024) |i| if (!ci.hasCollections(did(&key, 4096 + i))) return error.MissingWrite;
std.mem.sort(i64, &scan_samples, {}, std.sort.asc(i64));
std.mem.sort(i64, &writer_samples, {}, std.sort.asc(i64));
std.debug.print("mixed_scan_p99,1,{d}\nmixed_write_p99,1,{d}\n", .{ scan_samples[989], writer_samples[1013] });
}