summaryrefslogtreecommitdiff
path: root/bench/store.zig
blob: a3d5e9acf2db4661db532e40ce87335a12faa124 (plain) (blame)
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
//! What a recording costs: bytes and time a moment, for the shapes of work
//! the backends produce. `zig build bench-store`.
const std = @import("std");
const zt = @import("ztrace");

const moments = 100_000;

const Case = struct {
    name: []const u8,
    /// Emits one moment's events, ending with the one that cuts it.
    run: *const fn (rec: *zt.Recorder, i: u64) anyerror!void,
};

fn step(rec: *zt.Recorder, i: u64) !void {
    const regs: zt.Regs = .{ .rip = 0x401000 + i % 512, .rsp = 0x7ffd_0000_1000 - 8 * (i % 64), .rax = i };
    try rec.emit(.{ .kind = .step, .tid = 1, .a = .{ regs.rip, 0, 0, 0, 0, 0 } }, std.mem.asBytes(&regs));
}

fn mark(rec: *zt.Recorder, i: u64) !void {
    try rec.emit(.{ .kind = .mark, .tid = 1, .a = .{ i, 0, 0, 0, 0, 0 } }, "");
}

fn write(rec: *zt.Recorder, addr: u64, bytes: []const u8) !void {
    try rec.emit(.{ .kind = .mem_write, .tid = 1, .a = .{ addr, bytes.len, 0, 0, 0, 0 } }, bytes);
}

const cases = [_]Case{
    .{ .name = "a mark", .run = mark },
    .{ .name = "a step with registers", .run = step },
    .{ .name = "... and a word pushed on the stack", .run = struct {
        fn run(rec: *zt.Recorder, i: u64) !void {
            try write(rec, 0x7ffd_0000_1000 - 8 * (i % 64), std.mem.asBytes(&i));
            try step(rec, i);
        }
    }.run },
    .{ .name = "a word written somewhere in 1 MiB of heap", .run = struct {
        fn run(rec: *zt.Recorder, i: u64) !void {
            try write(rec, 0x5555_0000_0000 + (i *% 0x9e3779b97f4a7c15 >> 44) * 8, std.mem.asBytes(&i));
            try mark(rec, i);
        }
    }.run },
    .{ .name = "4 KiB of zeros written (a calloc, a fresh page)", .run = struct {
        fn run(rec: *zt.Recorder, i: u64) !void {
            const zeros: [4096]u8 = @splat(0);
            try write(rec, 0x5555_0000_0000 + (i % 256) * 4096, &zeros);
            try mark(rec, i);
        }
    }.run },
    .{ .name = "4 KiB of the same text written (a buffer filled again)", .run = struct {
        fn run(rec: *zt.Recorder, i: u64) !void {
            var text: [4096]u8 = undefined;
            for (&text, 0..) |*c, at| c.* = "the quick brown fox "[at % 20];
            try write(rec, 0x5555_0000_0000 + (i % 256) * 4096, &text);
            try mark(rec, i);
        }
    }.run },
    .{ .name = "4 KiB of new bytes written (a read from a file)", .run = struct {
        fn run(rec: *zt.Recorder, i: u64) !void {
            var bytes: [4096]u8 = undefined;
            var rng: std.Random.DefaultPrng = .init(i);
            rng.random().bytes(&bytes);
            try write(rec, 0x5555_0000_0000 + (i % 256) * 4096, &bytes);
            try mark(rec, i);
        }
    }.run },
};

pub fn main(init: std.process.Init) !void {
    const path = "bench-store.ztrace";
    defer std.Io.Dir.cwd().deleteFile(init.io, path) catch {};
    std.debug.print("{s:<56} {s:>12} {s:>12}\n", .{ "each moment", "bytes", "ns" });
    for (cases) |case| {
        var rec = try zt.Recorder.create(init.gpa, init.io, path);
        const n: u64 = if (std.mem.startsWith(u8, case.name, "4 KiB")) moments / 10 else moments;
        const start = zt.event.now();
        for (0..n) |i| try case.run(&rec, i);
        try rec.close();
        const ns = zt.event.now() - start;
        const size = (try std.Io.Dir.cwd().statFile(init.io, path, .{})).size;
        std.debug.print("{s:<56} {d:>12} {d:>12}\n", .{ case.name, size / n, ns / n });
    }
}