Files
zigp2p/examples/bench_inline.zig
2026-02-06 21:03:59 -03:00

38 lines
1.2 KiB
Zig

const std = @import("std");
const noise = @import("noise");
const cipher = noise.cipher;
pub fn main() !void {
const allocator = std.heap.page_allocator;
const size: usize = 1024 * 1024;
const plaintext = try allocator.alloc(u8, size);
defer allocator.free(plaintext);
const ciphertext = try allocator.alloc(u8, size + 16);
defer allocator.free(ciphertext);
@memset(plaintext, 0x42);
var cs = cipher.CipherState.initWithKey([_]u8{0x01} ** 32);
// Warmup
for (0..10) |_| {
_ = try cs.encryptWithAd(&.{}, plaintext, ciphertext);
cs.n = 0;
}
const iterations: usize = 100;
const start = std.time.nanoTimestamp();
for (0..iterations) |_| {
_ = try cs.encryptWithAd(&.{}, plaintext, ciphertext);
}
const end = std.time.nanoTimestamp();
const elapsed_ms = @as(f64, @floatFromInt(end - start)) / 1_000_000.0;
const total_mb = @as(f64, @floatFromInt(iterations * size)) / (1024.0 * 1024.0);
const throughput = total_mb / (elapsed_ms / 1000.0);
std.debug.print("Noise cipher: {d:.2} MB/s, {d:.2}ms per 1MB\n", .{
throughput, elapsed_ms / @as(f64, iterations),
});
}