-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbenchmark.zig
78 lines (62 loc) · 2.59 KB
/
benchmark.zig
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
const std = @import("std");
const Block = @import("../../radio.zig").Block;
const ProcessResult = @import("../../radio.zig").ProcessResult;
////////////////////////////////////////////////////////////////////////////////
// Benchmark Sink
////////////////////////////////////////////////////////////////////////////////
pub fn BenchmarkSink(comptime T: type) type {
return struct {
const Self = @This();
pub const Options = struct {
title: []const u8 = "BenchmarkSink",
report_period_ms: usize = 3000,
};
block: Block,
options: Options,
count: usize = 0,
tic_ms: u64 = 0,
pub fn init(options: Options) Self {
return .{ .block = Block.init(@This()), .options = options };
}
pub fn initialize(self: *Self, _: std.mem.Allocator) !void {
self.count = 0;
self.tic_ms = @as(u64, @intCast(std.time.milliTimestamp()));
}
pub fn deinitialize(self: *Self, _: std.mem.Allocator) void {
const toc_ms = @as(u64, @intCast(std.time.milliTimestamp()));
self.report(toc_ms);
}
fn normalize(amount: f32) struct { f32, []const u8 } {
if (amount > 1e9) {
return .{ amount / 1e9, "G" };
} else if (amount > 1e6) {
return .{ amount / 1e6, "M" };
} else if (amount > 1e3) {
return .{ amount / 1e3, "K" };
} else {
return .{ amount, "" };
}
}
fn report(self: *Self, toc_ms: u64) void {
// Compute rate
const sps = @as(f32, @floatFromInt(1000 * self.count)) / @as(f32, @floatFromInt(toc_ms - self.tic_ms));
const bps = sps * @sizeOf(T);
// Normalize rates with unit prefix
const normalized_sps = normalize(sps);
const normalized_bps = normalize(bps);
// Print report string
std.debug.print("[{s}] {d:.2} {s}S/s ({d:.2} {s}B/s)\n", .{ self.options.title, normalized_sps[0], normalized_sps[1], normalized_bps[0], normalized_bps[1] });
// Reset tic and count
self.tic_ms = toc_ms;
self.count = 0;
}
pub fn process(self: *Self, x: []const T) !ProcessResult {
self.count += x.len;
const toc_ms = @as(u64, @intCast(std.time.milliTimestamp()));
if (toc_ms - self.tic_ms > self.options.report_period_ms) {
self.report(toc_ms);
}
return ProcessResult.init(&[1]usize{x.len}, &[0]usize{});
}
};
}