| author | |
| committer | |
| log | 485b996b6159babb865dbdc009428677ef01e50c |
| tree | ba5edd90f91f61fe17661c910045588f8d05fdb4 |
| parent | ce1f7136ae6db5809d077ccc52639befd73f50c0 |
In #31086, the `std.time.Timer` struct was removed, but this broke the last few programs that used it, those being the benchmarking programs for `std.Random`, `std.hash`, `std.crypto` and `std.unicode`. One more is `zig/perf_test.zig`, but as far as I can tell, that one is broken due to changes in file import rules too, unless I'm launching it wrong.
I also spotted some performance and benchmarking issues with the RNGs, detailed in #31554.
Reviewed-on: https://codeberg.org/ziglang/zig/pulls/31553
Reviewed-by: Andrew Kelley <andrew@ziglang.org>
Co-authored-by: UraniaZPM <uraniazpm@noreply.codeberg.org>
Co-committed-by: UraniaZPM <uraniazpm@noreply.codeberg.org>4 files changed, 95 insertions(+), 97 deletions(-)
lib/std/Random/benchmark.zig+11-9| ... | ... | @@ -5,7 +5,6 @@ const builtin = @import("builtin"); |
| 5 | 5 | const std = @import("std"); |
| 6 | 6 | const Io = std.Io; |
| 7 | 7 | const time = std.time; |
| 8 | const Timer = time.Timer; | |
| 9 | 8 | const Random = std.Random; |
| 10 | 9 | |
| 11 | 10 | const KiB = 1024; |
| ... | ... | @@ -72,7 +71,11 @@ const Result = struct { |
| 72 | 71 | const long_block_size: usize = 8 * 8192; |
| 73 | 72 | const short_block_size: usize = 8; |
| 74 | 73 | |
| 75 | pub fn benchmark(comptime H: anytype, bytes: usize, comptime block_size: usize) !Result { | |
| 74 | pub fn benchTime(io: Io) i96 { | |
| 75 | return Io.Clock.awake.now(io).nanoseconds; | |
| 76 | } | |
| 77 | ||
| 78 | pub fn benchmark(comptime H: anytype, io: Io, bytes: usize, comptime block_size: usize) !Result { | |
| 76 | 79 | var rng = blk: { |
| 77 | 80 | if (H.init_u8s) |init| { |
| 78 | 81 | break :blk H.ty.init(init[0..].*); |
| ... | ... | @@ -86,12 +89,11 @@ pub fn benchmark(comptime H: anytype, bytes: usize, comptime block_size: usize) |
| 86 | 89 | var block: [block_size]u8 = undefined; |
| 87 | 90 | |
| 88 | 91 | var offset: usize = 0; |
| 89 | var timer = try Timer.start(); | |
| 90 | const start = timer.lap(); | |
| 92 | const start = benchTime(io); | |
| 91 | 93 | while (offset < bytes) : (offset += block.len) { |
| 92 | 94 | rng.fill(block[0..]); |
| 93 | 95 | } |
| 94 | const end = timer.read(); | |
| 96 | const end = benchTime(io); | |
| 95 | 97 | |
| 96 | 98 | const elapsed_s = @as(f64, @floatFromInt(end - start)) / time.ns_per_s; |
| 97 | 99 | const throughput = @as(u64, @intFromFloat(@as(f64, @floatFromInt(bytes)) / elapsed_s)); |
| ... | ... | @@ -187,7 +189,7 @@ pub fn main(init: std.process.Init) !void { |
| 187 | 189 | try stdout.print("{s} (long outputs)\n", .{R.name}); |
| 188 | 190 | try stdout.flush(); |
| 189 | 191 | |
| 190 | const result_long = try benchmark(R, count, long_block_size); | |
| 192 | const result_long = try benchmark(R, io, count, long_block_size); | |
| 191 | 193 | try stdout.print(" {:5} MiB/s\n", .{result_long.throughput / (1 * MiB)}); |
| 192 | 194 | } |
| 193 | 195 | } |
| ... | ... | @@ -198,7 +200,7 @@ pub fn main(init: std.process.Init) !void { |
| 198 | 200 | try stdout.print("{s} (short outputs)\n", .{R.name}); |
| 199 | 201 | try stdout.flush(); |
| 200 | 202 | |
| 201 | const result_short = try benchmark(R, count, short_block_size); | |
| 203 | const result_short = try benchmark(R, io, count, short_block_size); | |
| 202 | 204 | try stdout.print(" {:5} MiB/s\n", .{result_short.throughput / (1 * MiB)}); |
| 203 | 205 | } |
| 204 | 206 | } |
| ... | ... | @@ -211,7 +213,7 @@ pub fn main(init: std.process.Init) !void { |
| 211 | 213 | try stdout.print("{s} (cryptographic, long outputs)\n", .{R.name}); |
| 212 | 214 | try stdout.flush(); |
| 213 | 215 | |
| 214 | const result_long = try benchmark(R, count, long_block_size); | |
| 216 | const result_long = try benchmark(R, io, count, long_block_size); | |
| 215 | 217 | try stdout.print(" {:5} MiB/s\n", .{result_long.throughput / (1 * MiB)}); |
| 216 | 218 | } |
| 217 | 219 | } |
| ... | ... | @@ -222,7 +224,7 @@ pub fn main(init: std.process.Init) !void { |
| 222 | 224 | try stdout.print("{s} (cryptographic, short outputs)\n", .{R.name}); |
| 223 | 225 | try stdout.flush(); |
| 224 | 226 | |
| 225 | const result_short = try benchmark(R, count, short_block_size); | |
| 227 | const result_short = try benchmark(R, io, count, short_block_size); | |
| 226 | 228 | try stdout.print(" {:5} MiB/s\n", .{result_short.throughput / (1 * MiB)}); |
| 227 | 229 | } |
| 228 | 230 | } |
lib/std/crypto/benchmark.zig+44-55| ... | ... | @@ -6,7 +6,6 @@ const std = @import("std"); |
| 6 | 6 | const Io = std.Io; |
| 7 | 7 | const mem = std.mem; |
| 8 | 8 | const time = std.time; |
| 9 | const Timer = std.time.Timer; | |
| 10 | 9 | const crypto = std.crypto; |
| 11 | 10 | |
| 12 | 11 | const KiB = 1024; |
| ... | ... | @@ -45,15 +44,18 @@ const parallel_hashes = [_]Crypto{ |
| 45 | 44 | |
| 46 | 45 | const block_size: usize = 8 * 8192; |
| 47 | 46 | |
| 48 | pub fn benchmarkHash(comptime Hash: anytype, comptime bytes: comptime_int) !u64 { | |
| 47 | pub fn benchTime(io: Io) i96 { | |
| 48 | return Io.Clock.awake.now(io).nanoseconds; | |
| 49 | } | |
| 50 | ||
| 51 | pub fn benchmarkHash(comptime Hash: anytype, comptime bytes: comptime_int, io: Io) !u64 { | |
| 49 | 52 | const blocks_count = bytes / block_size; |
| 50 | 53 | var block: [block_size]u8 = undefined; |
| 51 | 54 | random.bytes(&block); |
| 52 | 55 | |
| 53 | 56 | var h = Hash.init(.{}); |
| 54 | 57 | |
| 55 | var timer = try Timer.start(); | |
| 56 | const start = timer.lap(); | |
| 58 | const start = benchTime(io); | |
| 57 | 59 | for (0..blocks_count) |_| { |
| 58 | 60 | h.update(&block); |
| 59 | 61 | } |
| ... | ... | @@ -61,7 +63,7 @@ pub fn benchmarkHash(comptime Hash: anytype, comptime bytes: comptime_int) !u64 |
| 61 | 63 | h.final(&final); |
| 62 | 64 | std.mem.doNotOptimizeAway(final); |
| 63 | 65 | |
| 64 | const end = timer.read(); | |
| 66 | const end = benchTime(io); | |
| 65 | 67 | |
| 66 | 68 | const elapsed_s = @as(f64, @floatFromInt(end - start)) / time.ns_per_s; |
| 67 | 69 | const throughput = @as(u64, @intFromFloat(bytes / elapsed_s)); |
| ... | ... | @@ -74,13 +76,12 @@ pub fn benchmarkHashParallel(comptime Hash: anytype, comptime bytes: comptime_in |
| 74 | 76 | defer allocator.free(data); |
| 75 | 77 | random.bytes(data); |
| 76 | 78 | |
| 77 | var timer = try Timer.start(); | |
| 78 | const start = timer.lap(); | |
| 79 | const start = benchTime(io); | |
| 79 | 80 | var final: [Hash.digest_length]u8 = undefined; |
| 80 | 81 | try Hash.hashParallel(data, &final, .{}, allocator, io); |
| 81 | 82 | std.mem.doNotOptimizeAway(final); |
| 82 | 83 | |
| 83 | const end = timer.read(); | |
| 84 | const end = benchTime(io); | |
| 84 | 85 | |
| 85 | 86 | const elapsed_s = @as(f64, @floatFromInt(end - start)) / time.ns_per_s; |
| 86 | 87 | const throughput = @as(u64, @intFromFloat(bytes / elapsed_s)); |
| ... | ... | @@ -109,7 +110,7 @@ const macs = [_]Crypto{ |
| 109 | 110 | Crypto{ .ty = crypto.auth.cmac.CmacAes128, .name = "aes-cmac" }, |
| 110 | 111 | }; |
| 111 | 112 | |
| 112 | pub fn benchmarkMac(comptime Mac: anytype, comptime bytes: comptime_int) !u64 { | |
| 113 | pub fn benchmarkMac(comptime Mac: anytype, comptime bytes: comptime_int, io: Io) !u64 { | |
| 113 | 114 | var in: [512 * KiB]u8 = undefined; |
| 114 | 115 | random.bytes(in[0..]); |
| 115 | 116 | |
| ... | ... | @@ -119,13 +120,12 @@ pub fn benchmarkMac(comptime Mac: anytype, comptime bytes: comptime_int) !u64 { |
| 119 | 120 | |
| 120 | 121 | var mac: [Mac.mac_length]u8 = undefined; |
| 121 | 122 | var offset: usize = 0; |
| 122 | var timer = try Timer.start(); | |
| 123 | const start = timer.lap(); | |
| 123 | const start = benchTime(io); | |
| 124 | 124 | while (offset < bytes) : (offset += in.len) { |
| 125 | 125 | Mac.create(mac[0..], in[0..], key[0..]); |
| 126 | 126 | mem.doNotOptimizeAway(&mac); |
| 127 | 127 | } |
| 128 | const end = timer.read(); | |
| 128 | const end = benchTime(io); | |
| 129 | 129 | |
| 130 | 130 | const elapsed_s = @as(f64, @floatFromInt(end - start)) / time.ns_per_s; |
| 131 | 131 | const throughput = @as(u64, @intFromFloat(bytes / elapsed_s)); |
| ... | ... | @@ -135,7 +135,7 @@ pub fn benchmarkMac(comptime Mac: anytype, comptime bytes: comptime_int) !u64 { |
| 135 | 135 | |
| 136 | 136 | const exchanges = [_]Crypto{Crypto{ .ty = crypto.dh.X25519, .name = "x25519" }}; |
| 137 | 137 | |
| 138 | pub fn benchmarkKeyExchange(comptime DhKeyExchange: anytype, comptime exchange_count: comptime_int) !u64 { | |
| 138 | pub fn benchmarkKeyExchange(comptime DhKeyExchange: anytype, comptime exchange_count: comptime_int, io: Io) !u64 { | |
| 139 | 139 | std.debug.assert(DhKeyExchange.shared_length >= DhKeyExchange.secret_length); |
| 140 | 140 | |
| 141 | 141 | var secret: [DhKeyExchange.shared_length]u8 = undefined; |
| ... | ... | @@ -144,8 +144,7 @@ pub fn benchmarkKeyExchange(comptime DhKeyExchange: anytype, comptime exchange_c |
| 144 | 144 | var public: [DhKeyExchange.shared_length]u8 = undefined; |
| 145 | 145 | random.bytes(public[0..]); |
| 146 | 146 | |
| 147 | var timer = try Timer.start(); | |
| 148 | const start = timer.lap(); | |
| 147 | const start = benchTime(io); | |
| 149 | 148 | { |
| 150 | 149 | var i: usize = 0; |
| 151 | 150 | while (i < exchange_count) : (i += 1) { |
| ... | ... | @@ -155,7 +154,7 @@ pub fn benchmarkKeyExchange(comptime DhKeyExchange: anytype, comptime exchange_c |
| 155 | 154 | mem.doNotOptimizeAway(&out); |
| 156 | 155 | } |
| 157 | 156 | } |
| 158 | const end = timer.read(); | |
| 157 | const end = benchTime(io); | |
| 159 | 158 | |
| 160 | 159 | const elapsed_s = @as(f64, @floatFromInt(end - start)) / time.ns_per_s; |
| 161 | 160 | const throughput = @as(u64, @intFromFloat(exchange_count / elapsed_s)); |
| ... | ... | @@ -177,8 +176,7 @@ pub fn benchmarkSignature(comptime Signature: anytype, comptime signatures_count |
| 177 | 176 | const msg = [_]u8{0} ** 64; |
| 178 | 177 | const key_pair = Signature.KeyPair.generate(io); |
| 179 | 178 | |
| 180 | var timer = try Timer.start(); | |
| 181 | const start = timer.lap(); | |
| 179 | const start = benchTime(io); | |
| 182 | 180 | { |
| 183 | 181 | var i: usize = 0; |
| 184 | 182 | while (i < signatures_count) : (i += 1) { |
| ... | ... | @@ -186,7 +184,7 @@ pub fn benchmarkSignature(comptime Signature: anytype, comptime signatures_count |
| 186 | 184 | mem.doNotOptimizeAway(&sig); |
| 187 | 185 | } |
| 188 | 186 | } |
| 189 | const end = timer.read(); | |
| 187 | const end = benchTime(io); | |
| 190 | 188 | |
| 191 | 189 | const elapsed_s = @as(f64, @floatFromInt(end - start)) / time.ns_per_s; |
| 192 | 190 | const throughput = @as(u64, @intFromFloat(signatures_count / elapsed_s)); |
| ... | ... | @@ -206,8 +204,7 @@ pub fn benchmarkSignatureVerification(comptime Signature: anytype, comptime sign |
| 206 | 204 | const key_pair = Signature.KeyPair.generate(io); |
| 207 | 205 | const sig = try key_pair.sign(&msg, null); |
| 208 | 206 | |
| 209 | var timer = try Timer.start(); | |
| 210 | const start = timer.lap(); | |
| 207 | const start = benchTime(io); | |
| 211 | 208 | { |
| 212 | 209 | var i: usize = 0; |
| 213 | 210 | while (i < signatures_count) : (i += 1) { |
| ... | ... | @@ -215,7 +212,7 @@ pub fn benchmarkSignatureVerification(comptime Signature: anytype, comptime sign |
| 215 | 212 | mem.doNotOptimizeAway(&sig); |
| 216 | 213 | } |
| 217 | 214 | } |
| 218 | const end = timer.read(); | |
| 215 | const end = benchTime(io); | |
| 219 | 216 | |
| 220 | 217 | const elapsed_s = @as(f64, @floatFromInt(end - start)) / time.ns_per_s; |
| 221 | 218 | const throughput = @as(u64, @intFromFloat(signatures_count / elapsed_s)); |
| ... | ... | @@ -235,8 +232,7 @@ pub fn benchmarkBatchSignatureVerification(comptime Signature: anytype, comptime |
| 235 | 232 | element.* = Signature.BatchElement{ .sig = sig, .msg = &msg, .public_key = key_pair.public_key }; |
| 236 | 233 | } |
| 237 | 234 | |
| 238 | var timer = try Timer.start(); | |
| 239 | const start = timer.lap(); | |
| 235 | const start = benchTime(io); | |
| 240 | 236 | { |
| 241 | 237 | var i: usize = 0; |
| 242 | 238 | while (i < signatures_count) : (i += 1) { |
| ... | ... | @@ -244,7 +240,7 @@ pub fn benchmarkBatchSignatureVerification(comptime Signature: anytype, comptime |
| 244 | 240 | mem.doNotOptimizeAway(&sig); |
| 245 | 241 | } |
| 246 | 242 | } |
| 247 | const end = timer.read(); | |
| 243 | const end = benchTime(io); | |
| 248 | 244 | |
| 249 | 245 | const elapsed_s = @as(f64, @floatFromInt(end - start)) / time.ns_per_s; |
| 250 | 246 | const throughput = batch.len * @as(u64, @intFromFloat(signatures_count / elapsed_s)); |
| ... | ... | @@ -261,8 +257,7 @@ const kems = [_]Crypto{ |
| 261 | 257 | pub fn benchmarkKem(comptime Kem: anytype, comptime kems_count: comptime_int, io: std.Io) !u64 { |
| 262 | 258 | const key_pair = Kem.KeyPair.generate(io); |
| 263 | 259 | |
| 264 | var timer = try Timer.start(); | |
| 265 | const start = timer.lap(); | |
| 260 | const start = benchTime(io); | |
| 266 | 261 | { |
| 267 | 262 | var i: usize = 0; |
| 268 | 263 | while (i < kems_count) : (i += 1) { |
| ... | ... | @@ -270,7 +265,7 @@ pub fn benchmarkKem(comptime Kem: anytype, comptime kems_count: comptime_int, io |
| 270 | 265 | mem.doNotOptimizeAway(&e); |
| 271 | 266 | } |
| 272 | 267 | } |
| 273 | const end = timer.read(); | |
| 268 | const end = benchTime(io); | |
| 274 | 269 | |
| 275 | 270 | const elapsed_s = @as(f64, @floatFromInt(end - start)) / time.ns_per_s; |
| 276 | 271 | const throughput = @as(u64, @intFromFloat(kems_count / elapsed_s)); |
| ... | ... | @@ -283,8 +278,7 @@ pub fn benchmarkKemDecaps(comptime Kem: anytype, comptime kems_count: comptime_i |
| 283 | 278 | |
| 284 | 279 | const e = key_pair.public_key.encaps(io); |
| 285 | 280 | |
| 286 | var timer = try Timer.start(); | |
| 287 | const start = timer.lap(); | |
| 281 | const start = benchTime(io); | |
| 288 | 282 | { |
| 289 | 283 | var i: usize = 0; |
| 290 | 284 | while (i < kems_count) : (i += 1) { |
| ... | ... | @@ -292,7 +286,7 @@ pub fn benchmarkKemDecaps(comptime Kem: anytype, comptime kems_count: comptime_i |
| 292 | 286 | mem.doNotOptimizeAway(&ss2); |
| 293 | 287 | } |
| 294 | 288 | } |
| 295 | const end = timer.read(); | |
| 289 | const end = benchTime(io); | |
| 296 | 290 | |
| 297 | 291 | const elapsed_s = @as(f64, @floatFromInt(end - start)) / time.ns_per_s; |
| 298 | 292 | const throughput = @as(u64, @intFromFloat(kems_count / elapsed_s)); |
| ... | ... | @@ -301,8 +295,7 @@ pub fn benchmarkKemDecaps(comptime Kem: anytype, comptime kems_count: comptime_i |
| 301 | 295 | } |
| 302 | 296 | |
| 303 | 297 | pub fn benchmarkKemKeyGen(comptime Kem: anytype, comptime kems_count: comptime_int, io: std.Io) !u64 { |
| 304 | var timer = try Timer.start(); | |
| 305 | const start = timer.lap(); | |
| 298 | const start = benchTime(io); | |
| 306 | 299 | { |
| 307 | 300 | var i: usize = 0; |
| 308 | 301 | while (i < kems_count) : (i += 1) { |
| ... | ... | @@ -310,7 +303,7 @@ pub fn benchmarkKemKeyGen(comptime Kem: anytype, comptime kems_count: comptime_i |
| 310 | 303 | mem.doNotOptimizeAway(&key_pair); |
| 311 | 304 | } |
| 312 | 305 | } |
| 313 | const end = timer.read(); | |
| 306 | const end = benchTime(io); | |
| 314 | 307 | |
| 315 | 308 | const elapsed_s = @as(f64, @floatFromInt(end - start)) / time.ns_per_s; |
| 316 | 309 | const throughput = @as(u64, @intFromFloat(kems_count / elapsed_s)); |
| ... | ... | @@ -337,7 +330,7 @@ const aeads = [_]Crypto{ |
| 337 | 330 | Crypto{ .ty = crypto.aead.isap.IsapA128A, .name = "isapa128a" }, |
| 338 | 331 | }; |
| 339 | 332 | |
| 340 | pub fn benchmarkAead(comptime Aead: anytype, comptime bytes: comptime_int) !u64 { | |
| 333 | pub fn benchmarkAead(comptime Aead: anytype, comptime bytes: comptime_int, io: Io) !u64 { | |
| 341 | 334 | var in: [512 * KiB]u8 = undefined; |
| 342 | 335 | random.bytes(in[0..]); |
| 343 | 336 | |
| ... | ... | @@ -350,14 +343,13 @@ pub fn benchmarkAead(comptime Aead: anytype, comptime bytes: comptime_int) !u64 |
| 350 | 343 | random.bytes(nonce[0..]); |
| 351 | 344 | |
| 352 | 345 | var offset: usize = 0; |
| 353 | var timer = try Timer.start(); | |
| 354 | const start = timer.lap(); | |
| 346 | const start = benchTime(io); | |
| 355 | 347 | while (offset < bytes) : (offset += in.len) { |
| 356 | 348 | Aead.encrypt(in[0..], tag[0..], in[0..], &[_]u8{}, nonce, key); |
| 357 | 349 | try Aead.decrypt(in[0..], in[0..], tag, &[_]u8{}, nonce, key); |
| 358 | 350 | } |
| 359 | 351 | mem.doNotOptimizeAway(&in); |
| 360 | const end = timer.read(); | |
| 352 | const end = benchTime(io); | |
| 361 | 353 | |
| 362 | 354 | const elapsed_s = @as(f64, @floatFromInt(end - start)) / time.ns_per_s; |
| 363 | 355 | const throughput = @as(u64, @intFromFloat(2 * bytes / elapsed_s)); |
| ... | ... | @@ -370,15 +362,14 @@ const aes = [_]Crypto{ |
| 370 | 362 | Crypto{ .ty = crypto.core.aes.Aes256, .name = "aes256-single" }, |
| 371 | 363 | }; |
| 372 | 364 | |
| 373 | pub fn benchmarkAes(comptime Aes: anytype, comptime count: comptime_int) !u64 { | |
| 365 | pub fn benchmarkAes(comptime Aes: anytype, comptime count: comptime_int, io: Io) !u64 { | |
| 374 | 366 | var key: [Aes.key_bits / 8]u8 = undefined; |
| 375 | 367 | random.bytes(key[0..]); |
| 376 | 368 | const ctx = Aes.initEnc(key); |
| 377 | 369 | |
| 378 | 370 | var in = [_]u8{0} ** 16; |
| 379 | 371 | |
| 380 | var timer = try Timer.start(); | |
| 381 | const start = timer.lap(); | |
| 372 | const start = benchTime(io); | |
| 382 | 373 | { |
| 383 | 374 | var i: usize = 0; |
| 384 | 375 | while (i < count) : (i += 1) { |
| ... | ... | @@ -386,7 +377,7 @@ pub fn benchmarkAes(comptime Aes: anytype, comptime count: comptime_int) !u64 { |
| 386 | 377 | } |
| 387 | 378 | } |
| 388 | 379 | mem.doNotOptimizeAway(&in); |
| 389 | const end = timer.read(); | |
| 380 | const end = benchTime(io); | |
| 390 | 381 | |
| 391 | 382 | const elapsed_s = @as(f64, @floatFromInt(end - start)) / time.ns_per_s; |
| 392 | 383 | const throughput = @as(u64, @intFromFloat(count / elapsed_s)); |
| ... | ... | @@ -399,15 +390,14 @@ const aes8 = [_]Crypto{ |
| 399 | 390 | Crypto{ .ty = crypto.core.aes.Aes256, .name = "aes256-8" }, |
| 400 | 391 | }; |
| 401 | 392 | |
| 402 | pub fn benchmarkAes8(comptime Aes: anytype, comptime count: comptime_int) !u64 { | |
| 393 | pub fn benchmarkAes8(comptime Aes: anytype, comptime count: comptime_int, io: Io) !u64 { | |
| 403 | 394 | var key: [Aes.key_bits / 8]u8 = undefined; |
| 404 | 395 | random.bytes(key[0..]); |
| 405 | 396 | const ctx = Aes.initEnc(key); |
| 406 | 397 | |
| 407 | 398 | var in = [_]u8{0} ** (8 * 16); |
| 408 | 399 | |
| 409 | var timer = try Timer.start(); | |
| 410 | const start = timer.lap(); | |
| 400 | const start = benchTime(io); | |
| 411 | 401 | { |
| 412 | 402 | var i: usize = 0; |
| 413 | 403 | while (i < count) : (i += 1) { |
| ... | ... | @@ -415,7 +405,7 @@ pub fn benchmarkAes8(comptime Aes: anytype, comptime count: comptime_int) !u64 { |
| 415 | 405 | } |
| 416 | 406 | } |
| 417 | 407 | mem.doNotOptimizeAway(&in); |
| 418 | const end = timer.read(); | |
| 408 | const end = benchTime(io); | |
| 419 | 409 | |
| 420 | 410 | const elapsed_s = @as(f64, @floatFromInt(end - start)) / time.ns_per_s; |
| 421 | 411 | const throughput = @as(u64, @intFromFloat(8 * count / elapsed_s)); |
| ... | ... | @@ -468,8 +458,7 @@ fn benchmarkPwhash( |
| 468 | 458 | const needs_salt = strHashFnInfo.params.len == 4 and strHashFnInfo.params[3].type != std.Io; |
| 469 | 459 | const salt: [16]u8 = .{0} ** 16; |
| 470 | 460 | |
| 471 | var timer = try Timer.start(); | |
| 472 | const start = timer.lap(); | |
| 461 | const start = benchTime(io); | |
| 473 | 462 | { |
| 474 | 463 | var i: usize = 0; |
| 475 | 464 | while (i < count) : (i += 1) { |
| ... | ... | @@ -483,7 +472,7 @@ fn benchmarkPwhash( |
| 483 | 472 | mem.doNotOptimizeAway(&buf); |
| 484 | 473 | } |
| 485 | 474 | } |
| 486 | const end = timer.read(); | |
| 475 | const end = benchTime(io); | |
| 487 | 476 | |
| 488 | 477 | const elapsed_s = @as(f64, @floatFromInt(end - start)) / time.ns_per_s; |
| 489 | 478 | const throughput = elapsed_s / count; |
| ... | ... | @@ -554,7 +543,7 @@ pub fn main(init: std.process.Init) !void { |
| 554 | 543 | |
| 555 | 544 | inline for (hashes) |H| { |
| 556 | 545 | if (filter == null or std.mem.find(u8, H.name, filter.?) != null) { |
| 557 | const throughput = try benchmarkHash(H.ty, mode(128 * MiB)); | |
| 546 | const throughput = try benchmarkHash(H.ty, mode(128 * MiB), io); | |
| 558 | 547 | try stdout.print("{s:>17}: {:10} MiB/s\n", .{ H.name, throughput / (1 * MiB) }); |
| 559 | 548 | try stdout.flush(); |
| 560 | 549 | } |
| ... | ... | @@ -570,7 +559,7 @@ pub fn main(init: std.process.Init) !void { |
| 570 | 559 | |
| 571 | 560 | inline for (macs) |M| { |
| 572 | 561 | if (filter == null or std.mem.find(u8, M.name, filter.?) != null) { |
| 573 | const throughput = try benchmarkMac(M.ty, mode(128 * MiB)); | |
| 562 | const throughput = try benchmarkMac(M.ty, mode(128 * MiB), io); | |
| 574 | 563 | try stdout.print("{s:>17}: {:10} MiB/s\n", .{ M.name, throughput / (1 * MiB) }); |
| 575 | 564 | try stdout.flush(); |
| 576 | 565 | } |
| ... | ... | @@ -578,7 +567,7 @@ pub fn main(init: std.process.Init) !void { |
| 578 | 567 | |
| 579 | 568 | inline for (exchanges) |E| { |
| 580 | 569 | if (filter == null or std.mem.find(u8, E.name, filter.?) != null) { |
| 581 | const throughput = try benchmarkKeyExchange(E.ty, mode(1000)); | |
| 570 | const throughput = try benchmarkKeyExchange(E.ty, mode(1000), io); | |
| 582 | 571 | try stdout.print("{s:>17}: {:10} exchanges/s\n", .{ E.name, throughput }); |
| 583 | 572 | try stdout.flush(); |
| 584 | 573 | } |
| ... | ... | @@ -610,7 +599,7 @@ pub fn main(init: std.process.Init) !void { |
| 610 | 599 | |
| 611 | 600 | inline for (aeads) |E| { |
| 612 | 601 | if (filter == null or std.mem.find(u8, E.name, filter.?) != null) { |
| 613 | const throughput = try benchmarkAead(E.ty, mode(128 * MiB)); | |
| 602 | const throughput = try benchmarkAead(E.ty, mode(128 * MiB), io); | |
| 614 | 603 | try stdout.print("{s:>17}: {:10} MiB/s\n", .{ E.name, throughput / (1 * MiB) }); |
| 615 | 604 | try stdout.flush(); |
| 616 | 605 | } |
| ... | ... | @@ -618,7 +607,7 @@ pub fn main(init: std.process.Init) !void { |
| 618 | 607 | |
| 619 | 608 | inline for (aes) |E| { |
| 620 | 609 | if (filter == null or std.mem.find(u8, E.name, filter.?) != null) { |
| 621 | const throughput = try benchmarkAes(E.ty, mode(100000000)); | |
| 610 | const throughput = try benchmarkAes(E.ty, mode(100000000), io); | |
| 622 | 611 | try stdout.print("{s:>17}: {:10} ops/s\n", .{ E.name, throughput }); |
| 623 | 612 | try stdout.flush(); |
| 624 | 613 | } |
| ... | ... | @@ -626,7 +615,7 @@ pub fn main(init: std.process.Init) !void { |
| 626 | 615 | |
| 627 | 616 | inline for (aes8) |E| { |
| 628 | 617 | if (filter == null or std.mem.find(u8, E.name, filter.?) != null) { |
| 629 | const throughput = try benchmarkAes8(E.ty, mode(10000000)); | |
| 618 | const throughput = try benchmarkAes8(E.ty, mode(10000000), io); | |
| 630 | 619 | try stdout.print("{s:>17}: {:10} ops/s\n", .{ E.name, throughput }); |
| 631 | 620 | try stdout.flush(); |
| 632 | 621 | } |
lib/std/hash/benchmark.zig+26-21| ... | ... | @@ -4,7 +4,6 @@ const builtin = @import("builtin"); |
| 4 | 4 | const std = @import("std"); |
| 5 | 5 | const Io = std.Io; |
| 6 | 6 | const time = std.time; |
| 7 | const Timer = time.Timer; | |
| 8 | 7 | const hash = std.hash; |
| 9 | 8 | |
| 10 | 9 | const KiB = 1024; |
| ... | ... | @@ -111,7 +110,11 @@ const Result = struct { |
| 111 | 110 | |
| 112 | 111 | const block_size: usize = 8 * 8192; |
| 113 | 112 | |
| 114 | pub fn benchmarkHash(comptime H: anytype, bytes: usize, allocator: std.mem.Allocator) !Result { | |
| 113 | pub fn benchTime(io: Io) i96 { | |
| 114 | return Io.Clock.awake.now(io).nanoseconds; | |
| 115 | } | |
| 116 | ||
| 117 | pub fn benchmarkHash(comptime H: anytype, bytes: usize, allocator: std.mem.Allocator, io: Io) !Result { | |
| 115 | 118 | var blocks = try allocator.alloc(u8, bytes); |
| 116 | 119 | defer allocator.free(blocks); |
| 117 | 120 | random.bytes(blocks); |
| ... | ... | @@ -131,7 +134,7 @@ pub fn benchmarkHash(comptime H: anytype, bytes: usize, allocator: std.mem.Alloc |
| 131 | 134 | break :blk .init(); |
| 132 | 135 | }; |
| 133 | 136 | |
| 134 | var timer = try Timer.start(); | |
| 137 | const start = benchTime(io); | |
| 135 | 138 | for (0..block_count) |i| { |
| 136 | 139 | h.update(blocks[i * block_size ..][0..block_size]); |
| 137 | 140 | } |
| ... | ... | @@ -143,7 +146,7 @@ pub fn benchmarkHash(comptime H: anytype, bytes: usize, allocator: std.mem.Alloc |
| 143 | 146 | h.final(); |
| 144 | 147 | std.mem.doNotOptimizeAway(final); |
| 145 | 148 | |
| 146 | const elapsed_ns = timer.read(); | |
| 149 | const elapsed_ns = benchTime(io) - start; | |
| 147 | 150 | |
| 148 | 151 | const elapsed_s = @as(f64, @floatFromInt(elapsed_ns)) / time.ns_per_s; |
| 149 | 152 | const size_float: f64 = @floatFromInt(block_size * block_count); |
| ... | ... | @@ -155,14 +158,14 @@ pub fn benchmarkHash(comptime H: anytype, bytes: usize, allocator: std.mem.Alloc |
| 155 | 158 | }; |
| 156 | 159 | } |
| 157 | 160 | |
| 158 | pub fn benchmarkHashSmallKeys(comptime H: anytype, key_size: usize, bytes: usize, allocator: std.mem.Allocator) !Result { | |
| 161 | pub fn benchmarkHashSmallKeys(comptime H: anytype, key_size: usize, bytes: usize, allocator: std.mem.Allocator, io: Io) !Result { | |
| 159 | 162 | var blocks = try allocator.alloc(u8, bytes); |
| 160 | 163 | defer allocator.free(blocks); |
| 161 | 164 | random.bytes(blocks); |
| 162 | 165 | |
| 163 | 166 | const key_count = bytes / key_size; |
| 164 | 167 | |
| 165 | var timer = try Timer.start(); | |
| 168 | const start = benchTime(io); | |
| 166 | 169 | |
| 167 | 170 | var sum: u64 = 0; |
| 168 | 171 | for (0..key_count) |i| { |
| ... | ... | @@ -182,7 +185,7 @@ pub fn benchmarkHashSmallKeys(comptime H: anytype, key_size: usize, bytes: usize |
| 182 | 185 | }; |
| 183 | 186 | sum +%= final; |
| 184 | 187 | } |
| 185 | const elapsed_ns = timer.read(); | |
| 188 | const elapsed_ns = benchTime(io) - start; | |
| 186 | 189 | |
| 187 | 190 | const elapsed_s = @as(f64, @floatFromInt(elapsed_ns)) / time.ns_per_s; |
| 188 | 191 | const size_float: f64 = @floatFromInt(key_count * key_size); |
| ... | ... | @@ -204,6 +207,7 @@ pub fn benchmarkHashSmallKeysArrayPtr( |
| 204 | 207 | comptime key_size: usize, |
| 205 | 208 | bytes: usize, |
| 206 | 209 | allocator: std.mem.Allocator, |
| 210 | io: Io, | |
| 207 | 211 | ) !Result { |
| 208 | 212 | var blocks = try allocator.alloc(u8, bytes); |
| 209 | 213 | defer allocator.free(blocks); |
| ... | ... | @@ -211,7 +215,7 @@ pub fn benchmarkHashSmallKeysArrayPtr( |
| 211 | 215 | |
| 212 | 216 | const key_count = bytes / key_size; |
| 213 | 217 | |
| 214 | var timer = try Timer.start(); | |
| 218 | const start = benchTime(io); | |
| 215 | 219 | |
| 216 | 220 | var sum: u64 = 0; |
| 217 | 221 | for (0..key_count) |i| { |
| ... | ... | @@ -231,7 +235,7 @@ pub fn benchmarkHashSmallKeysArrayPtr( |
| 231 | 235 | }; |
| 232 | 236 | sum +%= final; |
| 233 | 237 | } |
| 234 | const elapsed_ns = timer.read(); | |
| 238 | const elapsed_ns = benchTime(io) - start; | |
| 235 | 239 | |
| 236 | 240 | const elapsed_s = @as(f64, @floatFromInt(elapsed_ns)) / time.ns_per_s; |
| 237 | 241 | const throughput: u64 = @intFromFloat(@as(f64, @floatFromInt(bytes)) / elapsed_s); |
| ... | ... | @@ -252,6 +256,7 @@ pub fn benchmarkHashSmallKeysArray( |
| 252 | 256 | comptime key_size: usize, |
| 253 | 257 | bytes: usize, |
| 254 | 258 | allocator: std.mem.Allocator, |
| 259 | io: Io, | |
| 255 | 260 | ) !Result { |
| 256 | 261 | var blocks = try allocator.alloc(u8, bytes); |
| 257 | 262 | defer allocator.free(blocks); |
| ... | ... | @@ -260,7 +265,7 @@ pub fn benchmarkHashSmallKeysArray( |
| 260 | 265 | const key_count = bytes / key_size; |
| 261 | 266 | |
| 262 | 267 | var i: usize = 0; |
| 263 | var timer = try Timer.start(); | |
| 268 | const start = benchTime(io); | |
| 264 | 269 | |
| 265 | 270 | var sum: u64 = 0; |
| 266 | 271 | while (i < key_count) : (i += 1) { |
| ... | ... | @@ -280,7 +285,7 @@ pub fn benchmarkHashSmallKeysArray( |
| 280 | 285 | }; |
| 281 | 286 | sum +%= final; |
| 282 | 287 | } |
| 283 | const elapsed_ns = timer.read(); | |
| 288 | const elapsed_ns = benchTime(io) - start; | |
| 284 | 289 | |
| 285 | 290 | const elapsed_s = @as(f64, @floatFromInt(elapsed_ns)) / time.ns_per_s; |
| 286 | 291 | const throughput: u64 = @intFromFloat(@as(f64, @floatFromInt(bytes)) / elapsed_s); |
| ... | ... | @@ -293,14 +298,14 @@ pub fn benchmarkHashSmallKeysArray( |
| 293 | 298 | }; |
| 294 | 299 | } |
| 295 | 300 | |
| 296 | pub fn benchmarkHashSmallApi(comptime H: anytype, key_size: usize, bytes: usize, allocator: std.mem.Allocator) !Result { | |
| 301 | pub fn benchmarkHashSmallApi(comptime H: anytype, key_size: usize, bytes: usize, allocator: std.mem.Allocator, io: Io) !Result { | |
| 297 | 302 | var blocks = try allocator.alloc(u8, bytes); |
| 298 | 303 | defer allocator.free(blocks); |
| 299 | 304 | random.bytes(blocks); |
| 300 | 305 | |
| 301 | 306 | const key_count = bytes / key_size; |
| 302 | 307 | |
| 303 | var timer = try Timer.start(); | |
| 308 | const start = benchTime(io); | |
| 304 | 309 | |
| 305 | 310 | var sum: u64 = 0; |
| 306 | 311 | for (0..key_count) |i| { |
| ... | ... | @@ -320,7 +325,7 @@ pub fn benchmarkHashSmallApi(comptime H: anytype, key_size: usize, bytes: usize, |
| 320 | 325 | }; |
| 321 | 326 | sum +%= final; |
| 322 | 327 | } |
| 323 | const elapsed_ns = timer.read(); | |
| 328 | const elapsed_ns = benchTime(io) - start; | |
| 324 | 329 | |
| 325 | 330 | const elapsed_s = @as(f64, @floatFromInt(elapsed_ns)) / time.ns_per_s; |
| 326 | 331 | const throughput: u64 = @intFromFloat(@as(f64, @floatFromInt(bytes)) / elapsed_s); |
| ... | ... | @@ -454,7 +459,7 @@ pub fn main(init: std.process.Init) !void { |
| 454 | 459 | // This allows easier comparison between different implementations. |
| 455 | 460 | if (H.has_iterative_api and !test_small_key_only) { |
| 456 | 461 | prng.seed(seed); |
| 457 | const result = try benchmarkHash(H, count, allocator); | |
| 462 | const result = try benchmarkHash(H, count, allocator, io); | |
| 458 | 463 | try stdout.print(" iterative: {:5} MiB/s [{x:0<16}]\n", .{ result.throughput / (1 * MiB), result.hash }); |
| 459 | 464 | try stdout.flush(); |
| 460 | 465 | } |
| ... | ... | @@ -462,7 +467,7 @@ pub fn main(init: std.process.Init) !void { |
| 462 | 467 | if (!test_iterative_only) { |
| 463 | 468 | if (key_size) |size| { |
| 464 | 469 | prng.seed(seed); |
| 465 | const result_small = try benchmarkHashSmallKeys(H, size, count, allocator); | |
| 470 | const result_small = try benchmarkHashSmallKeys(H, size, count, allocator, io); | |
| 466 | 471 | try stdout.print(" small keys: {:3}B {:5} MiB/s {} Hashes/s [{x:0<16}]\n", .{ |
| 467 | 472 | size, |
| 468 | 473 | result_small.throughput / (1 * MiB), |
| ... | ... | @@ -476,9 +481,9 @@ pub fn main(init: std.process.Init) !void { |
| 476 | 481 | inline for (sizes) |exact_size| { |
| 477 | 482 | if (size == exact_size) { |
| 478 | 483 | prng.seed(seed); |
| 479 | const result_array = try benchmarkHashSmallKeysArray(H, exact_size, count, allocator); | |
| 484 | const result_array = try benchmarkHashSmallKeysArray(H, exact_size, count, allocator, io); | |
| 480 | 485 | prng.seed(seed); |
| 481 | const result_ptr = try benchmarkHashSmallKeysArrayPtr(H, exact_size, count, allocator); | |
| 486 | const result_ptr = try benchmarkHashSmallKeysArrayPtr(H, exact_size, count, allocator, io); | |
| 482 | 487 | try stdout.print(" array: {:5} MiB/s [{x:0<16}]\n", .{ |
| 483 | 488 | result_array.throughput / (1 * MiB), |
| 484 | 489 | result_array.hash, |
| ... | ... | @@ -493,7 +498,7 @@ pub fn main(init: std.process.Init) !void { |
| 493 | 498 | } |
| 494 | 499 | } else { |
| 495 | 500 | prng.seed(seed); |
| 496 | const result_small = try benchmarkHashSmallKeys(H, default_small_key_size, count, allocator); | |
| 501 | const result_small = try benchmarkHashSmallKeys(H, default_small_key_size, count, allocator, io); | |
| 497 | 502 | try stdout.print(" small keys: {:3}B {:5} MiB/s {} Hashes/s [{x:0<16}]\n", .{ |
| 498 | 503 | default_small_key_size, |
| 499 | 504 | result_small.throughput / (1 * MiB), |
| ... | ... | @@ -507,7 +512,7 @@ pub fn main(init: std.process.Init) !void { |
| 507 | 512 | try stdout.print(" array:\n", .{}); |
| 508 | 513 | inline for (sizes) |exact_size| { |
| 509 | 514 | prng.seed(seed); |
| 510 | const result = try benchmarkHashSmallKeysArray(H, exact_size, count, allocator); | |
| 515 | const result = try benchmarkHashSmallKeysArray(H, exact_size, count, allocator, io); | |
| 511 | 516 | try stdout.print(" {d: >3}B {:5} MiB/s [{x:0<16}]\n", .{ |
| 512 | 517 | exact_size, |
| 513 | 518 | result.throughput / (1 * MiB), |
| ... | ... | @@ -518,7 +523,7 @@ pub fn main(init: std.process.Init) !void { |
| 518 | 523 | try stdout.print(" array ptr: \n", .{}); |
| 519 | 524 | inline for (sizes) |exact_size| { |
| 520 | 525 | prng.seed(seed); |
| 521 | const result = try benchmarkHashSmallKeysArrayPtr(H, exact_size, count, allocator); | |
| 526 | const result = try benchmarkHashSmallKeysArrayPtr(H, exact_size, count, allocator, io); | |
| 522 | 527 | try stdout.print(" {d: >3}B {:5} MiB/s [{x:0<16}]\n", .{ |
| 523 | 528 | exact_size, |
| 524 | 529 | result.throughput / (1 * MiB), |
lib/std/unicode/throughput_test.zig+14-12| ... | ... | @@ -2,7 +2,6 @@ const std = @import("std"); |
| 2 | 2 | const Io = std.Io; |
| 3 | 3 | const time = std.time; |
| 4 | 4 | const unicode = std.unicode; |
| 5 | const Timer = std.time.Timer; | |
| 6 | 5 | |
| 7 | 6 | const N = 1_000_000; |
| 8 | 7 | |
| ... | ... | @@ -15,12 +14,14 @@ const ResultCount = struct { |
| 15 | 14 | throughput: u64, |
| 16 | 15 | }; |
| 17 | 16 | |
| 18 | fn benchmarkCodepointCount(buf: []const u8) !ResultCount { | |
| 19 | var timer = try Timer.start(); | |
| 17 | fn benchTime(io: Io) i96 { | |
| 18 | return Io.Clock.awake.now(io).nanoseconds; | |
| 19 | } | |
| 20 | 20 | |
| 21 | fn benchmarkCodepointCount(buf: []const u8, io: Io) !ResultCount { | |
| 21 | 22 | const bytes = N * buf.len; |
| 22 | 23 | |
| 23 | const start = timer.lap(); | |
| 24 | const start = benchTime(io); | |
| 24 | 25 | var i: usize = 0; |
| 25 | 26 | var r: usize = undefined; |
| 26 | 27 | while (i < N) : (i += 1) { |
| ... | ... | @@ -30,7 +31,7 @@ fn benchmarkCodepointCount(buf: []const u8) !ResultCount { |
| 30 | 31 | .{buf}, |
| 31 | 32 | ); |
| 32 | 33 | } |
| 33 | const end = timer.read(); | |
| 34 | const end = benchTime(io); | |
| 34 | 35 | |
| 35 | 36 | const elapsed_s = @as(f64, @floatFromInt(end - start)) / time.ns_per_s; |
| 36 | 37 | const throughput = @as(u64, @intFromFloat(@as(f64, @floatFromInt(bytes)) / elapsed_s)); |
| ... | ... | @@ -38,44 +39,45 @@ fn benchmarkCodepointCount(buf: []const u8) !ResultCount { |
| 38 | 39 | return ResultCount{ .count = r, .throughput = throughput }; |
| 39 | 40 | } |
| 40 | 41 | |
| 41 | pub fn main() !void { | |
| 42 | pub fn main(init: std.process.Init) !void { | |
| 42 | 43 | // Size of buffer is about size of printed message. |
| 44 | const io = init.io; | |
| 43 | 45 | var stdout_buffer: [0x100]u8 = undefined; |
| 44 | var stdout_writer = Io.File.stdout().writer(&stdout_buffer); | |
| 46 | var stdout_writer = Io.File.stdout().writer(io, &stdout_buffer); | |
| 45 | 47 | const stdout = &stdout_writer.interface; |
| 46 | 48 | |
| 47 | 49 | try stdout.print("short ASCII strings\n", .{}); |
| 48 | 50 | try stdout.flush(); |
| 49 | 51 | { |
| 50 | const result = try benchmarkCodepointCount("abc"); | |
| 52 | const result = try benchmarkCodepointCount("abc", io); | |
| 51 | 53 | try stdout.print(" count: {:5} MiB/s [{d}]\n", .{ result.throughput / (1 * MiB), result.count }); |
| 52 | 54 | } |
| 53 | 55 | |
| 54 | 56 | try stdout.print("short Unicode strings\n", .{}); |
| 55 | 57 | try stdout.flush(); |
| 56 | 58 | { |
| 57 | const result = try benchmarkCodepointCount("ŌŌŌ"); | |
| 59 | const result = try benchmarkCodepointCount("ŌŌŌ", io); | |
| 58 | 60 | try stdout.print(" count: {:5} MiB/s [{d}]\n", .{ result.throughput / (1 * MiB), result.count }); |
| 59 | 61 | } |
| 60 | 62 | |
| 61 | 63 | try stdout.print("pure ASCII strings\n", .{}); |
| 62 | 64 | try stdout.flush(); |
| 63 | 65 | { |
| 64 | const result = try benchmarkCodepointCount("hello" ** 16); | |
| 66 | const result = try benchmarkCodepointCount("hello" ** 16, io); | |
| 65 | 67 | try stdout.print(" count: {:5} MiB/s [{d}]\n", .{ result.throughput / (1 * MiB), result.count }); |
| 66 | 68 | } |
| 67 | 69 | |
| 68 | 70 | try stdout.print("pure Unicode strings\n", .{}); |
| 69 | 71 | try stdout.flush(); |
| 70 | 72 | { |
| 71 | const result = try benchmarkCodepointCount("こんにちは" ** 16); | |
| 73 | const result = try benchmarkCodepointCount("こんにちは" ** 16, io); | |
| 72 | 74 | try stdout.print(" count: {:5} MiB/s [{d}]\n", .{ result.throughput / (1 * MiB), result.count }); |
| 73 | 75 | } |
| 74 | 76 | |
| 75 | 77 | try stdout.print("mixed ASCII/Unicode strings\n", .{}); |
| 76 | 78 | try stdout.flush(); |
| 77 | 79 | { |
| 78 | const result = try benchmarkCodepointCount("Hyvää huomenta" ** 16); | |
| 80 | const result = try benchmarkCodepointCount("Hyvää huomenta" ** 16, io); | |
| 79 | 81 | try stdout.print(" count: {:5} MiB/s [{d}]\n", .{ result.throughput / (1 * MiB), result.count }); |
| 80 | 82 | } |
| 81 | 83 | try stdout.flush(); |