| 1 |
// This file is part of the uutils coreutils package. |
| 2 |
// |
| 3 |
// For the full copyright and license information, please view the LICENSE |
| 4 |
// file that was distributed with this source code. |
| 5 |
|
| 6 |
//! Benchmarks for hostname utility |
| 7 |
//! |
| 8 |
//! This benchmark tests the performance of hostname with large /etc/hosts files, |
| 9 |
//! specifically targeting the DNS resolution functionality (`-i` flag). |
| 10 |
//! |
| 11 |
//! # Important Note on Large Hosts File Testing |
| 12 |
//! |
| 13 |
//! To properly test with large /etc/hosts files, NSS_WRAPPER library must be used: |
| 14 |
//! |
| 15 |
//! ```bash |
| 16 |
//! LD_PRELOAD=/usr/lib/libnss_wrapper.so NSS_WRAPPER_HOSTS=/tmp/large_hosts \ |
| 17 |
//! cargo bench --package uu_hostname hostname_ip_lookup |
| 18 |
//! ``` |
| 19 |
//! |
| 20 |
//! Without NSS_WRAPPER, the benchmark tests with the system's real /etc/hosts. |
| 21 |
|
| 22 |
#![cfg(any(all(unix, not(any(target_os = "aix", target_os = "redox"))), windows))] |
| 23 |
|
| 24 |
use divan::{Bencher, black_box}; |
| 25 |
use std::io::Write; |
| 26 |
use uu_hostname::uumain; |
| 27 |
use uucore::benchmark::run_util_function; |
| 28 |
|
| 29 |
/// Generate a large hosts file with the specified number of entries |
| 30 |
fn generate_hosts_file(entries: usize) -> Vec<u8> { |
| 31 |
let avg_line_size = 80; |
| 32 |
let mut data = Vec::with_capacity(entries * avg_line_size); |
| 33 |
let mut writer = std::io::BufWriter::new(&mut data); |
| 34 |
|
| 35 |
// Localhost entries |
| 36 |
writeln!(writer, "127.0.0.1 localhost localhost.localdomain").unwrap(); |
| 37 |
writeln!(writer, "::1 localhost localhost.localdomain").unwrap(); |
| 38 |
|
| 39 |
// Generate host entries |
| 40 |
let environments = ["prod", "staging", "dev", "test"]; |
| 41 |
let regions = ["us-east-1", "us-west-2", "eu-west-1", "ap-southeast-1"]; |
| 42 |
let roles = [ |
| 43 |
"web", "db", "cache", "api", "worker", "lb", "monitor", "backup", |
| 44 |
]; |
| 45 |
|
| 46 |
for i in 2..=entries { |
| 47 |
let ip = match i % 5 { |
| 48 |
0 => format!("127.0.0.{}", i % 256), |
| 49 |
1 => format!("10.{}.{}.{}", (i / 256) % 256, (i / 16) % 16, i % 256), |
| 50 |
2 => format!("192.168.{}.{}", (i / 256) % 256, i % 256), |
| 51 |
3 => format!("172.16.{}.{}", (i / 256) % 256, i % 256), |
| 52 |
_ => format!("10.0.{}.{}", (i / 256) % 256, i % 256), |
| 53 |
}; |
| 54 |
|
| 55 |
let role = roles[i % roles.len()]; |
| 56 |
let env = environments[i % environments.len()]; |
| 57 |
let region = regions[i % regions.len()]; |
| 58 |
let fqdn = format!("{role}-{i:03}.{region}.{env}.example.com"); |
| 59 |
let short = format!("{role}-{i:03}"); |
| 60 |
|
| 61 |
writeln!(writer, "{ip:<15} {fqdn} {short}").unwrap(); |
| 62 |
} |
| 63 |
|
| 64 |
writer.flush().unwrap(); |
| 65 |
drop(writer); |
| 66 |
data |
| 67 |
} |
| 68 |
|
| 69 |
/// Benchmark hostname -i with large hosts file |
| 70 |
/// |
| 71 |
/// # Important |
| 72 |
/// Use NSS_WRAPPER to test with generated hosts file: |
| 73 |
/// `LD_PRELOAD=/usr/lib/libnss_wrapper.so NSS_WRAPPER_HOSTS=/path/to/hosts cargo bench` |
| 74 |
#[divan::bench( |
| 75 |
args = [100_000], |
| 76 |
name = "hostname_ip_lookup" |
| 77 |
)] |
| 78 |
fn bench_hostname_ip(bencher: Bencher, entries: usize) { |
| 79 |
let temp_dir = tempfile::tempdir().unwrap(); |
| 80 |
let hosts_file = temp_dir.path().join("hosts"); |
| 81 |
let hosts_data = generate_hosts_file(entries); |
| 82 |
std::fs::write(&hosts_file, &hosts_data).unwrap(); |
| 83 |
|
| 84 |
let nss_wrapper_env = std::env::var("LD_PRELOAD").unwrap_or_default(); |
| 85 |
let has_nss_wrapper = nss_wrapper_env.contains("nss_wrapper"); |
| 86 |
|
| 87 |
if has_nss_wrapper { |
| 88 |
unsafe { |
| 89 |
std::env::set_var("NSS_WRAPPER_HOSTS", &hosts_file); |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
bencher.bench(|| { |
| 94 |
let result = black_box(run_util_function(uumain, &["-i"])); |
| 95 |
assert_eq!(result, 0); |
| 96 |
}); |
| 97 |
|
| 98 |
if has_nss_wrapper { |
| 99 |
unsafe { |
| 100 |
std::env::remove_var("NSS_WRAPPER_HOSTS"); |
| 101 |
} |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
/// Benchmark basic hostname display (baseline) |
| 106 |
#[divan::bench(name = "hostname_basic")] |
| 107 |
fn bench_hostname_basic(bencher: Bencher) { |
| 108 |
bencher.bench(|| { |
| 109 |
let result = black_box(run_util_function(uumain, &[])); |
| 110 |
assert_eq!(result, 0); |
| 111 |
}); |
| 112 |
} |
| 113 |
|
| 114 |
/// Benchmark direct DNS lookup (Linux/macOS path) |
| 115 |
#[cfg(not(any(target_os = "freebsd", target_os = "openbsd")))] |
| 116 |
#[divan::bench( |
| 117 |
args = [100_000], |
| 118 |
name = "socket_addrs_direct" |
| 119 |
)] |
| 120 |
fn bench_socket_addrs_direct(bencher: Bencher, _entries: usize) { |
| 121 |
use std::net::ToSocketAddrs; |
| 122 |
|
| 123 |
let hostname = hostname::get() |
| 124 |
.unwrap_or_default() |
| 125 |
.to_string_lossy() |
| 126 |
.into_owned(); |
| 127 |
let hostname_with_port = format!("{hostname}:1"); |
| 128 |
|
| 129 |
bencher.bench(|| { |
| 130 |
let result: Result<Vec<_>, _> = hostname_with_port.to_socket_addrs().map(Iterator::collect); |
| 131 |
let _ = black_box(result); |
| 132 |
}); |
| 133 |
} |
| 134 |
|
| 135 |
fn main() { |
| 136 |
divan::main(); |
| 137 |
} |