const std = @import("std"); const Client = @import("client.zig").Client; const port = @import("benchmark_options").port; pub fn main() !void { var threaded: std.Io.Threaded = .init(std.heap.smp_allocator, .{}); defer threaded.deinit(); const io = threaded.io(); const small = try std.heap.smp_allocator.alloc(u8, 20480); defer std.heap.smp_allocator.free(small); @memset(small, 'a'); for ([_]usize{ 1024, 131072 }) |size| { const large = try std.heap.smp_allocator.alloc(u8, 40960 + size); defer std.heap.smp_allocator.free(large); @memset(large, 'b'); var count = std.testing.FailingAllocator.init(std.heap.smp_allocator, .{}); const key = "redis-client:command-growth-benchmark"; const start = std.Io.Timestamp.now(io, .awake).nanoseconds; for (0..200) |_| { var client = try Client.connectWithConfig(io, count.allocator(), .{ .host = "127.0.0.1", .port = port }); defer client.close(); for ([_][]const u8{ small, large }) |value| { if (!std.mem.eql(u8, (try client.sendCommand(&.{ "SET", key, value })).asString().?, "OK")) return error.InvalidSet; } const body = (try client.sendCommand(&.{ "GET", key })).asString() orelse return error.MissingPayload; if (!std.mem.eql(u8, body, large)) return error.InvalidPayload; _ = try client.sendCommand(&.{ "DEL", key }); } const elapsed = std.Io.Timestamp.now(io, .awake).nanoseconds - start; if (count.allocated_bytes != count.freed_bytes) return error.Leak; std.debug.print("{{\"bytes\":{d},\"iterations\":200,\"ns_per_cycle\":{d},\"allocations\":{d},\"allocated_bytes\":{d}}}\n", .{ size, @divTrunc(elapsed, 200), count.allocations, count.allocated_bytes }); } }