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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
//! bench — hot-path timings for each subsystem.
//!
//! Local and comparative only: the numbers mean something against another run
//! on the same machine, and only as a ratio. This is here to catch
//! order-of-magnitude regressions, not to resolve a few percent. See
//! docs/benchmarks.md for what each case corresponds to upstream.
//!
//! Deliberately naive: fixed iteration counts, a short prime, no statistics.
//! If a number matters enough to argue about, measure it properly.
const std = @import("std");
const archive_mod = @import("internal/storage/archive.zig");
const cold = @import("internal/storage/cold.zig");
const gloom = @import("internal/storage/gloom.zig");
const segment = @import("internal/storage/segment.zig");
const tail_mod = @import("internal/serve/tail.zig");
const wire = @import("internal/serve/wire.zig");
const Io = std.Io;
const Allocator = std.mem.Allocator;
/// Reported per case. `unit_count` is whatever the case counts as one unit of
/// useful work (rows, or uncompressed payload bytes) so the derived throughput
/// column is honest about what it is dividing.
const Result = struct {
name: []const u8,
subsystem: []const u8,
iters: u64,
total_ns: u64,
unit_count: u64,
unit: []const u8,
fn nsPerOp(self: Result) f64 {
return @as(f64, @floatFromInt(self.total_ns)) / @as(f64, @floatFromInt(self.iters));
}
fn throughput(self: Result) f64 {
const secs = @as(f64, @floatFromInt(self.total_ns)) / std.time.ns_per_s;
if (secs == 0) return 0;
return @as(f64, @floatFromInt(self.unit_count)) / secs;
}
};
var results: std.ArrayList(Result) = .empty;
/// Nanoseconds between two Io timestamps. std.time.nanoTimestamp does not
/// exist in Zig 0.16; the rest of the codebase measures through Io the same way.
fn since(io: Io, start: Io.Timestamp) u64 {
return @intCast(@max(start.durationTo(Io.Timestamp.now(io, .awake)).nanoseconds, 0));
}
fn record(alloc: Allocator, r: Result) !void {
try results.append(alloc, r);
}
const payload = "\xa2\x64text\x6bhello world\x65$type\x72app.bsky.feed.post";
fn sampleEvent(i: usize) segment.Event {
return .{
.seq = 0,
.witnessed_at = 1_700_000_000_000_000 + @as(i64, @intCast(i)),
.indexed_at = 0,
.kind = .create,
.did = "did:plc:benchmarkaccount000",
.collection = "app.bsky.feed.post",
.rkey = "3l3qo2vuowo2b",
.rev = "3l3qo2vutsw2b",
.payload = payload,
};
}
/// Archive append under the two shapes upstream distinguishes: live is one row
/// at a time with the flush cadence that implies, backfill is batched.
fn benchArchiveAppend(alloc: Allocator, io: Io, comptime batched: bool) !Result {
var tmp_buf: [Io.Dir.max_path_bytes]u8 = undefined;
const dir = try std.fmt.bufPrint(&tmp_buf, "/tmp/stream-bench-{s}", .{if (batched) "backfill" else "live"});
Io.Dir.cwd().deleteTree(io, dir) catch {};
defer Io.Dir.cwd().deleteTree(io, dir) catch {};
var archive = try archive_mod.Archive.init(alloc, io, dir);
defer archive.deinit();
const iters: u64 = if (batched) 20_000 else 5_000;
const batch_size = 64;
var events: [batch_size]segment.Event = undefined;
const start = Io.Timestamp.now(io, .awake);
if (batched) {
var done: u64 = 0;
while (done < iters) : (done += batch_size) {
for (&events, 0..) |*e, j| e.* = sampleEvent(@intCast(done + j));
_ = try archive.appendBatch(&events, 1_700_000_000_000_000);
}
} else {
var i: u64 = 0;
while (i < iters) : (i += 1) {
_ = try archive.append(sampleEvent(@intCast(i)), 1_700_000_000_000_000);
}
}
try archive.flushBlock();
const elapsed = since(io, start);
archive.close();
return .{
.name = if (batched) "archive_append_backfill" else "archive_append_live",
.subsystem = "ingest->storage",
.iters = iters,
.total_ns = elapsed,
.unit_count = iters,
.unit = "rows/s",
};
}
/// Seal cost, then the two reader-open paths: checksum-verifying and not.
/// Splitting them is upstream's ReaderOpen vs ReaderOpenNoVerify.
fn benchSealAndParse(alloc: Allocator, io: Io) ![3]Result {
var tmp_buf: [Io.Dir.max_path_bytes]u8 = undefined;
const dir = try std.fmt.bufPrint(&tmp_buf, "/tmp/stream-bench-seal", .{});
Io.Dir.cwd().deleteTree(io, dir) catch {};
defer Io.Dir.cwd().deleteTree(io, dir) catch {};
var archive = try archive_mod.Archive.init(alloc, io, dir);
defer archive.deinit();
const rows: u64 = 20_000;
var i: u64 = 0;
while (i < rows) : (i += 1) _ = try archive.append(sampleEvent(@intCast(i)), 1_700_000_000_000_000);
const seal_start = Io.Timestamp.now(io, .awake);
try archive.rotate();
const seal_ns = since(io, seal_start);
var seg_buf: [Io.Dir.max_path_bytes]u8 = undefined;
const seg_path = try std.fmt.bufPrint(&seg_buf, "{s}/segments", .{dir});
var seg_dir = try Io.Dir.cwd().openDir(io, seg_path, .{ .iterate = true });
defer seg_dir.close(io);
const bytes = try seg_dir.readFileAlloc(io, "seg_0000000000.jss", alloc, .limited(1 << 30));
defer alloc.free(bytes);
archive.close();
const parse_iters: u64 = 2_000;
var verified_ns: u64 = 0;
{
const start = Io.Timestamp.now(io, .awake);
var n: u64 = 0;
while (n < parse_iters) : (n += 1) {
var sealed = try segment.Sealed.parse(alloc, bytes);
sealed.deinit(alloc);
}
verified_ns = since(io, start);
}
var unchecked_ns: u64 = 0;
{
const start = Io.Timestamp.now(io, .awake);
var n: u64 = 0;
while (n < parse_iters) : (n += 1) {
var sealed = try segment.Sealed.parseUnchecked(alloc, bytes);
sealed.deinit(alloc);
}
unchecked_ns = since(io, start);
}
return .{
.{
.name = "archive_seal",
.subsystem = "storage",
.iters = 1,
.total_ns = seal_ns,
.unit_count = rows,
.unit = "rows/s",
},
.{
.name = "sealed_parse",
.subsystem = "storage",
.iters = parse_iters,
.total_ns = verified_ns,
.unit_count = parse_iters,
.unit = "parses/s",
},
.{
.name = "sealed_parse_unchecked",
.subsystem = "storage",
.iters = parse_iters,
.total_ns = unchecked_ns,
.unit_count = parse_iters,
.unit = "parses/s",
},
};
}
/// Decoding one sealed block: the cold reader's inner loop.
fn benchSealedReadBlock(alloc: Allocator, io: Io) !Result {
var tmp_buf: [Io.Dir.max_path_bytes]u8 = undefined;
const dir = try std.fmt.bufPrint(&tmp_buf, "/tmp/stream-bench-block", .{});
Io.Dir.cwd().deleteTree(io, dir) catch {};
defer Io.Dir.cwd().deleteTree(io, dir) catch {};
var archive = try archive_mod.Archive.init(alloc, io, dir);
defer archive.deinit();
const rows: u64 = 20_000;
var i: u64 = 0;
while (i < rows) : (i += 1) _ = try archive.append(sampleEvent(@intCast(i)), 1_700_000_000_000_000);
try archive.rotate();
var seg_buf: [Io.Dir.max_path_bytes]u8 = undefined;
const seg_path = try std.fmt.bufPrint(&seg_buf, "{s}/segments", .{dir});
var seg_dir = try Io.Dir.cwd().openDir(io, seg_path, .{ .iterate = true });
defer seg_dir.close(io);
const bytes = try seg_dir.readFileAlloc(io, "seg_0000000000.jss", alloc, .limited(1 << 30));
defer alloc.free(bytes);
archive.close();
var sealed = try segment.Sealed.parse(alloc, bytes);
defer sealed.deinit(alloc);
const iters: u64 = 2_000;
var decoded_rows: u64 = 0;
const start = Io.Timestamp.now(io, .awake);
var n: u64 = 0;
while (n < iters) : (n += 1) {
var block = try sealed.readBlock(alloc, n % sealed.block_index.len);
decoded_rows += block.events.len;
block.deinit(alloc);
}
const elapsed = since(io, start);
return .{
.name = "sealed_read_block",
.subsystem = "storage",
.iters = iters,
.total_ns = elapsed,
.unit_count = decoded_rows,
.unit = "rows/s",
};
}
/// Per-block DID bloom lookup, consulted once per block per planSnapshot.
fn benchBloom(alloc: Allocator, io: Io) !Result {
var filter = try gloom.Filter.init(alloc, 10_000, 0.01);
defer filter.deinit(alloc);
var key_buf: [64]u8 = undefined;
var i: usize = 0;
while (i < 10_000) : (i += 1) {
const key = try std.fmt.bufPrint(&key_buf, "did:plc:bloomkey{d:0>12}", .{i});
filter.add(key);
}
const blob = try filter.marshal(alloc);
defer alloc.free(blob);
const iters: u64 = 200_000;
var hits: u64 = 0;
const start = Io.Timestamp.now(io, .awake);
var n: u64 = 0;
while (n < iters) : (n += 1) {
const key = try std.fmt.bufPrint(&key_buf, "did:plc:bloomkey{d:0>12}", .{n % 20_000});
if (try gloom.containsMarshaled(blob, key)) hits += 1;
}
const elapsed = since(io, start);
std.mem.doNotOptimizeAway(&hits);
return .{
.name = "bloom_lookup",
.subsystem = "storage",
.iters = iters,
.total_ns = elapsed,
.unit_count = iters,
.unit = "lookups/s",
};
}
/// Wire encoding, run once per row per subscriber wire.
fn benchWire(alloc: Allocator, io: Io, comptime v2: bool) !Result {
const cid = try @import("zat").cbor.Cid.forDagCbor(alloc, "bench");
defer alloc.free(cid.raw);
const rec: @import("zat").cbor.Value = .{ .map = &.{
.{ .key = "$type", .value = .{ .text = "app.bsky.feed.post" } },
.{ .key = "text", .value = .{ .text = "hello world" } },
} };
const op: wire.CommitOp = .{
.did = "did:plc:benchmarkaccount000",
.time_us = 1_700_000_000_000_000,
.rev = "3l3qo2vutsw2b",
.operation = "create",
.collection = "app.bsky.feed.post",
.rkey = "3l3qo2vuowo2b",
.record = rec,
.cid = cid,
};
const iters: u64 = 100_000;
var bytes_out: u64 = 0;
const start = Io.Timestamp.now(io, .awake);
var n: u64 = 0;
while (n < iters) : (n += 1) {
var buf: std.Io.Writer.Allocating = .init(alloc);
defer buf.deinit();
if (v2) {
try wire.encodeCommitV2(alloc, &buf.writer, op, n);
} else {
try wire.encodeCommit(alloc, &buf.writer, op);
}
bytes_out += buf.written().len;
}
const elapsed = since(io, start);
return .{
.name = if (v2) "wire_encode_v2" else "wire_encode_v1",
.subsystem = "serve",
.iters = iters,
.total_ns = elapsed,
.unit_count = iters,
.unit = "frames/s",
};
}
/// The cold reader's batch loop: what a subscriber behind the hot tail pays.
fn benchColdReplay(alloc: Allocator, io: Io, active: bool) !Result {
var tmp_buf: [Io.Dir.max_path_bytes]u8 = undefined;
const dir = try std.fmt.bufPrint(&tmp_buf, "/tmp/stream-bench-cold", .{});
Io.Dir.cwd().deleteTree(io, dir) catch {};
defer Io.Dir.cwd().deleteTree(io, dir) catch {};
var archive = try archive_mod.Archive.init(alloc, io, dir);
defer archive.deinit();
const rows: u64 = if (active) 500_000 else 20_000;
var i: u64 = 0;
while (i < rows) : (i += 1) _ = try archive.append(sampleEvent(@intCast(i)), 1_700_000_000_000_000);
if (active) try archive.flushBlock() else try archive.rotate();
var reader = cold.ColdReader.init(alloc, io, 64 << 20);
defer reader.deinit();
const Sink = struct {
delivered: u64 = 0,
fn wants(_: *@This(), _: wire.Kind, _: []const u8, _: []const u8, _: tail_mod.SkipV1) bool {
return true;
}
fn write(_: *@This(), _: cold.CachedFrame) anyerror!cold.WriteResult {
return .sent;
}
fn observe(self: *@This(), _: u64, result: cold.ReplayResult) anyerror!void {
if (result == .sent) self.delivered += 1;
}
};
var sink: Sink = .{};
const start = Io.Timestamp.now(io, .awake);
var from: u64 = if (active) rows - 20_000 + 1 else 1;
while (from <= rows) {
const batch = try reader.readSeqBatch(
&archive,
&sink,
Sink.wants,
&sink,
Sink.write,
Sink.observe,
false,
false,
from,
rows + 1,
1024,
);
if (batch.next_seq <= from) break;
from = batch.next_seq;
}
const elapsed = since(io, start);
if (sink.delivered != 20_000) return error.IncompleteReplay;
archive.close();
return .{
.name = if (active) "active_late_replay_batch" else "cold_replay_batch",
.subsystem = "serve",
.iters = sink.delivered,
.total_ns = elapsed,
.unit_count = sink.delivered,
.unit = "rows/s",
};
}
fn humanize(v: f64, buf: []u8) []const u8 {
if (v >= 1_000_000_000) return std.fmt.bufPrint(buf, "{d:.2}G", .{v / 1e9}) catch "?";
if (v >= 1_000_000) return std.fmt.bufPrint(buf, "{d:.2}M", .{v / 1e6}) catch "?";
if (v >= 1_000) return std.fmt.bufPrint(buf, "{d:.2}K", .{v / 1e3}) catch "?";
return std.fmt.bufPrint(buf, "{d:.0}", .{v}) catch "?";
}
pub fn main() !void {
// Production runs on c_allocator (src/main.zig). Benching on
// DebugAllocator would inflate every allocation-heavy path -- wire encode
// most of all -- and measure the harness rather than the system.
const alloc = std.heap.c_allocator;
defer results.deinit(alloc);
var threaded: Io.Threaded = .init(alloc, .{});
defer threaded.deinit();
const io = threaded.io();
try record(alloc, try benchArchiveAppend(alloc, io, false));
try record(alloc, try benchArchiveAppend(alloc, io, true));
const seal = try benchSealAndParse(alloc, io);
for (seal) |r| try record(alloc, r);
try record(alloc, try benchSealedReadBlock(alloc, io));
try record(alloc, try benchBloom(alloc, io));
try record(alloc, try benchWire(alloc, io, false));
try record(alloc, try benchWire(alloc, io, true));
try record(alloc, try benchColdReplay(alloc, io, false));
try record(alloc, try benchColdReplay(alloc, io, true));
var out_buf: [4096]u8 = undefined;
var stdout = Io.File.stdout().writer(io, &out_buf);
const w = &stdout.interface;
try w.print("{s:<26} {s:<16} {s:>12} {s:>14}\n", .{ "bench", "subsystem", "ns/op", "throughput" });
for (results.items) |r| {
var tbuf: [32]u8 = undefined;
var nbuf: [32]u8 = undefined;
try w.print("{s:<26} {s:<16} {s:>12} {s:>10} {s}\n", .{
r.name,
r.subsystem,
humanize(r.nsPerOp(), &nbuf),
humanize(r.throughput(), &tbuf),
r.unit,
});
}
try w.flush();
}