From 69637c921297a49c3a80874cb2f5dd37f7a7ddd6 Mon Sep 17 00:00:00 2001 From: okaneco <47607823+okaneco@users.noreply.github.com> Date: Wed, 21 Feb 2024 20:54:00 -0500 Subject: [PATCH] Add benches for `net` parsing Add benches for IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4, and SocketAddrV6 parsing --- library/core/benches/lib.rs | 1 + library/core/benches/net/addr_parser.rs | 78 +++++++++++++++++++++++++ library/core/benches/net/mod.rs | 1 + 3 files changed, 80 insertions(+) create mode 100644 library/core/benches/net/addr_parser.rs create mode 100644 library/core/benches/net/mod.rs diff --git a/library/core/benches/lib.rs b/library/core/benches/lib.rs index fdefc9a714e..4d14b930e41 100644 --- a/library/core/benches/lib.rs +++ b/library/core/benches/lib.rs @@ -16,6 +16,7 @@ mod fmt; mod hash; mod iter; +mod net; mod num; mod ops; mod pattern; diff --git a/library/core/benches/net/addr_parser.rs b/library/core/benches/net/addr_parser.rs new file mode 100644 index 00000000000..b9406a9779d --- /dev/null +++ b/library/core/benches/net/addr_parser.rs @@ -0,0 +1,78 @@ +use core::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4, SocketAddrV6}; +use core::str::FromStr; + +use test::{black_box, Bencher}; + +const IPV4_STR: &str = "192.168.0.1"; +const IPV4_STR_PORT: &str = "192.168.0.1:8080"; + +const IPV6_STR_FULL: &str = "2001:db8:0:0:0:0:c0a8:1"; +const IPV6_STR_COMPRESS: &str = "2001:db8::c0a8:1"; +const IPV6_STR_V4: &str = "2001:db8::192.168.0.1"; +const IPV6_STR_PORT: &str = "[2001:db8::c0a8:1]:8080"; +const IPV6_STR_PORT_SCOPE_ID: &str = "[2001:db8::c0a8:1%1337]:8080"; + +#[bench] +fn bench_parse_ipv4(b: &mut Bencher) { + b.iter(|| Ipv4Addr::from_str(black_box(IPV4_STR))); +} + +#[bench] +fn bench_parse_ipv6_full(b: &mut Bencher) { + b.iter(|| Ipv6Addr::from_str(black_box(IPV6_STR_FULL))); +} + +#[bench] +fn bench_parse_ipv6_compress(b: &mut Bencher) { + b.iter(|| Ipv6Addr::from_str(black_box(IPV6_STR_COMPRESS))); +} + +#[bench] +fn bench_parse_ipv6_v4(b: &mut Bencher) { + b.iter(|| Ipv6Addr::from_str(black_box(IPV6_STR_V4))); +} + +#[bench] +fn bench_parse_ipaddr_v4(b: &mut Bencher) { + b.iter(|| IpAddr::from_str(black_box(IPV4_STR))); +} + +#[bench] +fn bench_parse_ipaddr_v6_full(b: &mut Bencher) { + b.iter(|| IpAddr::from_str(black_box(IPV6_STR_FULL))); +} + +#[bench] +fn bench_parse_ipaddr_v6_compress(b: &mut Bencher) { + b.iter(|| IpAddr::from_str(black_box(IPV6_STR_COMPRESS))); +} + +#[bench] +fn bench_parse_ipaddr_v6_v4(b: &mut Bencher) { + b.iter(|| IpAddr::from_str(black_box(IPV6_STR_V4))); +} + +#[bench] +fn bench_parse_socket_v4(b: &mut Bencher) { + b.iter(|| SocketAddrV4::from_str(black_box(IPV4_STR_PORT))); +} + +#[bench] +fn bench_parse_socket_v6(b: &mut Bencher) { + b.iter(|| SocketAddrV6::from_str(black_box(IPV6_STR_PORT))); +} + +#[bench] +fn bench_parse_socket_v6_scope_id(b: &mut Bencher) { + b.iter(|| SocketAddrV6::from_str(black_box(IPV6_STR_PORT_SCOPE_ID))); +} + +#[bench] +fn bench_parse_socketaddr_v4(b: &mut Bencher) { + b.iter(|| SocketAddr::from_str(black_box(IPV4_STR_PORT))); +} + +#[bench] +fn bench_parse_socketaddr_v6(b: &mut Bencher) { + b.iter(|| SocketAddr::from_str(black_box(IPV6_STR_PORT))); +} diff --git a/library/core/benches/net/mod.rs b/library/core/benches/net/mod.rs new file mode 100644 index 00000000000..c29aed46ccd --- /dev/null +++ b/library/core/benches/net/mod.rs @@ -0,0 +1 @@ +mod addr_parser;