// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie // SPDX-License-Identifier: MIT //! Microbenchmarks for the parsing, formatting and classification paths. //! //! Run with `zig build bench`. Each benchmark walks a pre-generated corpus of //! addresses and is timed over the whole corpus; the best of several rounds is //! reported, since the minimum is the run least disturbed by the rest of the //! machine. The corpus comes from a fixed seed so runs are comparable. const std = @import("std"); const z46 = @import("z46"); const corpus_len = 100_000; const rounds = 20; pub fn main() !void { var arena_state = std.heap.ArenaAllocator.init(std.heap.page_allocator); defer arena_state.deinit(); const arena = arena_state.allocator(); // Held for the whole run: this is the same stream the timings went to when // this printed them itself. var stderr_buffer: [256]u8 = undefined; const stderr = std.debug.lockStderr(&stderr_buffer); defer std.debug.unlockStderr(); const out = &stderr.file_writer.interface; var prng = std.Random.DefaultPrng.init(0x5eed_c0de); const random = prng.random(); // IPv4 corpus: uniformly random addresses and their canonical text. const v4_addrs = try arena.alloc(z46.Ipv4Address, corpus_len); const v4_texts = try arena.alloc([]const u8, corpus_len); for (v4_addrs, v4_texts) |*address, *text| { address.* = .fromInt(random.int(u32)); var buffer: [z46.Ipv4Address.max_text_len]u8 = undefined; text.* = try arena.dupe(u8, try address.bufPrint(&buffer)); } // IPv6 corpora. Uniformly random 128-bit values almost never contain a // zero group, so the compressed form is generated separately by zeroing a // run of groups, and the IPv4-mapped form separately again. The mixed // corpus used for formatting draws from all three shapes. const v6_addrs = try arena.alloc(z46.Ipv6Address, corpus_len); const v6_full = try arena.alloc([]const u8, corpus_len); const v6_compressed = try arena.alloc([]const u8, corpus_len); const v6_mapped = try arena.alloc([]const u8, corpus_len); for (v6_addrs, v6_full, v6_compressed, v6_mapped) |*address, *full, *compressed, *mapped| { var buffer: [z46.Ipv6Address.max_text_len]u8 = undefined; const dense: z46.Ipv6Address = .fromInt(random.int(u128)); full.* = try arena.dupe(u8, try dense.bufPrint(&buffer)); var groups = dense.groups(); const run_start = random.uintLessThan(usize, 6); const run_len = 2 + random.uintLessThan(usize, 7 - run_start); @memset(groups[run_start..][0..run_len], 0); const sparse: z46.Ipv6Address = .init(groups); compressed.* = try arena.dupe(u8, try sparse.bufPrint(&buffer)); const embedded: z46.Ipv4Address = .fromInt(random.int(u32)); mapped.* = try arena.dupe(u8, try embedded.toIpv6Mapped().bufPrint(&buffer)); address.* = switch (random.uintLessThan(u8, 3)) { 0 => dense, 1 => sparse, else => embedded.toIpv6Mapped(), }; } // Network corpora: a uniformly random prefix length and an address // truncated to it, so the text carries every prefix width and, for IPv6, // the `::` compression that clearing the host bits produces. Each network // is paired with a random address for the `contains` benchmark. const v4_nets = try arena.alloc(z46.Ipv4Network, corpus_len); const v4_net_texts = try arena.alloc([]const u8, corpus_len); for (v4_nets, v4_net_texts) |*network, *text| { const prefix_len = random.uintAtMost(u8, z46.Ipv4Address.bit_count); network.* = z46.Ipv4Network.initTruncate(.fromInt(random.int(u32)), prefix_len) catch unreachable; var buffer: [z46.Ipv4Network.max_text_len]u8 = undefined; text.* = try arena.dupe(u8, try network.bufPrint(&buffer)); } const v6_nets = try arena.alloc(z46.Ipv6Network, corpus_len); const v6_net_texts = try arena.alloc([]const u8, corpus_len); for (v6_nets, v6_net_texts) |*network, *text| { const prefix_len = random.uintAtMost(u8, z46.Ipv6Address.bit_count); network.* = z46.Ipv6Network.initTruncate(.fromInt(random.int(u128)), prefix_len) catch unreachable; var buffer: [z46.Ipv6Network.max_text_len]u8 = undefined; text.* = try arena.dupe(u8, try network.bufPrint(&buffer)); } // Interface corpora: the address corpora above, given the prefix lengths // of the networks above, so the text carries host bits at every prefix // width. These hold the same address and prefix as the network corpora, // untruncated -- which is why their parse timings are the higher of the // two: keeping the host bits leaves longer text for the address parser, // where truncating them shortens an IPv4 address and compresses an IPv6 // one. const v4_ifaces = try arena.alloc(z46.Ipv4Interface, corpus_len); const v4_iface_texts = try arena.alloc([]const u8, corpus_len); for (v4_ifaces, v4_addrs, v4_nets, v4_iface_texts) |*interface, address, network, *text| { interface.* = z46.Ipv4Interface.init(address, network.prefix_len) catch unreachable; var buffer: [z46.Ipv4Interface.max_text_len]u8 = undefined; text.* = try arena.dupe(u8, try interface.bufPrint(&buffer)); } const v6_ifaces = try arena.alloc(z46.Ipv6Interface, corpus_len); const v6_iface_texts = try arena.alloc([]const u8, corpus_len); for (v6_ifaces, v6_addrs, v6_nets, v6_iface_texts) |*interface, address, network, *text| { interface.* = z46.Ipv6Interface.init(address, network.prefix_len) catch unreachable; var buffer: [z46.Ipv6Interface.max_text_len]u8 = undefined; text.* = try arena.dupe(u8, try interface.bufPrint(&buffer)); } // Endpoint corpora: the address corpora above, paired with random ports. const v4_endpoints = try arena.alloc(z46.Ipv4Endpoint, corpus_len); const v4_endpoint_texts = try arena.alloc([]const u8, corpus_len); for (v4_endpoints, v4_addrs, v4_endpoint_texts) |*endpoint, address, *text| { endpoint.* = .init(address, random.int(u16)); var buffer: [z46.Ipv4Endpoint.max_text_len]u8 = undefined; text.* = try arena.dupe(u8, try endpoint.bufPrint(&buffer)); } const v6_endpoints = try arena.alloc(z46.Ipv6Endpoint, corpus_len); const v6_endpoint_texts = try arena.alloc([]const u8, corpus_len); for (v6_endpoints, v6_addrs, v6_endpoint_texts) |*endpoint, address, *text| { endpoint.* = .init(address, random.int(u16)); var buffer: [z46.Ipv6Endpoint.max_text_len]u8 = undefined; text.* = try arena.dupe(u8, try endpoint.bufPrint(&buffer)); } // Range corpora: a uniformly random first address and a span drawn // log-uniformly by shifting a random value right by a random amount. The // spread is the point. `prefixes` yields one block per level it descends, // so its cost is set by how wide the range is and how badly its ends line // up; a uniformly random span would be nearly the whole address space // almost every time, and would measure only the worst case. This corpus // holds the pool-sized ranges somebody writes by hand as well. const v4_ranges = try arena.alloc(z46.Ipv4Range, corpus_len); const v4_range_texts = try arena.alloc([]const u8, corpus_len); for (v4_ranges, v4_range_texts) |*range, *text| { const first = random.int(u32); const span = random.int(u32) >> random.uintAtMost(u5, 31); range.* = z46.Ipv4Range.init(.fromInt(first), .fromInt(first +| span)) catch unreachable; var buffer: [z46.Ipv4Range.max_text_len]u8 = undefined; text.* = try arena.dupe(u8, try range.bufPrint(&buffer)); } const v6_ranges = try arena.alloc(z46.Ipv6Range, corpus_len); const v6_range_texts = try arena.alloc([]const u8, corpus_len); for (v6_ranges, v6_range_texts) |*range, *text| { const first = random.int(u128); const span = random.int(u128) >> random.uintAtMost(u7, 127); range.* = z46.Ipv6Range.init(.fromInt(first), .fromInt(first +| span)) catch unreachable; var buffer: [z46.Ipv6Range.max_text_len]u8 = undefined; text.* = try arena.dupe(u8, try range.bufPrint(&buffer)); } try run(out, "Ipv4Address.parse", benchIpv4Parse, .{v4_texts}); try run(out, "Ipv4Address.format", benchIpv4Format, .{v4_addrs}); try run(out, "Ipv4Address.isGlobal", benchIpv4IsGlobal, .{v4_addrs}); try run(out, "Ipv6Address.parse full", benchIpv6Parse, .{v6_full}); try run(out, "Ipv6Address.parse compressed", benchIpv6Parse, .{v6_compressed}); try run(out, "Ipv6Address.parse mapped", benchIpv6Parse, .{v6_mapped}); try run(out, "Ipv6Address.format", benchIpv6Format, .{v6_addrs}); try run(out, "Ipv6Address.isGlobal", benchIpv6IsGlobal, .{v6_addrs}); try run(out, "Ipv4Network.parse", benchIpv4NetworkParse, .{v4_net_texts}); try run(out, "Ipv4Network.format", benchIpv4NetworkFormat, .{v4_nets}); try run(out, "Ipv4Network.contains", benchIpv4NetworkContains, .{ v4_nets, v4_addrs }); try run(out, "Ipv6Network.parse", benchIpv6NetworkParse, .{v6_net_texts}); try run(out, "Ipv6Network.format", benchIpv6NetworkFormat, .{v6_nets}); try run(out, "Ipv6Network.contains", benchIpv6NetworkContains, .{ v6_nets, v6_addrs }); try run(out, "Ipv4Interface.parse", benchIpv4InterfaceParse, .{v4_iface_texts}); try run(out, "Ipv4Interface.format", benchIpv4InterfaceFormat, .{v4_ifaces}); try run(out, "Ipv4Interface.network", benchIpv4InterfaceNetwork, .{v4_ifaces}); try run(out, "Ipv6Interface.parse", benchIpv6InterfaceParse, .{v6_iface_texts}); try run(out, "Ipv6Interface.format", benchIpv6InterfaceFormat, .{v6_ifaces}); try run(out, "Ipv6Interface.network", benchIpv6InterfaceNetwork, .{v6_ifaces}); try run(out, "Ipv4Range.parse", benchIpv4RangeParse, .{v4_range_texts}); try run(out, "Ipv4Range.format", benchIpv4RangeFormat, .{v4_ranges}); try run(out, "Ipv4Range.prefixes", benchIpv4RangePrefixes, .{v4_ranges}); try run(out, "Ipv6Range.parse", benchIpv6RangeParse, .{v6_range_texts}); try run(out, "Ipv6Range.format", benchIpv6RangeFormat, .{v6_ranges}); try run(out, "Ipv6Range.prefixes", benchIpv6RangePrefixes, .{v6_ranges}); try run(out, "Ipv4Endpoint.parse", benchIpv4EndpointParse, .{v4_endpoint_texts}); try run(out, "Ipv4Endpoint.format", benchIpv4EndpointFormat, .{v4_endpoints}); try run(out, "Ipv6Endpoint.parse", benchIpv6EndpointParse, .{v6_endpoint_texts}); try run(out, "Ipv6Endpoint.format", benchIpv6EndpointFormat, .{v6_endpoints}); } /// Nanoseconds from the monotonic clock. fn now() u64 { var timespec: std.posix.timespec = undefined; if (std.posix.errno(std.posix.system.clock_gettime(.MONOTONIC, ×pec)) != .SUCCESS) unreachable; return @as(u64, @intCast(timespec.sec)) * std.time.ns_per_s + @as(u64, @intCast(timespec.nsec)); } /// Times `f(args...)` over `rounds` passes and writes the best pass to `w`. /// The accumulator each benchmark returns is kept alive so the work cannot be /// optimized away. /// /// The line goes to a writer rather than to `std.debug.print` so that this can /// run with nothing attached to the terminal, which is what its own test does. fn run(w: *std.Io.Writer, name: []const u8, f: anytype, args: anytype) !void { var best: u64 = std.math.maxInt(u64); for (0..rounds) |_| { const start = now(); const accumulator = @call(.auto, f, args); const elapsed = now() - start; std.mem.doNotOptimizeAway(accumulator); best = @min(best, elapsed); } const ns_per_op = @as(f64, @floatFromInt(best)) / @as(f64, @floatFromInt(corpus_len)); try w.print("{s:<30} {d:>8.1} ns/op {d:>8.1} M/s\n", .{ name, ns_per_op, 1000.0 / ns_per_op }); // Each line as it is measured rather than the lot of them at the end: a // full run takes long enough that the progress is worth having. try w.flush(); } fn benchIpv4Parse(texts: []const []const u8) u64 { var accumulator: u64 = 0; for (texts) |text| { const address = z46.Ipv4Address.parse(text) catch unreachable; accumulator +%= address.toInt(); } return accumulator; } fn benchIpv4Format(addrs: []const z46.Ipv4Address) u64 { var accumulator: u64 = 0; for (addrs) |address| { var buffer: [z46.Ipv4Address.max_text_len]u8 = undefined; const text = address.bufPrint(&buffer) catch unreachable; // The bytes themselves feed the accumulator; consuming only the length // would let the compiler skip producing the text at all. accumulator +%= text.len +% text[0] +% text[text.len - 1]; } return accumulator; } fn benchIpv4IsGlobal(addrs: []const z46.Ipv4Address) u64 { var accumulator: u64 = 0; for (addrs) |address| accumulator +%= @intFromBool(address.isGlobal()); return accumulator; } fn benchIpv6Parse(texts: []const []const u8) u64 { var accumulator: u64 = 0; for (texts) |text| { const address = z46.Ipv6Address.parse(text) catch unreachable; accumulator +%= @truncate(address.toInt()); } return accumulator; } fn benchIpv6Format(addrs: []const z46.Ipv6Address) u64 { var accumulator: u64 = 0; for (addrs) |address| { var buffer: [z46.Ipv6Address.max_text_len]u8 = undefined; const text = address.bufPrint(&buffer) catch unreachable; // The bytes themselves feed the accumulator; consuming only the length // would let the compiler skip producing the text at all. accumulator +%= text.len +% text[0] +% text[text.len - 1]; } return accumulator; } fn benchIpv6IsGlobal(addrs: []const z46.Ipv6Address) u64 { var accumulator: u64 = 0; for (addrs) |address| accumulator +%= @intFromBool(address.isGlobal()); return accumulator; } fn benchIpv4NetworkParse(texts: []const []const u8) u64 { var accumulator: u64 = 0; for (texts) |text| { const network = z46.Ipv4Network.parse(text) catch unreachable; accumulator +%= network.address.toInt() +% network.prefix_len; } return accumulator; } fn benchIpv4NetworkFormat(nets: []const z46.Ipv4Network) u64 { var accumulator: u64 = 0; for (nets) |network| { var buffer: [z46.Ipv4Network.max_text_len]u8 = undefined; const text = network.bufPrint(&buffer) catch unreachable; accumulator +%= text.len +% text[0] +% text[text.len - 1]; } return accumulator; } fn benchIpv4NetworkContains(nets: []const z46.Ipv4Network, addrs: []const z46.Ipv4Address) u64 { var accumulator: u64 = 0; for (nets, addrs) |network, address| accumulator +%= @intFromBool(network.contains(address)); return accumulator; } fn benchIpv6NetworkParse(texts: []const []const u8) u64 { var accumulator: u64 = 0; for (texts) |text| { const network = z46.Ipv6Network.parse(text) catch unreachable; accumulator +%= @as(u64, @truncate(network.address.toInt())) +% network.prefix_len; } return accumulator; } fn benchIpv6NetworkFormat(nets: []const z46.Ipv6Network) u64 { var accumulator: u64 = 0; for (nets) |network| { var buffer: [z46.Ipv6Network.max_text_len]u8 = undefined; const text = network.bufPrint(&buffer) catch unreachable; accumulator +%= text.len +% text[0] +% text[text.len - 1]; } return accumulator; } fn benchIpv6NetworkContains(nets: []const z46.Ipv6Network, addrs: []const z46.Ipv6Address) u64 { var accumulator: u64 = 0; for (nets, addrs) |network, address| accumulator +%= @intFromBool(network.contains(address)); return accumulator; } fn benchIpv4InterfaceParse(texts: []const []const u8) u64 { var accumulator: u64 = 0; for (texts) |text| { const interface = z46.Ipv4Interface.parse(text) catch unreachable; accumulator +%= interface.address.toInt() +% interface.prefix_len; } return accumulator; } fn benchIpv4InterfaceFormat(ifaces: []const z46.Ipv4Interface) u64 { var accumulator: u64 = 0; for (ifaces) |interface| { var buffer: [z46.Ipv4Interface.max_text_len]u8 = undefined; const text = interface.bufPrint(&buffer) catch unreachable; accumulator +%= text.len +% text[0] +% text[text.len - 1]; } return accumulator; } fn benchIpv4InterfaceNetwork(ifaces: []const z46.Ipv4Interface) u64 { var accumulator: u64 = 0; for (ifaces) |interface| accumulator +%= interface.network().address.toInt(); return accumulator; } fn benchIpv6InterfaceParse(texts: []const []const u8) u64 { var accumulator: u64 = 0; for (texts) |text| { const interface = z46.Ipv6Interface.parse(text) catch unreachable; accumulator +%= @as(u64, @truncate(interface.address.toInt())) +% interface.prefix_len; } return accumulator; } fn benchIpv6InterfaceFormat(ifaces: []const z46.Ipv6Interface) u64 { var accumulator: u64 = 0; for (ifaces) |interface| { var buffer: [z46.Ipv6Interface.max_text_len]u8 = undefined; const text = interface.bufPrint(&buffer) catch unreachable; accumulator +%= text.len +% text[0] +% text[text.len - 1]; } return accumulator; } fn benchIpv6InterfaceNetwork(ifaces: []const z46.Ipv6Interface) u64 { var accumulator: u64 = 0; for (ifaces) |interface| accumulator +%= @truncate(interface.network().address.toInt()); return accumulator; } fn benchIpv4RangeParse(texts: []const []const u8) u64 { var accumulator: u64 = 0; for (texts) |text| { const range = z46.Ipv4Range.parse(text) catch unreachable; accumulator +%= range.first.toInt() +% range.last.toInt(); } return accumulator; } fn benchIpv4RangeFormat(ranges: []const z46.Ipv4Range) u64 { var accumulator: u64 = 0; for (ranges) |range| { var buffer: [z46.Ipv4Range.max_text_len]u8 = undefined; const text = range.bufPrint(&buffer) catch unreachable; accumulator +%= text.len +% text[0] +% text[text.len - 1]; } return accumulator; } /// The whole cover, not one block: the cost of `prefixes` is the walk, and /// stopping after the first block would measure something nobody asks for. fn benchIpv4RangePrefixes(ranges: []const z46.Ipv4Range) u64 { var accumulator: u64 = 0; for (ranges) |range| { var it = range.prefixes(); while (it.next()) |network| accumulator +%= network.prefix_len; } return accumulator; } fn benchIpv6RangeParse(texts: []const []const u8) u64 { var accumulator: u64 = 0; for (texts) |text| { const range = z46.Ipv6Range.parse(text) catch unreachable; accumulator +%= @truncate(range.first.toInt() +% range.last.toInt()); } return accumulator; } fn benchIpv6RangeFormat(ranges: []const z46.Ipv6Range) u64 { var accumulator: u64 = 0; for (ranges) |range| { var buffer: [z46.Ipv6Range.max_text_len]u8 = undefined; const text = range.bufPrint(&buffer) catch unreachable; accumulator +%= text.len +% text[0] +% text[text.len - 1]; } return accumulator; } fn benchIpv6RangePrefixes(ranges: []const z46.Ipv6Range) u64 { var accumulator: u64 = 0; for (ranges) |range| { var it = range.prefixes(); while (it.next()) |network| accumulator +%= network.prefix_len; } return accumulator; } fn benchIpv4EndpointParse(texts: []const []const u8) u64 { var accumulator: u64 = 0; for (texts) |text| { const endpoint = z46.Ipv4Endpoint.parse(text) catch unreachable; accumulator +%= endpoint.address.toInt() +% endpoint.port; } return accumulator; } fn benchIpv4EndpointFormat(endpoints: []const z46.Ipv4Endpoint) u64 { var accumulator: u64 = 0; for (endpoints) |endpoint| { var buffer: [z46.Ipv4Endpoint.max_text_len]u8 = undefined; const text = endpoint.bufPrint(&buffer) catch unreachable; accumulator +%= text.len +% text[0] +% text[text.len - 1]; } return accumulator; } fn benchIpv6EndpointParse(texts: []const []const u8) u64 { var accumulator: u64 = 0; for (texts) |text| { const endpoint = z46.Ipv6Endpoint.parse(text) catch unreachable; accumulator +%= @as(u64, @truncate(endpoint.address.toInt())) +% endpoint.port; } return accumulator; } fn benchIpv6EndpointFormat(endpoints: []const z46.Ipv6Endpoint) u64 { var accumulator: u64 = 0; for (endpoints) |endpoint| { var buffer: [z46.Ipv6Endpoint.max_text_len]u8 = undefined; const text = endpoint.bufPrint(&buffer) catch unreachable; accumulator +%= text.len +% text[0] +% text[text.len - 1]; } return accumulator; } const testing = std.testing; test now { // A monotonic clock, which is what makes the difference of two readings a // duration rather than an accident of the wall clock being adjusted. const first = now(); const second = now(); try testing.expect(second >= first); } test run { // Times the function over `rounds` passes and reports the best of them. // Nothing comes back: the accumulator goes to `doNotOptimizeAway` rather // than to the caller, so the work cannot be optimized out from under the // clock. var out: std.Io.Writer.Allocating = .init(testing.allocator); defer out.deinit(); try run(&out.writer, "Ipv4Address.parse", benchIpv4Parse, .{&[_][]const u8{"192.0.2.1"}}); // One line per benchmark, the name padded so that a run reads as a table. try testing.expect(std.mem.startsWith(u8, out.written(), "Ipv4Address.parse ")); try testing.expect(std.mem.endsWith(u8, out.written(), "M/s\n")); } test benchIpv4Parse { // Every benchmark returns a checksum of the work it did, which is what // keeps the compiler from eliding the work itself. try testing.expectEqual( @as(u64, 0xc000_0201) +% 0x0a00_0001, benchIpv4Parse(&.{ "192.0.2.1", "10.0.0.1" }), ); } test benchIpv4Format { // The checksum takes the length and the first and last bytes, so two // addresses whose text is the same length still check differently. // Consuming only the length would let the compiler skip producing the // text at all. const a = try z46.Ipv4Address.parse("1.0.0.1"); const b = try z46.Ipv4Address.parse("2.0.0.2"); try testing.expect(benchIpv4Format(&.{a}) != benchIpv4Format(&.{b})); // And the whole corpus is walked: the total is the sum of the parts. try testing.expectEqual( benchIpv4Format(&.{a}) +% benchIpv4Format(&.{b}), benchIpv4Format(&.{ a, b }), ); } test benchIpv4IsGlobal { try testing.expectEqual(@as(u64, 1), benchIpv4IsGlobal(&.{ try .parse("1.1.1.1"), try .parse("10.0.0.1"), })); } test benchIpv6Parse { // The low 64 bits of each address, which is all a `u64` accumulator can // hold of a 128-bit one. try testing.expectEqual(@as(u64, 3), benchIpv6Parse(&.{ "2001:db8::1", "2001:db8::2" })); } test benchIpv6Format { const a = try z46.Ipv6Address.parse("2001:db8::1"); const b = try z46.Ipv6Address.parse("2001:db8::2"); try testing.expect(benchIpv6Format(&.{a}) != benchIpv6Format(&.{b})); try testing.expectEqual( benchIpv6Format(&.{a}) +% benchIpv6Format(&.{b}), benchIpv6Format(&.{ a, b }), ); } test benchIpv6IsGlobal { try testing.expectEqual(@as(u64, 1), benchIpv6IsGlobal(&.{ try .parse("2606:4700:4700::1111"), try .parse("fd00::1"), })); } test benchIpv4NetworkParse { // The address and the prefix length both feed the checksum, so neither // half of the parse can be skipped. try testing.expectEqual( @as(u64, 0xc0a8_0100) +% 24, benchIpv4NetworkParse(&.{"192.168.1.0/24"}), ); } test benchIpv4NetworkFormat { const a = try z46.Ipv4Network.parse("10.0.0.0/8"); const b = try z46.Ipv4Network.parse("20.0.0.0/8"); try testing.expect(benchIpv4NetworkFormat(&.{a}) != benchIpv4NetworkFormat(&.{b})); try testing.expectEqual( benchIpv4NetworkFormat(&.{a}) +% benchIpv4NetworkFormat(&.{b}), benchIpv4NetworkFormat(&.{ a, b }), ); } test benchIpv4NetworkContains { // The two slices are walked in step, one address per network. try testing.expectEqual(@as(u64, 1), benchIpv4NetworkContains( &.{ try .parse("192.168.1.0/24"), try .parse("192.168.1.0/24") }, &.{ try .parse("192.168.1.42"), try .parse("192.168.2.42") }, )); } test benchIpv6NetworkParse { // The low 64 bits of `2001:db8::` are zero, so what is left is the prefix // length. try testing.expectEqual(@as(u64, 32), benchIpv6NetworkParse(&.{"2001:db8::/32"})); } test benchIpv6NetworkFormat { const a = try z46.Ipv6Network.parse("2001:db8::/32"); const b = try z46.Ipv6Network.parse("2001:db8::/64"); try testing.expect(benchIpv6NetworkFormat(&.{a}) != benchIpv6NetworkFormat(&.{b})); try testing.expectEqual( benchIpv6NetworkFormat(&.{a}) +% benchIpv6NetworkFormat(&.{b}), benchIpv6NetworkFormat(&.{ a, b }), ); } test benchIpv6NetworkContains { try testing.expectEqual(@as(u64, 1), benchIpv6NetworkContains( &.{ try .parse("2001:db8::/32"), try .parse("2001:db8::/32") }, &.{ try .parse("2001:db8::1"), try .parse("2001:db9::1") }, )); } test benchIpv4InterfaceParse { // The host bits survive, which is the difference from the network parse // above and what the checksum would show if they did not. try testing.expectEqual( @as(u64, 0xc0a8_012a) +% 24, benchIpv4InterfaceParse(&.{"192.168.1.42/24"}), ); } test benchIpv4InterfaceFormat { const a = try z46.Ipv4Interface.parse("10.0.0.1/8"); const b = try z46.Ipv4Interface.parse("20.0.0.2/8"); try testing.expect(benchIpv4InterfaceFormat(&.{a}) != benchIpv4InterfaceFormat(&.{b})); try testing.expectEqual( benchIpv4InterfaceFormat(&.{a}) +% benchIpv4InterfaceFormat(&.{b}), benchIpv4InterfaceFormat(&.{ a, b }), ); } test benchIpv4InterfaceNetwork { // What is checksummed is the network's address, so the host bits are gone // from it. const eth0 = try z46.Ipv4Interface.parse("192.168.1.42/24"); try testing.expectEqual(@as(u64, 0xc0a8_0100), benchIpv4InterfaceNetwork(&.{eth0})); } test benchIpv6InterfaceParse { // The low 64 bits of the address, host bits and all, plus the prefix. try testing.expectEqual(@as(u64, 1) +% 64, benchIpv6InterfaceParse(&.{"2001:db8::1/64"})); } test benchIpv6InterfaceFormat { const a = try z46.Ipv6Interface.parse("2001:db8::1/64"); const b = try z46.Ipv6Interface.parse("2001:db8::1/96"); try testing.expect(benchIpv6InterfaceFormat(&.{a}) != benchIpv6InterfaceFormat(&.{b})); try testing.expectEqual( benchIpv6InterfaceFormat(&.{a}) +% benchIpv6InterfaceFormat(&.{b}), benchIpv6InterfaceFormat(&.{ a, b }), ); } test benchIpv6InterfaceNetwork { // What is checksummed is the network's address, so the host bits are gone // from it. const eth0 = try z46.Ipv6Interface.parse("2001:db8::1:2:3:4/96"); try testing.expectEqual( @as(u64, @truncate(eth0.network().address.toInt())), benchIpv6InterfaceNetwork(&.{eth0}), ); } test benchIpv4RangeParse { try testing.expectEqual( @as(u64, 0x0a00_000a) +% 0x0a00_0014, benchIpv4RangeParse(&.{"10.0.0.10-10.0.0.20"}), ); } test benchIpv4RangeFormat { const a = try z46.Ipv4Range.parse("1.0.0.1-1.0.0.2"); const b = try z46.Ipv4Range.parse("9.0.0.8-9.0.0.9"); // Same length, different bytes, so the checksum still tells them apart. // The two ends have to differ in more than their sum: the checksum takes // the first and last bytes and adds them, so `1...9` and `2...8` collide. try testing.expect(benchIpv4RangeFormat(&.{a}) != benchIpv4RangeFormat(&.{b})); try testing.expectEqual( benchIpv4RangeFormat(&.{a}) +% benchIpv4RangeFormat(&.{b}), benchIpv4RangeFormat(&.{ a, b }), ); } test benchIpv4RangePrefixes { // 10.0.0.10-10.0.0.20 covers as a /31, two /30s and a /32, so the // checksum is the sum of those prefix lengths and the whole walk is // accounted for. try testing.expectEqual( @as(u64, 31 + 30 + 30 + 32), benchIpv4RangePrefixes(&.{try .parse("10.0.0.10-10.0.0.20")}), ); } test benchIpv6RangeParse { try testing.expectEqual( @as(u64, 0xa +% 0x14), benchIpv6RangeParse(&.{"2001:db8::a-2001:db8::14"}), ); } test benchIpv6RangeFormat { const a = try z46.Ipv6Range.parse("2001:db8::1-2001:db8::9"); const b = try z46.Ipv6Range.parse("2001:db8::2-2001:db8::8"); try testing.expect(benchIpv6RangeFormat(&.{a}) != benchIpv6RangeFormat(&.{b})); } test benchIpv6RangePrefixes { try testing.expectEqual( @as(u64, 127 + 126 + 126 + 128), benchIpv6RangePrefixes(&.{try .parse("2001:db8::a-2001:db8::14")}), ); } test benchIpv4EndpointParse { // The address and the port both feed the checksum, so neither half of the // parse can be skipped. try testing.expectEqual( @as(u64, 0xc000_0201) +% 443, benchIpv4EndpointParse(&.{"192.0.2.1:443"}), ); } test benchIpv4EndpointFormat { const a = try z46.Ipv4Endpoint.parse("10.0.0.1:80"); const b = try z46.Ipv4Endpoint.parse("20.0.0.2:81"); try testing.expect(benchIpv4EndpointFormat(&.{a}) != benchIpv4EndpointFormat(&.{b})); try testing.expectEqual( benchIpv4EndpointFormat(&.{a}) +% benchIpv4EndpointFormat(&.{b}), benchIpv4EndpointFormat(&.{ a, b }), ); } test benchIpv6EndpointParse { try testing.expectEqual( @as(u64, 1) +% 443, benchIpv6EndpointParse(&.{"[2001:db8::1]:443"}), ); } test benchIpv6EndpointFormat { const a = try z46.Ipv6Endpoint.parse("[2001:db8::1]:80"); const b = try z46.Ipv6Endpoint.parse("[2001:db8::2]:81"); try testing.expect(benchIpv6EndpointFormat(&.{a}) != benchIpv6EndpointFormat(&.{b})); try testing.expectEqual( benchIpv6EndpointFormat(&.{a}) +% benchIpv6EndpointFormat(&.{b}), benchIpv6EndpointFormat(&.{ a, b }), ); }