-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchmark.zig
More file actions
128 lines (105 loc) · 3.1 KB
/
benchmark.zig
File metadata and controls
128 lines (105 loc) · 3.1 KB
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
const std = @import("std");
const thread = @import("thread.zig");
const AtomicRing = @import("atomic.zig").Ring;
const MutexRing = @import("mutex.zig").Ring;
const stdout = std.io.getStdOut().writer();
fn producer(queue: anytype, start: *std.time.Instant, cpu: ?usize, warmup: usize, ops: usize) void {
if (cpu) |c| thread.setAffinity(c);
// warmup
for (0..warmup) |i| {
while (!queue.enqueue(i)) {}
}
while (!queue.isEmpty()) {
std.time.sleep(1000);
}
start.* = std.time.Instant.now() catch unreachable;
for (0..ops) |i| {
while (!queue.enqueue(i)) {}
}
}
fn consumer(queue: anytype, end: *std.time.Instant, cpu: ?usize, warmup: usize, ops: usize) void {
if (cpu) |c| thread.setAffinity(c);
// warm up
for (0..warmup) |i| {
while (true) {
if (queue.dequeue()) |c| {
if (c != i) {
@panic("it's over");
}
break;
}
}
}
for (0..ops) |i| {
while (true) {
if (queue.dequeue()) |c| {
if (c != i) {
@panic("it's over");
}
break;
}
}
}
end.* = std.time.Instant.now() catch unreachable;
}
const Benchmark = struct {
type: type,
ops: usize = 1_000_000_000,
cpus: ?[2]usize = null,
};
fn benchmark(opts: Benchmark) !void {
if (opts.cpus) |_| thread.setAffinity(0);
const warmup = opts.type.capacity * 4;
var queue = opts.type{};
var start: std.time.Instant = undefined;
var end: std.time.Instant = undefined;
var p = try std.Thread.spawn(.{}, producer, .{ &queue, &start, if (opts.cpus) |cpus| cpus[0] else null, warmup, opts.ops });
var c = try std.Thread.spawn(.{}, consumer, .{ &queue, &end, if (opts.cpus) |cpus| cpus[1] else null, warmup, opts.ops });
p.join();
c.join();
const elapsed = end.since(start);
try stdout.print("{any} - ops/sec: {d:.2}M\n", .{ opts.type, @as(f64, @floatFromInt(opts.ops)) / @as(f64, @floatFromInt(elapsed)) * 1_000 });
}
pub fn main() !void {
const sizes = [_]comptime_int{
8,
16,
32,
64,
128,
256,
512,
1024,
2048,
4096,
};
// This is based on AMD Ryzen 9 5950X
const cpus = .{
.{
"no affinity",
null,
},
.{
"same smt",
.{ 1, 17 },
},
.{
"same ccd",
.{ 1, 2 },
},
.{
"different ccd",
.{ 1, 8 },
},
};
inline for (cpus) |c| {
try stdout.print("=======================================\n", .{});
try stdout.print("topology: {s}, cpus: {any}\n", .{ c[0], c[1] });
try stdout.print("=======================================\n\n", .{});
inline for (sizes) |size| {
try benchmark(.{ .type = MutexRing(u64, size), .cpus = c[1] });
try benchmark(.{ .type = AtomicRing(u64, size), .cpus = c[1] });
try stdout.print("\n", .{});
}
}
}