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 { const CryptoPwhash = struct {
hashFn: @compileError("anytype fields are removed from the language"), ty: type,
params: @compileError("anytype fields are removed from the language"), params: *const anyopaque,
name: []const u8, name: []const u8,
}; };
const bcrypt_params = crypto.pwhash.bcrypt.Params{ .rounds_log = 12 }; const bcrypt_params = crypto.pwhash.bcrypt.Params{ .rounds_log = 12 };
const pwhashes = [_]CryptoPwhash{ 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, .ty = crypto.pwhash.scrypt,
.params = crypto.pwhash.scrypt.Params.interactive, .params = &crypto.pwhash.scrypt.Params.interactive,
.name = "scrypt", .name = "scrypt",
}, },
.{ .{
.hashFn = crypto.pwhash.argon2.strHash, .ty = crypto.pwhash.argon2,
.params = crypto.pwhash.argon2.Params.interactive_2id, .params = &crypto.pwhash.argon2.Params.interactive_2id,
.name = "argon2", .name = "argon2",
}, },
}; };
fn benchmarkPwhash( fn benchmarkPwhash(
comptime hashFn: anytype, comptime ty: anytype,
comptime params: anytype, comptime params: *const anyopaque,
comptime count: comptime_int, comptime count: comptime_int,
) !f64 { ) !f64 {
const password = "testpass" ** 2; 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 buf: [256]u8 = undefined;
var timer = try Timer.start(); var timer = try Timer.start();
@ -330,7 +330,7 @@ fn benchmarkPwhash(
{ {
var i: usize = 0; var i: usize = 0;
while (i < count) : (i += 1) { while (i < count) : (i += 1) {
_ = try hashFn(password, opts, &buf); _ = try ty.strHash(password, opts, &buf);
mem.doNotOptimizeAway(&buf); mem.doNotOptimizeAway(&buf);
} }
} }
@ -463,8 +463,8 @@ pub fn main() !void {
inline for (pwhashes) |H| { inline for (pwhashes) |H| {
if (filter == null or std.mem.indexOf(u8, H.name, filter.?) != null) { if (filter == null or std.mem.indexOf(u8, H.name, filter.?) != null) {
const throughput = try benchmarkPwhash(H.hashFn, H.params, mode(64)); const throughput = try benchmarkPwhash(H.ty, H.params, mode(64));
try stdout.print("{s:>17}: {d:.3} s/ops\n", .{ H.name, throughput }); 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| { inline for (hashes) |H| {
if (filter == null or std.mem.indexOf(u8, H.name, filter.?) != null) { if (filter == null or std.mem.indexOf(u8, H.name, filter.?) != null) {
if (!test_iterative_only or H.has_iterative_api) { 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. // Always reseed prior to every call so we are hashing the same buffer contents.
// This allows easier comparison between different implementations. // This allows easier comparison between different implementations.