Commit c050ec4e57
Changed files (4)
std
hash
std/crypto/throughput_test.zig → std/crypto/benchmark.zig
@@ -1,8 +1,10 @@
+// zig run benchmark.zig --release-fast --override-std-dir ..
+
const builtin = @import("builtin");
-const std = @import("std");
+const std = @import("../std.zig");
const time = std.time;
const Timer = time.Timer;
-const crypto = @import("../crypto.zig");
+const crypto = std.crypto;
const KiB = 1024;
const MiB = 1024 * KiB;
@@ -14,7 +16,7 @@ const Crypto = struct {
name: []const u8,
};
-const hashes = []Crypto{
+const hashes = [_]Crypto{
Crypto{ .ty = crypto.Md5, .name = "md5" },
Crypto{ .ty = crypto.Sha1, .name = "sha1" },
Crypto{ .ty = crypto.Sha256, .name = "sha256" },
@@ -45,7 +47,7 @@ pub fn benchmarkHash(comptime Hash: var, comptime bytes: comptime_int) !u64 {
return throughput;
}
-const macs = []Crypto{
+const macs = [_]Crypto{
Crypto{ .ty = crypto.Poly1305, .name = "poly1305" },
Crypto{ .ty = crypto.HmacMd5, .name = "hmac-md5" },
Crypto{ .ty = crypto.HmacSha1, .name = "hmac-sha1" },
@@ -75,7 +77,7 @@ pub fn benchmarkMac(comptime Mac: var, comptime bytes: comptime_int) !u64 {
return throughput;
}
-const exchanges = []Crypto{Crypto{ .ty = crypto.X25519, .name = "x25519" }};
+const exchanges = [_]Crypto{Crypto{ .ty = crypto.X25519, .name = "x25519" }};
pub fn benchmarkKeyExchange(comptime DhKeyExchange: var, comptime exchange_count: comptime_int) !u64 {
std.debug.assert(DhKeyExchange.minimum_key_length >= DhKeyExchange.secret_length);
@@ -135,13 +137,16 @@ pub fn main() !void {
var buffer: [1024]u8 = undefined;
var fixed = std.heap.FixedBufferAllocator.init(buffer[0..]);
- const args = try std.os.argsAlloc(&fixed.allocator);
+ const args = try std.process.argsAlloc(&fixed.allocator);
var filter: ?[]u8 = "";
var i: usize = 1;
while (i < args.len) : (i += 1) {
- if (std.mem.eql(u8, args[i], "--seed")) {
+ 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/crypto/blake2.zig
@@ -269,8 +269,8 @@ pub const Blake2b512 = Blake2b(512);
fn Blake2b(comptime out_len: usize) type {
return struct {
const Self = @This();
- const block_length = 128;
- const digest_length = out_len / 8;
+ pub const block_length = 128;
+ pub const digest_length = out_len / 8;
const iv = [8]u64{
0x6a09e667f3bcc908,
std/crypto/sha2.zig
@@ -420,8 +420,8 @@ pub const Sha512 = Sha2_64(Sha512Params);
fn Sha2_64(comptime params: Sha2Params64) type {
return struct {
const Self = @This();
- const block_length = 128;
- const digest_length = params.out_len / 8;
+ pub const block_length = 128;
+ pub const digest_length = params.out_len / 8;
s: [8]u64,
// Streaming Cache
std/hash/throughput_test.zig → std/hash/benchmark.zig
@@ -1,3 +1,5 @@
+// zig run benchmark.zig --release-fast --override-std-dir ..
+
const builtin = @import("builtin");
const std = @import("std");
const time = std.time;
@@ -32,6 +34,8 @@ const Result = struct {
throughput: u64,
};
+const block_size: usize = 8192;
+
pub fn benchmarkHash(comptime H: var, bytes: usize) !Result {
var h = blk: {
if (H.init_u8s) |init| {
@@ -43,7 +47,7 @@ pub fn benchmarkHash(comptime H: var, bytes: usize) !Result {
break :blk H.ty.init();
};
- var block: [8192]u8 = undefined;
+ var block: [block_size]u8 = undefined;
prng.random.bytes(block[0..]);
var offset: usize = 0;
@@ -63,6 +67,43 @@ pub fn benchmarkHash(comptime H: var, bytes: usize) !Result {
};
}
+pub fn benchmarkHashSmallKeys(comptime H: var, key_size: usize, bytes: usize) !Result {
+ const key_count = bytes / key_size;
+ var block: [block_size]u8 = undefined;
+ prng.random.bytes(block[0..]);
+
+ var i: usize = 0;
+ var timer = try Timer.start();
+ const start = timer.lap();
+
+ var sum: u64 = 0;
+ while (i < key_count) : (i += 1) {
+ const o = i % (block_size - key_size);
+ const small_key = block[o .. key_size + o];
+
+ const result = blk: {
+ if (H.init_u8s) |init| {
+ 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 +%= result;
+ }
+ const end = timer.read();
+
+ const elapsed_s = @intToFloat(f64, end - start) / time.ns_per_s;
+ const throughput = @floatToInt(u64, @intToFloat(f64, bytes) / elapsed_s);
+
+ return Result{
+ .hash = sum,
+ .throughput = throughput,
+ };
+}
+
fn usage() void {
std.debug.warn(
\\throughput_test [options]
@@ -100,10 +141,14 @@ pub fn main() !void {
var filter: ?[]u8 = "";
var count: usize = mode(128 * MiB);
+ var key_size: usize = 32;
var i: usize = 1;
while (i < args.len) : (i += 1) {
- if (std.mem.eql(u8, args[i], "--seed")) {
+ 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();
@@ -127,8 +172,20 @@ pub fn main() !void {
std.os.exit(1);
}
- const c = try std.fmt.parseUnsigned(u32, args[i], 10);
+ 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.os.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.os.exit(1);
+ }
} else if (std.mem.eql(u8, args[i], "--help")) {
usage();
return;
@@ -141,8 +198,11 @@ pub fn main() !void {
inline for (hashes) |H| {
if (filter == null or std.mem.indexOf(u8, H.name, filter.?) != null) {
const result = try benchmarkHash(H, count);
- try printPad(stdout, H.name);
- try stdout.print(": {:4} MiB/s [{:16}]\n", result.throughput / (1 * MiB), result.hash);
+ const result_small = try benchmarkHashSmallKeys(H, key_size, count);
+
+ try stdout.print("{}\n", H.name);
+ try stdout.print(" iterative: {:4} MiB/s [{x:0<16}]\n", result.throughput / (1 * MiB), result.hash);
+ try stdout.print(" small keys: {:4} MiB/s [{x:0<16}]\n", result_small.throughput / (1 * MiB), result_small.hash);
}
}
}