270 lines
9.2 KiB
Zig
270 lines
9.2 KiB
Zig
//! Performance benchmark for P2P core networking module
|
|
//!
|
|
//! This benchmark measures the performance of key operations in the
|
|
//! core networking module including buffer operations, address parsing,
|
|
//! socket operations, and connection management.
|
|
|
|
const std = @import("std");
|
|
const mem = std.mem;
|
|
|
|
const core = @import("core");
|
|
const Error = core.errors.Error;
|
|
const Address = core.address.Address;
|
|
const Socket = core.socket.Socket;
|
|
const Connection = core.connection.Connection;
|
|
const ConnectionConfig = core.connection.ConnectionConfig;
|
|
const Buffer = core.buffer;
|
|
|
|
pub fn main() !void {
|
|
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
|
defer _ = gpa.deinit();
|
|
const allocator = gpa.allocator();
|
|
|
|
std.debug.print("=== P2P Core Networking Performance Benchmark ===\n\n", .{});
|
|
|
|
// Benchmark configuration
|
|
const iterations = 10000;
|
|
const buffer_size = 1024;
|
|
|
|
// Run benchmarks
|
|
try benchmarkAddressParsing(iterations);
|
|
try benchmarkBufferOperations(allocator, 500, buffer_size); // Stay under BufferManager rate limit (1000/s)
|
|
try benchmarkSocketCreation(allocator, iterations);
|
|
try benchmarkConnectionCreation(allocator, iterations);
|
|
try benchmarkAddressConversion(allocator, iterations);
|
|
|
|
std.debug.print("\n=== Benchmark Complete ===\n", .{});
|
|
}
|
|
|
|
fn benchmarkAddressParsing(iterations: usize) !void {
|
|
std.debug.print("=== Address Parsing Benchmark ===\n", .{});
|
|
|
|
const addresses = [_][]const u8{
|
|
"192.168.1.100:8080",
|
|
"127.0.0.1:443",
|
|
"8.8.8.8:53",
|
|
"10.0.0.1:9999",
|
|
"172.16.0.1:80",
|
|
"0.0.0.0:0",
|
|
};
|
|
|
|
var total_time: i128 = 0;
|
|
var successful_parses: usize = 0;
|
|
|
|
for (0..iterations) |i| {
|
|
const addr_str = addresses[i % addresses.len];
|
|
|
|
const start_time = std.time.nanoTimestamp();
|
|
const addr = Address.parse(addr_str) catch {
|
|
continue;
|
|
};
|
|
const end_time = std.time.nanoTimestamp();
|
|
|
|
total_time += end_time - start_time;
|
|
successful_parses += 1;
|
|
|
|
// Verify the address is valid
|
|
_ = addr.isValid();
|
|
}
|
|
|
|
if (successful_parses > 0) {
|
|
const avg_time_ns = @divTrunc(total_time, @as(i128, @intCast(successful_parses)));
|
|
const total_f: f64 = @floatFromInt(total_time);
|
|
const success_f: f64 = @floatFromInt(successful_parses);
|
|
const throughput = success_f * 1_000_000_000.0 / total_f;
|
|
|
|
std.debug.print(" Iterations: {}\n", .{iterations});
|
|
std.debug.print(" Successful: {}\n", .{successful_parses});
|
|
std.debug.print(" Average time: {} ns\n", .{avg_time_ns});
|
|
std.debug.print(" Throughput: {d:.2} parses/sec\n", .{throughput});
|
|
}
|
|
std.debug.print("\n", .{});
|
|
}
|
|
|
|
fn benchmarkBufferOperations(allocator: mem.Allocator, iterations: usize, buffer_size: usize) !void {
|
|
std.debug.print("=== Buffer Operations Benchmark ===\n", .{});
|
|
|
|
var manager = try Buffer.BufferManager.init(.{}, allocator);
|
|
defer manager.deinit();
|
|
|
|
// Benchmark buffer creation
|
|
var create_time: i128 = 0;
|
|
var create_count: usize = 0;
|
|
|
|
for (0..iterations) |_| {
|
|
const start_time = std.time.nanoTimestamp();
|
|
var buf = manager.createStreamBuffer(buffer_size) catch continue;
|
|
const end_time = std.time.nanoTimestamp();
|
|
|
|
create_time += end_time - start_time;
|
|
create_count += 1;
|
|
buf.release();
|
|
}
|
|
|
|
// Benchmark buffer operations
|
|
var buffer = try manager.createStreamBuffer(buffer_size);
|
|
defer buffer.release();
|
|
|
|
const test_data = "A" ** 256; // 256 bytes of test data
|
|
|
|
var write_time: i128 = 0;
|
|
var read_time: i128 = 0;
|
|
var write_count: usize = 0;
|
|
var read_count: usize = 0;
|
|
|
|
for (0..iterations) |_| {
|
|
// Write benchmark
|
|
{
|
|
const start_time = std.time.nanoTimestamp();
|
|
buffer.write(test_data) catch continue;
|
|
const end_time = std.time.nanoTimestamp();
|
|
write_time += end_time - start_time;
|
|
write_count += 1;
|
|
}
|
|
|
|
// Read benchmark
|
|
{
|
|
const start_time = std.time.nanoTimestamp();
|
|
var read_buffer: [1024]u8 = undefined;
|
|
_ = buffer.read(&read_buffer) catch continue;
|
|
const end_time = std.time.nanoTimestamp();
|
|
read_time += end_time - start_time;
|
|
read_count += 1;
|
|
}
|
|
}
|
|
|
|
if (create_count > 0) {
|
|
const avg_create = @divTrunc(create_time, @as(i128, @intCast(create_count)));
|
|
std.debug.print(" Iterations: {}\n", .{iterations});
|
|
std.debug.print(" Buffer size: {} bytes\n", .{buffer_size});
|
|
std.debug.print(" Create avg time: {} ns\n", .{avg_create});
|
|
}
|
|
if (write_count > 0) {
|
|
const avg_write = @divTrunc(write_time, @as(i128, @intCast(write_count)));
|
|
std.debug.print(" Write avg time: {} ns\n", .{avg_write});
|
|
}
|
|
if (read_count > 0) {
|
|
const avg_read = @divTrunc(read_time, @as(i128, @intCast(read_count)));
|
|
std.debug.print(" Read avg time: {} ns\n", .{avg_read});
|
|
}
|
|
std.debug.print("\n", .{});
|
|
}
|
|
|
|
fn benchmarkSocketCreation(allocator: mem.Allocator, iterations: usize) !void {
|
|
std.debug.print("=== Socket Creation Benchmark ===\n", .{});
|
|
|
|
var total_time: i128 = 0;
|
|
var successful_creates: usize = 0;
|
|
|
|
for (0..iterations) |_| {
|
|
const start_time = std.time.nanoTimestamp();
|
|
var socket = Socket.init(.tcp, allocator) catch {
|
|
continue;
|
|
};
|
|
const end_time = std.time.nanoTimestamp();
|
|
|
|
total_time += end_time - start_time;
|
|
successful_creates += 1;
|
|
|
|
socket.deinit();
|
|
}
|
|
|
|
if (successful_creates > 0) {
|
|
const avg_time_ns = @divTrunc(total_time, @as(i128, @intCast(successful_creates)));
|
|
const total_f: f64 = @floatFromInt(total_time);
|
|
const success_f: f64 = @floatFromInt(successful_creates);
|
|
const throughput = success_f * 1_000_000_000.0 / total_f;
|
|
|
|
std.debug.print(" Iterations: {}\n", .{iterations});
|
|
std.debug.print(" Successful: {}\n", .{successful_creates});
|
|
std.debug.print(" Average time: {} ns\n", .{avg_time_ns});
|
|
std.debug.print(" Throughput: {d:.2} socket creations/sec\n", .{throughput});
|
|
}
|
|
std.debug.print("\n", .{});
|
|
}
|
|
|
|
fn benchmarkConnectionCreation(allocator: mem.Allocator, iterations: usize) !void {
|
|
std.debug.print("=== Connection Creation Benchmark ===\n", .{});
|
|
|
|
const config = ConnectionConfig{ .connection_type = .tcp };
|
|
|
|
var total_time: i128 = 0;
|
|
var successful_creates: usize = 0;
|
|
|
|
for (0..iterations) |_| {
|
|
const start_time = std.time.nanoTimestamp();
|
|
var connection = Connection.init(.tcp, config, allocator) catch {
|
|
continue;
|
|
};
|
|
const end_time = std.time.nanoTimestamp();
|
|
|
|
total_time += end_time - start_time;
|
|
successful_creates += 1;
|
|
|
|
connection.deinit();
|
|
}
|
|
|
|
if (successful_creates > 0) {
|
|
const avg_time_ns = @divTrunc(total_time, @as(i128, @intCast(successful_creates)));
|
|
const total_f: f64 = @floatFromInt(total_time);
|
|
const success_f: f64 = @floatFromInt(successful_creates);
|
|
const throughput = success_f * 1_000_000_000.0 / total_f;
|
|
|
|
std.debug.print(" Iterations: {}\n", .{iterations});
|
|
std.debug.print(" Successful: {}\n", .{successful_creates});
|
|
std.debug.print(" Average time: {} ns\n", .{avg_time_ns});
|
|
std.debug.print(" Throughput: {d:.2} connection creations/sec\n", .{throughput});
|
|
}
|
|
std.debug.print("\n", .{});
|
|
}
|
|
|
|
fn benchmarkAddressConversion(allocator: mem.Allocator, iterations: usize) !void {
|
|
std.debug.print("=== Address Conversion Benchmark ===\n", .{});
|
|
|
|
const addresses = try createTestAddresses(allocator);
|
|
defer allocator.free(addresses);
|
|
|
|
var total_time: i128 = 0;
|
|
var successful_conversions: usize = 0;
|
|
|
|
for (0..iterations) |i| {
|
|
const addr = addresses[i % addresses.len];
|
|
|
|
const start_time = std.time.nanoTimestamp();
|
|
const sock_addr = addr.toSockAddr() catch {
|
|
continue;
|
|
};
|
|
const end_time = std.time.nanoTimestamp();
|
|
|
|
total_time += end_time - start_time;
|
|
successful_conversions += 1;
|
|
|
|
// Also test conversion back
|
|
_ = Address.fromSockAddr(sock_addr) catch {};
|
|
}
|
|
|
|
if (successful_conversions > 0) {
|
|
const avg_time_ns = @divTrunc(total_time, @as(i128, @intCast(successful_conversions)));
|
|
const total_f: f64 = @floatFromInt(total_time);
|
|
const success_f: f64 = @floatFromInt(successful_conversions);
|
|
const throughput = success_f * 1_000_000_000.0 / total_f;
|
|
|
|
std.debug.print(" Iterations: {}\n", .{iterations});
|
|
std.debug.print(" Successful: {}\n", .{successful_conversions});
|
|
std.debug.print(" Average time: {} ns\n", .{avg_time_ns});
|
|
std.debug.print(" Throughput: {d:.2} conversions/sec\n", .{throughput});
|
|
}
|
|
std.debug.print("\n", .{});
|
|
}
|
|
|
|
fn createTestAddresses(allocator: mem.Allocator) ![]Address {
|
|
const addresses = try allocator.alloc(Address, 4);
|
|
|
|
addresses[0] = try Address.parse("192.168.1.100:8080");
|
|
addresses[1] = try Address.parse("127.0.0.1:443");
|
|
addresses[2] = try Address.parse("10.0.0.1:8080");
|
|
addresses[3] = try Address.parse("8.8.8.8:53");
|
|
|
|
return addresses;
|
|
}
|