std: fix crypto and hash benchmark

This commit is contained in:
jagt 2022-04-23 10:12:06 +08:00 committed by Andrew Kelley
parent 963ac60918
commit 76311aebff
2 changed files with 14 additions and 14 deletions

View File

@ -297,32 +297,32 @@ pub fn benchmarkAes8(comptime Aes: anytype, comptime count: comptime_int) !u64 {
}
const CryptoPwhash = struct {
hashFn: @compileError("anytype fields are removed from the language"),
params: @compileError("anytype fields are removed from the language"),
ty: type,
params: *const anyopaque,
name: []const u8,
};
const bcrypt_params = crypto.pwhash.bcrypt.Params{ .rounds_log = 12 };
const pwhashes = [_]CryptoPwhash{
.{ .hashFn = crypto.pwhash.bcrypt.strHash, .params = bcrypt_params, .name = "bcrypt" },
.{ .ty = crypto.pwhash.bcrypt, .params = &bcrypt_params, .name = "bcrypt" },
.{
.hashFn = crypto.pwhash.scrypt.strHash,
.params = crypto.pwhash.scrypt.Params.interactive,
.ty = crypto.pwhash.scrypt,
.params = &crypto.pwhash.scrypt.Params.interactive,
.name = "scrypt",
},
.{
.hashFn = crypto.pwhash.argon2.strHash,
.params = crypto.pwhash.argon2.Params.interactive_2id,
.ty = crypto.pwhash.argon2,
.params = &crypto.pwhash.argon2.Params.interactive_2id,
.name = "argon2",
},
};
fn benchmarkPwhash(
comptime hashFn: anytype,
comptime params: anytype,
comptime ty: anytype,
comptime params: *const anyopaque,
comptime count: comptime_int,
) !f64 {
const password = "testpass" ** 2;
const opts = .{ .allocator = std.testing.allocator, .params = params, .encoding = .phc };
const opts = .{ .allocator = std.testing.allocator, .params = @ptrCast(*const ty.Params, params).*, .encoding = .phc };
var buf: [256]u8 = undefined;
var timer = try Timer.start();
@ -330,7 +330,7 @@ fn benchmarkPwhash(
{
var i: usize = 0;
while (i < count) : (i += 1) {
_ = try hashFn(password, opts, &buf);
_ = try ty.strHash(password, opts, &buf);
mem.doNotOptimizeAway(&buf);
}
}
@ -463,8 +463,8 @@ pub fn main() !void {
inline for (pwhashes) |H| {
if (filter == null or std.mem.indexOf(u8, H.name, filter.?) != null) {
const throughput = try benchmarkPwhash(H.hashFn, H.params, mode(64));
try stdout.print("{s:>17}: {d:.3} s/ops\n", .{ H.name, throughput });
const throughput = try benchmarkPwhash(H.ty, H.params, mode(64));
try stdout.print("{s:>17}: {d:10.3} s/ops\n", .{ H.name, throughput });
}
}
}

View File

@ -230,7 +230,7 @@ pub fn main() !void {
inline for (hashes) |H| {
if (filter == null or std.mem.indexOf(u8, H.name, filter.?) != null) {
if (!test_iterative_only or H.has_iterative_api) {
try stdout.print("{}\n", .{H.name});
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.