/
benchmark.zig
498 lines (439 loc) · 16.3 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
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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
// zig run -O ReleaseFast --zig-lib-dir ../.. benchmark.zig
const std = @import("std");
const builtin = @import("builtin");
const time = std.time;
const Timer = time.Timer;
const hash = std.hash;
const KiB = 1024;
const MiB = 1024 * KiB;
const GiB = 1024 * MiB;
var prng = std.Random.DefaultPrng.init(0);
const random = prng.random();
const Hash = struct {
ty: type,
name: []const u8,
has_iterative_api: bool = true,
has_crypto_api: bool = false,
has_anytype_api: ?[]const comptime_int = null,
init_u8s: ?[]const u8 = null,
init_u64: ?u64 = null,
};
const hashes = [_]Hash{
Hash{
.ty = hash.XxHash3,
.name = "xxh3",
.init_u64 = 0,
.has_anytype_api = @as([]const comptime_int, &[_]comptime_int{ 8, 16, 32, 48, 64, 80, 96, 112, 128 }),
},
Hash{
.ty = hash.XxHash64,
.name = "xxhash64",
.init_u64 = 0,
.has_anytype_api = @as([]const comptime_int, &[_]comptime_int{ 8, 16, 32, 48, 64, 80, 96, 112, 128 }),
},
Hash{
.ty = hash.XxHash32,
.name = "xxhash32",
.init_u64 = 0,
.has_anytype_api = @as([]const comptime_int, &[_]comptime_int{ 8, 16, 32, 48, 64, 80, 96, 112, 128 }),
},
Hash{
.ty = hash.Wyhash,
.name = "wyhash",
.init_u64 = 0,
},
Hash{
.ty = hash.Fnv1a_64,
.name = "fnv1a",
},
Hash{
.ty = hash.Adler32,
.name = "adler32",
},
Hash{
.ty = hash.crc.Crc32,
.name = "crc32",
},
Hash{
.ty = hash.CityHash32,
.name = "cityhash-32",
.has_iterative_api = false,
},
Hash{
.ty = hash.CityHash64,
.name = "cityhash-64",
.has_iterative_api = false,
},
Hash{
.ty = hash.Murmur2_32,
.name = "murmur2-32",
.has_iterative_api = false,
},
Hash{
.ty = hash.Murmur2_64,
.name = "murmur2-64",
.has_iterative_api = false,
},
Hash{
.ty = hash.Murmur3_32,
.name = "murmur3-32",
.has_iterative_api = false,
},
Hash{
.ty = hash.SipHash64(1, 3),
.name = "siphash64",
.has_crypto_api = true,
.init_u8s = &[_]u8{0} ** 16,
},
Hash{
.ty = hash.SipHash128(1, 3),
.name = "siphash128",
.has_crypto_api = true,
.init_u8s = &[_]u8{0} ** 16,
},
};
const Result = struct {
hash: u64,
throughput: u64,
};
const block_size: usize = 8 * 8192;
pub fn benchmarkHash(comptime H: anytype, bytes: usize, allocator: std.mem.Allocator) !Result {
var blocks = try allocator.alloc(u8, bytes);
defer allocator.free(blocks);
random.bytes(blocks);
const block_count = bytes / block_size;
var h = blk: {
if (H.init_u8s) |init| {
break :blk H.ty.init(init[0..H.ty.key_length]);
}
if (H.init_u64) |init| {
break :blk H.ty.init(init);
}
break :blk H.ty.init();
};
var timer = try Timer.start();
for (0..block_count) |i| {
h.update(blocks[i * block_size ..][0..block_size]);
}
const final = if (H.has_crypto_api) @as(u64, @truncate(h.finalInt())) else h.final();
std.mem.doNotOptimizeAway(final);
const elapsed_ns = timer.read();
const elapsed_s = @as(f64, @floatFromInt(elapsed_ns)) / time.ns_per_s;
const size_float: f64 = @floatFromInt(block_size * block_count);
const throughput: u64 = @intFromFloat(size_float / elapsed_s);
return Result{
.hash = final,
.throughput = throughput,
};
}
pub fn benchmarkHashSmallKeys(comptime H: anytype, key_size: usize, bytes: usize, allocator: std.mem.Allocator) !Result {
var blocks = try allocator.alloc(u8, bytes);
defer allocator.free(blocks);
random.bytes(blocks);
const key_count = bytes / key_size;
var timer = try Timer.start();
var sum: u64 = 0;
for (0..key_count) |i| {
const small_key = blocks[i * key_size ..][0..key_size];
const final = blk: {
if (H.init_u8s) |init| {
if (H.has_crypto_api) {
break :blk @as(u64, @truncate(H.ty.toInt(small_key, init[0..H.ty.key_length])));
} else {
break :blk H.ty.hash(init, small_key);
}
}
if (H.init_u64) |init| {
break :blk H.ty.hash(init, small_key);
}
break :blk H.ty.hash(small_key);
};
sum +%= final;
}
const elapsed_ns = timer.read();
const elapsed_s = @as(f64, @floatFromInt(elapsed_ns)) / time.ns_per_s;
const size_float: f64 = @floatFromInt(key_count * key_size);
const throughput: u64 = @intFromFloat(size_float / elapsed_s);
std.mem.doNotOptimizeAway(sum);
return Result{
.hash = sum,
.throughput = throughput,
};
}
// the array and array pointer benchmarks for xxhash are very sensitive to in-lining,
// if you see strange performance changes consider using `.never_inline` or `.always_inline`
// to ensure the changes are not only due to the optimiser inlining the benchmark differently
pub fn benchmarkHashSmallKeysArrayPtr(
comptime H: anytype,
comptime key_size: usize,
bytes: usize,
allocator: std.mem.Allocator,
) !Result {
var blocks = try allocator.alloc(u8, bytes);
defer allocator.free(blocks);
random.bytes(blocks);
const key_count = bytes / key_size;
var timer = try Timer.start();
var sum: u64 = 0;
for (0..key_count) |i| {
const small_key = blocks[i * key_size ..][0..key_size];
const final: u64 = blk: {
if (H.init_u8s) |init| {
if (H.has_crypto_api) {
break :blk @truncate(H.ty.toInt(small_key, init[0..H.ty.key_length]));
} else {
break :blk H.ty.hash(init, small_key);
}
}
if (H.init_u64) |init| {
break :blk H.ty.hash(init, small_key);
}
break :blk H.ty.hash(small_key);
};
sum +%= final;
}
const elapsed_ns = timer.read();
const elapsed_s = @as(f64, @floatFromInt(elapsed_ns)) / time.ns_per_s;
const throughput: u64 = @intFromFloat(@as(f64, @floatFromInt(bytes)) / elapsed_s);
std.mem.doNotOptimizeAway(sum);
return Result{
.hash = sum,
.throughput = throughput,
};
}
// the array and array pointer benchmarks for xxhash are very sensitive to in-lining,
// if you see strange performance changes consider using `.never_inline` or `.always_inline`
// to ensure the changes are not only due to the optimiser inlining the benchmark differently
pub fn benchmarkHashSmallKeysArray(
comptime H: anytype,
comptime key_size: usize,
bytes: usize,
allocator: std.mem.Allocator,
) !Result {
var blocks = try allocator.alloc(u8, bytes);
defer allocator.free(blocks);
random.bytes(blocks);
const key_count = bytes / key_size;
var i: usize = 0;
var timer = try Timer.start();
var sum: u64 = 0;
while (i < key_count) : (i += 1) {
const small_key = blocks[i * key_size ..][0..key_size];
const final: u64 = blk: {
if (H.init_u8s) |init| {
if (H.has_crypto_api) {
break :blk @truncate(H.ty.toInt(small_key, init[0..H.ty.key_length]));
} else {
break :blk H.ty.hash(init, small_key.*);
}
}
if (H.init_u64) |init| {
break :blk H.ty.hash(init, small_key.*);
}
break :blk H.ty.hash(small_key.*);
};
sum +%= final;
}
const elapsed_ns = timer.read();
const elapsed_s = @as(f64, @floatFromInt(elapsed_ns)) / time.ns_per_s;
const throughput: u64 = @intFromFloat(@as(f64, @floatFromInt(bytes)) / elapsed_s);
std.mem.doNotOptimizeAway(sum);
return Result{
.hash = sum,
.throughput = throughput,
};
}
pub fn benchmarkHashSmallApi(comptime H: anytype, key_size: usize, bytes: usize, allocator: std.mem.Allocator) !Result {
var blocks = try allocator.alloc(u8, bytes);
defer allocator.free(blocks);
random.bytes(blocks);
const key_count = bytes / key_size;
var timer = try Timer.start();
var sum: u64 = 0;
for (0..key_count) |i| {
const small_key = blocks[i * key_size ..][0..key_size];
const final: u64 = blk: {
if (H.init_u8s) |init| {
if (H.has_crypto_api) {
break :blk @truncate(H.ty.toInt(small_key, init[0..H.ty.key_length]));
} else {
break :blk H.ty.hashSmall(init, small_key);
}
}
if (H.init_u64) |init| {
break :blk H.ty.hashSmall(init, small_key);
}
break :blk H.ty.hashSmall(small_key);
};
sum +%= final;
}
const elapsed_ns = timer.read();
const elapsed_s = @as(f64, @floatFromInt(elapsed_ns)) / time.ns_per_s;
const throughput: u64 = @intFromFloat(@as(f64, @floatFromInt(bytes)) / elapsed_s);
std.mem.doNotOptimizeAway(sum);
return Result{
.throughput = throughput,
.hash = sum,
};
}
fn usage() void {
std.debug.print(
\\throughput_test [options]
\\
\\Options:
\\ --filter [test-name]
\\ --seed [int]
\\ --count [int]
\\ --key-size [int]
\\ --iterative-only
\\ --help
\\
, .{});
}
fn mode(comptime x: comptime_int) comptime_int {
return if (builtin.mode == .Debug) x / 64 else x;
}
pub fn main() !void {
const stdout = std.io.getStdOut().writer();
var buffer: [1024]u8 = undefined;
var fixed = std.heap.FixedBufferAllocator.init(buffer[0..]);
const args = try std.process.argsAlloc(fixed.allocator());
var filter: ?[]u8 = "";
var count: usize = mode(128 * MiB);
var key_size: ?usize = null;
var seed: u32 = 0;
var test_iterative_only = false;
var test_arrays = false;
const default_small_key_size = 32;
var i: usize = 1;
while (i < args.len) : (i += 1) {
if (std.mem.eql(u8, args[i], "--mode")) {
try stdout.print("{}\n", .{builtin.mode});
return;
} else if (std.mem.eql(u8, args[i], "--seed")) {
i += 1;
if (i == args.len) {
usage();
std.process.exit(1);
}
seed = try std.fmt.parseUnsigned(u32, args[i], 10);
// we seed later
} else if (std.mem.eql(u8, args[i], "--filter")) {
i += 1;
if (i == args.len) {
usage();
std.process.exit(1);
}
filter = args[i];
} else if (std.mem.eql(u8, args[i], "--count")) {
i += 1;
if (i == args.len) {
usage();
std.process.exit(1);
}
const c = try std.fmt.parseUnsigned(usize, args[i], 10);
count = c * MiB;
} else if (std.mem.eql(u8, args[i], "--key-size")) {
i += 1;
if (i == args.len) {
usage();
std.process.exit(1);
}
key_size = try std.fmt.parseUnsigned(usize, args[i], 10);
if (key_size.? > block_size) {
try stdout.print("key_size cannot exceed block size of {}\n", .{block_size});
std.process.exit(1);
}
} else if (std.mem.eql(u8, args[i], "--iterative-only")) {
test_iterative_only = true;
} else if (std.mem.eql(u8, args[i], "--include-array")) {
test_arrays = true;
} else if (std.mem.eql(u8, args[i], "--help")) {
usage();
return;
} else {
usage();
std.process.exit(1);
}
}
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer std.testing.expect(gpa.deinit() == .ok) catch @panic("leak");
const allocator = gpa.allocator();
inline for (hashes) |H| {
if (filter == null or std.mem.indexOf(u8, H.name, filter.?) != null) hash: {
if (!test_iterative_only or H.has_iterative_api) {
try stdout.print("{s}\n", .{H.name});
// Always reseed prior to every call so we are hashing the same buffer contents.
// This allows easier comparison between different implementations.
if (H.has_iterative_api) {
prng.seed(seed);
const result = try benchmarkHash(H, count, allocator);
try stdout.print(" iterative: {:5} MiB/s [{x:0<16}]\n", .{ result.throughput / (1 * MiB), result.hash });
}
if (!test_iterative_only) {
if (key_size) |size| {
prng.seed(seed);
const result_small = try benchmarkHashSmallKeys(H, size, count, allocator);
try stdout.print(" small keys: {:3}B {:5} MiB/s {} Hashes/s [{x:0<16}]\n", .{
size,
result_small.throughput / (1 * MiB),
result_small.throughput / size,
result_small.hash,
});
if (!test_arrays) break :hash;
if (H.has_anytype_api) |sizes| {
inline for (sizes) |exact_size| {
if (size == exact_size) {
prng.seed(seed);
const result_array = try benchmarkHashSmallKeysArray(H, exact_size, count, allocator);
prng.seed(seed);
const result_ptr = try benchmarkHashSmallKeysArrayPtr(H, exact_size, count, allocator);
try stdout.print(" array: {:5} MiB/s [{x:0<16}]\n", .{
result_array.throughput / (1 * MiB),
result_array.hash,
});
try stdout.print(" array ptr: {:5} MiB/s [{x:0<16}]\n", .{
result_ptr.throughput / (1 * MiB),
result_ptr.hash,
});
}
}
}
} else {
prng.seed(seed);
const result_small = try benchmarkHashSmallKeys(H, default_small_key_size, count, allocator);
try stdout.print(" small keys: {:3}B {:5} MiB/s {} Hashes/s [{x:0<16}]\n", .{
default_small_key_size,
result_small.throughput / (1 * MiB),
result_small.throughput / default_small_key_size,
result_small.hash,
});
if (!test_arrays) break :hash;
if (H.has_anytype_api) |sizes| {
try stdout.print(" array:\n", .{});
inline for (sizes) |exact_size| {
prng.seed(seed);
const result = try benchmarkHashSmallKeysArray(H, exact_size, count, allocator);
try stdout.print(" {d: >3}B {:5} MiB/s [{x:0<16}]\n", .{
exact_size,
result.throughput / (1 * MiB),
result.hash,
});
}
try stdout.print(" array ptr: \n", .{});
inline for (sizes) |exact_size| {
prng.seed(seed);
const result = try benchmarkHashSmallKeysArrayPtr(H, exact_size, count, allocator);
try stdout.print(" {d: >3}B {:5} MiB/s [{x:0<16}]\n", .{
exact_size,
result.throughput / (1 * MiB),
result.hash,
});
}
}
}
}
}
}
}
}