#![allow(missing_docs)]
#![allow(unused_crate_dependencies)]
#![allow(unused_results)]
use bare_types::net::{
Cidr, DomainName, Email, Host, Hostname, IpAddr, Ipv4Range, Ipv6Range, MacAddress, Port,
SocketAddr, Url, Uuid,
};
use bare_types::sys::{Arch, Distro, Fd, KernelVersion, OsType, OsVersion, Pid, Username};
use criterion as _;
use criterion::{Criterion, criterion_group, criterion_main};
use std::hint::black_box;
fn bench_ipaddr_parsing(c: &mut Criterion) {
let mut group = c.benchmark_group("ipaddr_parsing");
group.bench_function("ipv4", |b| {
b.iter(|| black_box("192.168.1.1".parse::<IpAddr>().unwrap()))
});
group.bench_function("ipv6_loopback", |b| {
b.iter(|| black_box("::1".parse::<IpAddr>().unwrap()))
});
group.bench_function("ipv6_full", |b| {
b.iter(|| {
black_box(
"2001:0db8:85a3:0000:0000:8a2e:0370:7334"
.parse::<IpAddr>()
.unwrap(),
)
})
});
group.bench_function("ipv4_private_10", |b| {
b.iter(|| black_box("10.0.0.1".parse::<IpAddr>().unwrap()))
});
group.bench_function("ipv4_private_172", |b| {
b.iter(|| black_box("172.16.0.1".parse::<IpAddr>().unwrap()))
});
group.bench_function("ipv4_private_192", |b| {
b.iter(|| black_box("192.168.1.1".parse::<IpAddr>().unwrap()))
});
group.finish();
}
fn bench_ipaddr_operations(c: &mut Criterion) {
let ipv4: IpAddr = "192.168.1.1".parse().unwrap();
let ipv6: IpAddr = "::1".parse().unwrap();
let mut group = c.benchmark_group("ipaddr_operations");
group.bench_function("is_ipv4", |b| b.iter(|| black_box(ipv4.is_ipv4())));
group.bench_function("is_ipv6", |b| b.iter(|| black_box(ipv6.is_ipv6())));
group.bench_function("is_loopback_ipv4", |b| {
b.iter(|| black_box(ipv4.is_loopback()))
});
group.bench_function("is_loopback_ipv6", |b| {
b.iter(|| black_box(ipv6.is_loopback()))
});
group.bench_function("is_unspecified", |b| {
b.iter(|| {
black_box(
IpAddr::new(core::net::IpAddr::V4(core::net::Ipv4Addr::new(0, 0, 0, 0)))
.is_unspecified(),
)
})
});
group.bench_function("is_multicast", |b| {
b.iter(|| {
black_box(
IpAddr::new(core::net::IpAddr::V4(core::net::Ipv4Addr::new(
224, 0, 0, 1,
)))
.is_multicast(),
)
})
});
group.finish();
}
fn bench_hostname_parsing(c: &mut Criterion) {
let mut group = c.benchmark_group("hostname_parsing");
group.bench_function("localhost", |b| {
b.iter(|| black_box("localhost".parse::<Hostname>().unwrap()))
});
group.bench_function("simple", |b| {
b.iter(|| black_box("my-server".parse::<Hostname>().unwrap()))
});
group.bench_function("multi_label", |b| {
b.iter(|| black_box("my-server-01".parse::<Hostname>().unwrap()))
});
group.bench_function("long", |b| {
b.iter(|| {
black_box(
"this-is-a-very-long-hostname-with-many-labels.example.com"
.parse::<Hostname>()
.unwrap(),
)
})
});
group.finish();
}
fn bench_hostname_operations(c: &mut Criterion) {
let hostname: Hostname = "my-server".parse().unwrap();
let mut group = c.benchmark_group("hostname_operations");
group.bench_function("is_localhost", |b| {
b.iter(|| black_box(hostname.is_localhost()))
});
group.bench_function("as_str", |b| b.iter(|| black_box(hostname.as_str())));
group.finish();
}
fn bench_domain_parsing(c: &mut Criterion) {
let mut group = c.benchmark_group("domain_parsing");
group.bench_function("simple", |b| {
b.iter(|| black_box("example.com".parse::<DomainName>().unwrap()))
});
group.bench_function("subdomain", |b| {
b.iter(|| black_box("sub.example.com".parse::<DomainName>().unwrap()))
});
group.bench_function("long", |b| {
b.iter(|| {
black_box(
"this.is.a.very.long.domain.name.example.com"
.parse::<DomainName>()
.unwrap(),
)
})
});
group.finish();
}
fn bench_domain_operations(c: &mut Criterion) {
let domain: DomainName = "sub.example.com".parse().unwrap();
let mut group = c.benchmark_group("domain_operations");
group.bench_function("depth", |b| b.iter(|| black_box(domain.depth())));
group.bench_function("as_str", |b| b.iter(|| black_box(domain.as_str())));
group.finish();
}
fn bench_port_operations(c: &mut Criterion) {
let http: Port = Port::HTTP;
let custom: Port = 8080.try_into().unwrap();
let mut group = c.benchmark_group("port_operations");
group.bench_function("http_const", |b| b.iter(|| black_box(http.as_u16())));
group.bench_function("custom", |b| b.iter(|| black_box(custom.as_u16())));
group.bench_function("is_system_port", |b| {
b.iter(|| black_box(http.is_system_port()))
});
group.bench_function("is_registered_port", |b| {
b.iter(|| black_box(custom.is_registered_port()))
});
group.finish();
}
fn bench_socketaddr_parsing(c: &mut Criterion) {
let mut group = c.benchmark_group("socketaddr_parsing");
group.bench_function("ipv4", |b| {
b.iter(|| black_box("192.168.1.1:8080".parse::<SocketAddr>().unwrap()))
});
group.bench_function("domain", |b| {
b.iter(|| black_box("example.com:443".parse::<SocketAddr>().unwrap()))
});
group.bench_function("ipv6", |b| {
b.iter(|| black_box("[::1]:8080".parse::<SocketAddr>().unwrap()))
});
group.finish();
}
fn bench_socketaddr_operations(c: &mut Criterion) {
let socket: SocketAddr = "192.168.1.1:8080".parse().unwrap();
let mut group = c.benchmark_group("socketaddr_operations");
group.bench_function("port", |b| b.iter(|| black_box(socket.as_port())));
group.bench_function("host", |b| b.iter(|| black_box(socket.as_host())));
group.bench_function("is_ip", |b| b.iter(|| black_box(socket.is_ip())));
group.finish();
}
fn bench_cidr_parsing(c: &mut Criterion) {
let mut group = c.benchmark_group("cidr_parsing");
group.bench_function("ipv4_24", |b| {
b.iter(|| black_box("192.168.1.0/24".parse::<Cidr>().unwrap()))
});
group.bench_function("ipv4_16", |b| {
b.iter(|| black_box("192.168.0.0/16".parse::<Cidr>().unwrap()))
});
group.bench_function("ipv6_64", |b| {
b.iter(|| black_box("2001:db8::/64".parse::<Cidr>().unwrap()))
});
group.finish();
}
fn bench_cidr_operations(c: &mut Criterion) {
let cidr: Cidr = "192.168.1.0/24".parse().unwrap();
let ip: IpAddr = "192.168.1.100".parse().unwrap();
let mut group = c.benchmark_group("cidr_operations");
group.bench_function("prefix_length", |b| {
b.iter(|| black_box(cidr.prefix_length()))
});
group.bench_function("network_address", |b| {
b.iter(|| black_box(cidr.network_address()))
});
group.bench_function("contains", |b| b.iter(|| black_box(cidr.contains(&ip))));
group.finish();
}
fn bench_ipv4range_parsing(c: &mut Criterion) {
let mut group = c.benchmark_group("ipv4range_parsing");
group.bench_function("private_c_class", |b| {
b.iter(|| black_box("192.168.1.0-192.168.1.255".parse::<Ipv4Range>().unwrap()))
});
group.bench_function("private_b_class", |b| {
b.iter(|| black_box("172.16.0.0-172.16.255.255".parse::<Ipv4Range>().unwrap()))
});
group.bench_function("single", |b| {
b.iter(|| black_box("192.168.1.100-192.168.1.100".parse::<Ipv4Range>().unwrap()))
});
group.finish();
}
fn bench_ipv4range_operations(c: &mut Criterion) {
let range: Ipv4Range = "192.168.1.0-192.168.1.255".parse().unwrap();
let ip = core::net::Ipv4Addr::new(192, 168, 1, 100);
let mut group = c.benchmark_group("ipv4range_operations");
group.bench_function("contains", |b| b.iter(|| black_box(range.contains(&ip))));
group.bench_function("num_addresses", |b| {
b.iter(|| black_box(range.num_addresses()))
});
group.finish();
}
fn bench_ipv6range_parsing(c: &mut Criterion) {
let mut group = c.benchmark_group("ipv6range_parsing");
group.bench_function("small", |b| {
b.iter(|| black_box("2001:db8::1-2001:db8::ffff".parse::<Ipv6Range>().unwrap()))
});
group.bench_function("single", |b| {
b.iter(|| black_box("2001:db8::1-2001:db8::1".parse::<Ipv6Range>().unwrap()))
});
group.finish();
}
fn bench_ipv6range_operations(c: &mut Criterion) {
let range: Ipv6Range = "2001:db8::1-2001:db8::ffff".parse().unwrap();
let ip = core::net::Ipv6Addr::new(0x2001, 0x0db8, 0, 0, 0, 0, 0, 0x100);
let mut group = c.benchmark_group("ipv6range_operations");
group.bench_function("contains", |b| b.iter(|| black_box(range.contains(&ip))));
group.bench_function("num_addresses", |b| {
b.iter(|| black_box(range.num_addresses()))
});
group.finish();
}
fn bench_url_parsing(c: &mut Criterion) {
let mut group = c.benchmark_group("url_parsing");
group.bench_function("simple", |b| {
b.iter(|| black_box("https://example.com".parse::<Url>().unwrap()))
});
group.bench_function("with_path", |b| {
b.iter(|| {
black_box(
"https://example.com/path/to/resource"
.parse::<Url>()
.unwrap(),
)
})
});
group.bench_function("with_query", |b| {
b.iter(|| black_box("https://example.com/path?key=value".parse::<Url>().unwrap()))
});
group.bench_function("full", |b| {
b.iter(|| {
black_box(
"https://example.com:443/path?key=value#section"
.parse::<Url>()
.unwrap(),
)
})
});
group.finish();
}
fn bench_url_operations(c: &mut Criterion) {
let url: Url = "https://example.com:8080/path?key=value#section"
.parse()
.unwrap();
let mut group = c.benchmark_group("url_operations");
group.bench_function("scheme", |b| b.iter(|| black_box(url.scheme())));
group.bench_function("host", |b| b.iter(|| black_box(url.host())));
group.bench_function("port", |b| b.iter(|| black_box(url.port())));
group.finish();
}
fn bench_uuid_parsing(c: &mut Criterion) {
let mut group = c.benchmark_group("uuid_parsing");
group.bench_function("standard", |b| {
b.iter(|| {
black_box(
"550e8400-e29b-41d4-a716-446655440000"
.parse::<Uuid>()
.unwrap(),
)
})
});
group.bench_function("uppercase", |b| {
b.iter(|| {
black_box(
"550E8400-E29B-41D4-A716-446655440000"
.parse::<Uuid>()
.unwrap(),
)
})
});
group.bench_function("no_hyphens", |b| {
b.iter(|| {
black_box(
"550e8400-e29b-41d4-a716-446655440000"
.parse::<Uuid>()
.unwrap(),
)
})
});
group.finish();
}
fn bench_uuid_operations(c: &mut Criterion) {
let uuid: Uuid = "550e8400-e29b-41d4-a716-446655440000".parse().unwrap();
let mut group = c.benchmark_group("uuid_operations");
group.bench_function("as_str", |b| b.iter(|| black_box(uuid.as_str())));
group.bench_function("is_nil", |b| b.iter(|| black_box(uuid.is_nil())));
group.bench_function("version", |b| b.iter(|| black_box(uuid.version())));
group.finish();
}
fn bench_email_parsing(c: &mut Criterion) {
let mut group = c.benchmark_group("email_parsing");
group.bench_function("simple", |b| {
b.iter(|| black_box("user@example.com".parse::<Email>().unwrap()))
});
group.bench_function("with_subdomain", |b| {
b.iter(|| black_box("user@mail.example.com".parse::<Email>().unwrap()))
});
group.bench_function("with_plus", |b| {
b.iter(|| black_box("user+tag@example.com".parse::<Email>().unwrap()))
});
group.finish();
}
fn bench_email_operations(c: &mut Criterion) {
let email: Email = "user@example.com".parse().unwrap();
let mut group = c.benchmark_group("email_operations");
group.bench_function("local_part", |b| b.iter(|| black_box(email.local_part())));
group.bench_function("domain_part", |b| b.iter(|| black_box(email.domain_part())));
group.finish();
}
fn bench_macaddress_parsing(c: &mut Criterion) {
let mut group = c.benchmark_group("macaddress_parsing");
group.bench_function("colon", |b| {
b.iter(|| black_box("00:1a:2b:3c:4d:5e".parse::<MacAddress>().unwrap()))
});
group.bench_function("dash", |b| {
b.iter(|| black_box("00-1a-2b-3c-4d-5e".parse::<MacAddress>().unwrap()))
});
group.bench_function("plain", |b| {
b.iter(|| black_box("00:1a:2b:3c:4d:5f".parse::<MacAddress>().unwrap()))
});
group.finish();
}
fn bench_macaddress_operations(c: &mut Criterion) {
let mac: MacAddress = "00:1a:2b:3c:4d:5e".parse().unwrap();
let mut group = c.benchmark_group("macaddress_operations");
group.bench_function("as_str", |b| b.iter(|| black_box(mac.as_str())));
group.bench_function("is_unicast", |b| b.iter(|| black_box(mac.is_unicast())));
group.bench_function("is_multicast", |b| b.iter(|| black_box(mac.is_multicast())));
group.finish();
}
fn bench_host_parsing(c: &mut Criterion) {
let mut group = c.benchmark_group("host_parsing");
group.bench_function("ip", |b| {
b.iter(|| black_box("192.168.1.1".parse::<Host>().unwrap()))
});
group.bench_function("domain", |b| {
b.iter(|| black_box("example.com".parse::<Host>().unwrap()))
});
group.bench_function("hostname", |b| {
b.iter(|| black_box("my-server".parse::<Host>().unwrap()))
});
group.finish();
}
fn bench_host_operations(c: &mut Criterion) {
let ip_host: Host = "192.168.1.1".parse().unwrap();
let domain_host: Host = "example.com".parse().unwrap();
let mut group = c.benchmark_group("host_operations");
group.bench_function("is_ipaddr", |b| b.iter(|| black_box(ip_host.is_ipaddr())));
group.bench_function("is_domainname", |b| {
b.iter(|| black_box(domain_host.is_domainname()))
});
group.bench_function("is_localhost", |b| {
b.iter(|| black_box(ip_host.is_localhost()))
});
group.finish();
}
fn bench_os_version_parsing(c: &mut Criterion) {
let mut group = c.benchmark_group("os_version_parsing");
group.bench_function("simple", |b| {
b.iter(|| black_box("22.04".parse::<OsVersion>().unwrap()))
});
group.bench_function("with_patch", |b| {
b.iter(|| black_box("22.04.3".parse::<OsVersion>().unwrap()))
});
group.bench_function("ubuntu", |b| {
b.iter(|| black_box("22.04.5".parse::<OsVersion>().unwrap()))
});
group.finish();
}
fn bench_os_version_operations(c: &mut Criterion) {
let os_ver: OsVersion = "22.04.3".parse().unwrap();
let mut group = c.benchmark_group("os_version_operations");
group.bench_function("major", |b| b.iter(|| black_box(os_ver.major())));
group.bench_function("minor", |b| b.iter(|| black_box(os_ver.minor())));
group.bench_function("patch", |b| b.iter(|| black_box(os_ver.patch())));
group.bench_function("is_major_release", |b| {
b.iter(|| black_box(os_ver.is_major_release()))
});
group.finish();
}
fn bench_kernel_version_parsing(c: &mut Criterion) {
let mut group = c.benchmark_group("kernel_version_parsing");
group.bench_function("simple", |b| {
b.iter(|| black_box("6.8.0".parse::<KernelVersion>().unwrap()))
});
group.bench_function("with_release", |b| {
b.iter(|| black_box("6.8.0-40-generic".parse::<KernelVersion>().unwrap()))
});
group.bench_function("debian", |b| {
b.iter(|| black_box("6.1.0-26-amd64".parse::<KernelVersion>().unwrap()))
});
group.finish();
}
fn bench_kernel_version_operations(c: &mut Criterion) {
let kernel: KernelVersion = "6.8.0-40-generic".parse().unwrap();
let mut group = c.benchmark_group("kernel_version_operations");
group.bench_function("major", |b| b.iter(|| black_box(kernel.major())));
group.bench_function("minor", |b| b.iter(|| black_box(kernel.minor())));
group.bench_function("patch", |b| b.iter(|| black_box(kernel.patch())));
group.bench_function("is_lts", |b| b.iter(|| black_box(kernel.is_lts())));
group.finish();
}
fn bench_username_parsing(c: &mut Criterion) {
let mut group = c.benchmark_group("username_parsing");
group.bench_function("simple", |b| {
b.iter(|| black_box("root".parse::<Username>().unwrap()))
});
group.bench_function("system", |b| {
b.iter(|| black_box("www-data".parse::<Username>().unwrap()))
});
group.bench_function("service", |b| {
b.iter(|| black_box("svc-nginx".parse::<Username>().unwrap()))
});
group.finish();
}
fn bench_username_operations(c: &mut Criterion) {
let username: Username = "www-data".parse().unwrap();
let mut group = c.benchmark_group("username_operations");
group.bench_function("as_str", |b| b.iter(|| black_box(username.as_str())));
group.bench_function("is_system_user", |b| {
b.iter(|| black_box(username.is_system_user()))
});
group.bench_function("is_service_account", |b| {
b.iter(|| black_box(username.is_service_account()))
});
group.finish();
}
fn bench_arch_operations(c: &mut Criterion) {
let mut group = c.benchmark_group("arch_operations");
group.bench_function("is_64_bit", |b| {
b.iter(|| black_box(Arch::current().is_64_bit()))
});
group.bench_function("is_x86", |b| b.iter(|| black_box(Arch::current().is_x86())));
group.bench_function("is_arm", |b| b.iter(|| black_box(Arch::current().is_arm())));
group.bench_function("pointer_width", |b| {
b.iter(|| black_box(Arch::current().pointer_width()))
});
group.finish();
}
fn bench_os_type_operations(c: &mut Criterion) {
let mut group = c.benchmark_group("os_type_operations");
group.bench_function("is_unix", |b| {
b.iter(|| black_box(OsType::current().is_unix()))
});
group.bench_function("is_windows", |b| {
b.iter(|| black_box(OsType::current().is_windows()))
});
group.bench_function("family", |b| {
b.iter(|| black_box(OsType::current().family()))
});
group.finish();
}
fn bench_distro_parsing(c: &mut Criterion) {
let mut group = c.benchmark_group("distro_parsing");
group.bench_function("ubuntu", |b| {
b.iter(|| black_box("Ubuntu".parse::<Distro>().unwrap()))
});
group.bench_function("debian", |b| {
b.iter(|| black_box("Debian".parse::<Distro>().unwrap()))
});
group.bench_function("fedora", |b| {
b.iter(|| black_box("Fedora".parse::<Distro>().unwrap()))
});
group.finish();
}
fn bench_distro_operations(c: &mut Criterion) {
let distro: Distro = "Ubuntu".parse().unwrap();
let mut group = c.benchmark_group("distro_operations");
group.bench_function("is_debian_based", |b| {
b.iter(|| black_box(distro.is_debian_based()))
});
group.bench_function("is_redhat_based", |b| {
b.iter(|| black_box(distro.is_redhat_based()))
});
group.bench_function("is_lts", |b| b.iter(|| black_box(distro.is_lts())));
group.finish();
}
fn bench_pid_operations(c: &mut Criterion) {
let pid: Pid = 1234.try_into().unwrap();
let mut group = c.benchmark_group("pid_operations");
group.bench_function("new_valid", |b| {
b.iter(|| black_box(Pid::new(1234).unwrap()))
});
group.bench_function("as_u32", |b| b.iter(|| black_box(pid.as_u32())));
group.bench_function("is_init", |b| {
b.iter(|| black_box(Pid::new(1).unwrap().is_init()))
});
group.finish();
}
fn bench_fd_operations(c: &mut Criterion) {
let _fd: Fd = 5.try_into().unwrap();
let mut group = c.benchmark_group("fd_operations");
group.bench_function("new_valid", |b| b.iter(|| black_box(Fd::new(5).unwrap())));
group.bench_function("is_stdin", |b| {
let stdin = Fd::STDIN;
b.iter(|| black_box(stdin.is_stdin()))
});
group.bench_function("is_stdout", |b| {
let stdout = Fd::STDOUT;
b.iter(|| black_box(stdout.is_stdout()))
});
group.bench_function("is_stderr", |b| {
let stderr = Fd::STDERR;
b.iter(|| black_box(stderr.is_stderr()))
});
group.bench_function("is_standard", |b| {
let stdin = Fd::STDIN;
b.iter(|| black_box(stdin.is_standard()))
});
group.finish();
}
fn bench_version_comparison(c: &mut Criterion) {
let v1: OsVersion = "22.04".parse().unwrap();
let v2: OsVersion = "22.04.1".parse().unwrap();
let mut group = c.benchmark_group("version_comparison");
group.bench_function("lt", |b| b.iter(|| black_box(v1 < v2)));
group.bench_function("eq", |b| b.iter(|| black_box(v1 == v2)));
group.bench_function("cmp", |b| b.iter(|| black_box(v1.cmp(&v2))));
group.bench_function("le", |b| b.iter(|| black_box(v1 <= v2)));
group.bench_function("gt", |b| b.iter(|| black_box(v1 > v2)));
group.finish();
}
fn bench_compile_time_detection(c: &mut Criterion) {
let mut group = c.benchmark_group("compile_time_detection");
group.bench_function("arch_current", |b| b.iter(|| black_box(Arch::current())));
group.bench_function("os_current", |b| b.iter(|| black_box(OsType::current())));
group.finish();
}
criterion_group!(
benches,
bench_ipaddr_parsing,
bench_ipaddr_operations,
bench_hostname_parsing,
bench_hostname_operations,
bench_domain_parsing,
bench_domain_operations,
bench_port_operations,
bench_socketaddr_parsing,
bench_socketaddr_operations,
bench_cidr_parsing,
bench_cidr_operations,
bench_ipv4range_parsing,
bench_ipv4range_operations,
bench_ipv6range_parsing,
bench_ipv6range_operations,
bench_url_parsing,
bench_url_operations,
bench_uuid_parsing,
bench_uuid_operations,
bench_email_parsing,
bench_email_operations,
bench_macaddress_parsing,
bench_macaddress_operations,
bench_host_parsing,
bench_host_operations,
bench_os_version_parsing,
bench_os_version_operations,
bench_kernel_version_parsing,
bench_kernel_version_operations,
bench_username_parsing,
bench_username_operations,
bench_arch_operations,
bench_os_type_operations,
bench_distro_parsing,
bench_distro_operations,
bench_pid_operations,
bench_fd_operations,
bench_version_comparison,
bench_compile_time_detection,
);
criterion_main!(benches);