summaryrefslogtreecommitdiff
path: root/test/differential/probe.zig
blob: ec7c88e11a4995a65d18e2e0c6db2272240d4470 (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
const std = @import("std");
const c9 = @import("cloud9");
const libc = std.c;
var reads: u64 = 0;
var writes: u64 = 0;
var bytes_in: u64 = 0;
var bytes_out: u64 = 0;

fn readAll(bytes: []u8, chunk: usize) !void {
    var offset: usize = 0;
    while (offset < bytes.len) {
        reads += 1;
        const n = libc.read(0, bytes[offset..].ptr, @min(chunk, bytes.len - offset));
        if (n <= 0) return error.Input;
        offset += @intCast(n);
        bytes_in += @intCast(n);
    }
}
fn writeAll(bytes: []const u8, chunk: usize) !void {
    var offset: usize = 0;
    while (offset < bytes.len) {
        writes += 1;
        const n = libc.write(1, bytes[offset..].ptr, @min(chunk, bytes.len - offset));
        if (n <= 0) return error.Output;
        offset += @intCast(n);
        bytes_out += @intCast(n);
    }
}
fn nowNs() u64 {
    var ts: libc.timespec = undefined;
    std.debug.assert(libc.clock_gettime(.MONOTONIC, &ts) == 0);
    return @as(u64, @intCast(ts.sec)) * std.time.ns_per_s + @as(u64, @intCast(ts.nsec));
}
pub fn main() !void {
    var header: [12]u8 = undefined;
    try readAll(&header, header.len);
    const count = std.mem.readInt(u32, header[0..4], .little);
    const repeats = std.mem.readInt(u32, header[4..8], .little);
    const chunk = std.mem.readInt(u32, header[8..12], .little);
    if (count > 1_000_000 or repeats == 0 or repeats > 10000 or chunk == 0) return error.Options;
    var input: [65536]u8 = undefined;
    var output: [65536]u8 = undefined;
    var elapsed_ns: u64 = 0;
    for (0..count) |_| {
        try readAll(input[0..4], chunk);
        const len = c9.frameLen(input[0..4]).?;
        if (len < c9.header_len or len > input.len) return error.Frame;
        try readAll(input[4..len], chunk);
        const start = nowNs();
        for (0..repeats) |_| {
            const decoded = try c9.decode(input[0..len]);
            const encoded = try c9.encode(decoded.msg, decoded.tag, &output);
            std.mem.doNotOptimizeAway(encoded);
        }
        elapsed_ns += nowNs() - start;
        try writeAll(output[0..len], chunk);
    }
    std.debug.print("{{\"implementation\":\"cloud9\",\"codec_ns\":{d},\"operations\":{d},\"read_calls\":{d},\"write_calls\":{d},\"bytes_in\":{d},\"bytes_out\":{d},\"codec_allocations\":0,\"allocation_evidence\":\"no allocator or allocation calls in codec\"}}\n", .{ elapsed_ns, @as(u64, count) * repeats, reads, writes, bytes_in - header.len, bytes_out });
}