authorgravatar for marc@tiehu.isMarc Tiehuis <marc@tiehu.is> 2018-01-17 19:25:00+13:00
committergravatar for marc@tiehu.isMarc Tiehuis <marc@tiehu.is> 2018-01-17 21:19:45+13:00
logdfd53634941ef560971d80dee4b3028e41c9174e
treef9db29d3deb18a04150e8d6c8be2025d2edbaf5d
parent7af53d08268a5c84188e1f4ca481e93186a03c44

Add throughput test program

Blake performance numbers for reference: ``` Cpu: Intel(R) Core(TM) i5-6500 CPU @ 3.20GHz ``` -- Blake2s ``` Zig --release-fast 485 Mb/s Zig --release-safe 377 Mb/s Zig 11 Mb/s ``` -- Blake2b ``` Zig --release-fast 616 Mb/s Zig --release-safe 573 Mb/s Zig 18 Mb/s ```

5 files changed, 55 insertions(+), 7 deletions(-)

std/crypto/blake2.zig+4
......@@ -21,6 +21,8 @@ pub const Blake2s256 = Blake2s(256);
2121
2222fn Blake2s(comptime out_len: usize) -> type { return struct {
2323 const Self = this;
24 const block_size = 64;
25 const digest_size = out_len / 8;
2426
2527 const iv = [8]u32 {
2628 0x6A09E667, 0xBB67AE85, 0x3C6EF372, 0xA54FF53A,
......@@ -236,6 +238,8 @@ pub const Blake2b512 = Blake2b(512);
236238
237239fn Blake2b(comptime out_len: usize) -> type { return struct {
238240 const Self = this;
241 const block_size = 128;
242 const digest_size = out_len / 8;
239243
240244 const iv = [8]u64 {
241245 0x6a09e667f3bcc908, 0xbb67ae8584caa73b,
std/crypto/md5.zig+2-6
......@@ -14,14 +14,10 @@ fn Rp(a: usize, b: usize, c: usize, d: usize, k: usize, s: u32, t: u32) -> Round
1414 return RoundParam { .a = a, .b = b, .c = c, .d = d, .k = k, .s = s, .t = t };
1515}
1616
17/// const hash1 = Md5.hash("my input");
18///
19/// const hasher = Md5.init();
20/// hasher.update("my ");
21/// hasher.update("input");
22/// const hash2 = hasher.final();
2317pub const Md5 = struct {
2418 const Self = this;
19 const block_size = 64;
20 const digest_size = 16;
2521
2622 s: [4]u32,
2723 // Streaming Cache
std/crypto/sha1.zig+2
......@@ -16,6 +16,8 @@ fn Rp(a: usize, b: usize, c: usize, d: usize, e: usize, i: u32) -> RoundParam {
1616
1717pub const Sha1 = struct {
1818 const Self = this;
19 const block_size = 64;
20 const digest_size = 20;
1921
2022 s: [5]u32,
2123 // Streaming Cache
std/crypto/sha2.zig+4-1
......@@ -58,6 +58,8 @@ pub const Sha256 = Sha2_32(Sha256Params);
5858
5959fn Sha2_32(comptime params: Sha2Params32) -> type { return struct {
6060 const Self = this;
61 const block_size = 64;
62 const digest_size = params.out_len / 8;
6163
6264 s: [8]u32,
6365 // Streaming Cache
......@@ -372,7 +374,8 @@ pub const Sha512 = Sha2_64(Sha512Params);
372374
373375fn Sha2_64(comptime params: Sha2Params64) -> type { return struct {
374376 const Self = this;
375 const u9 = @IntType(false, 9);
377 const block_size = 128;
378 const digest_size = params.out_len / 8;
376379
377380 s: [8]u64,
378381 // Streaming Cache
std/crypto/throughput_test.zig created+43
......@@ -0,0 +1,43 @@
1// Modify the HashFunction variable to the one wanted to test.
2//
3// NOTE: The throughput measurement may be slightly lower than other measurements since we run
4// through our block alignment functions as well. Be aware when comparing against other tests.
5//
6// ```
7// zig build-exe --release-fast --library c throughput_test.zig
8// ./throughput_test
9// ```
10const HashFunction = @import("md5.zig").Md5;
11const BytesToHash = 1024 * Mb;
12
13const std = @import("std");
14
15const c = @cImport({
16 @cInclude("time.h");
17});
18
19const Mb = 1024 * 1024;
20
21pub fn main() -> %void {
22 var stdout_file = try std.io.getStdOut();
23 var stdout_out_stream = std.io.FileOutStream.init(&stdout_file);
24 const stdout = &stdout_out_stream.stream;
25
26 var block: [HashFunction.block_size]u8 = undefined;
27 std.mem.set(u8, block[0..], 0);
28
29 var h = HashFunction.init();
30 var offset: usize = 0;
31
32 const start = c.clock();
33 while (offset < BytesToHash) : (offset += block.len) {
34 h.update(block[0..]);
35 }
36 const end = c.clock();
37
38 const elapsed_s = f64((end - start) * c.CLOCKS_PER_SEC) / 1000000;
39 const throughput = u64(BytesToHash / elapsed_s);
40
41 try stdout.print("{}: ", @typeName(HashFunction));
42 try stdout.print("{} Mb/s\n", throughput);
43}