| 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 `numfmt`, with the numbers as arguments, and on stdin. |
| 7 |
|
| 8 |
use divan::{Bencher, black_box}; |
| 9 |
use uu_numfmt::uumain; |
| 10 |
use uucore::benchmark::run_util_function; |
| 11 |
|
| 12 |
/// Benchmark SI formatting by passing numbers as command-line arguments |
| 13 |
#[divan::bench(args = [10_000])] |
| 14 |
fn numfmt_to_si(bencher: Bencher, count: usize) { |
| 15 |
bencher |
| 16 |
.with_inputs(|| { |
| 17 |
let numbers: Vec<String> = (1..=count).map(|n| n.to_string()).collect(); |
| 18 |
let mut args: Vec<String> = vec!["--to=si".to_string()]; |
| 19 |
args.extend(numbers); |
| 20 |
args |
| 21 |
}) |
| 22 |
.bench_values(|args| { |
| 23 |
let arg_refs: Vec<&str> = args.iter().map(String::as_str).collect(); |
| 24 |
black_box(run_util_function(uumain, &arg_refs)); |
| 25 |
}); |
| 26 |
} |
| 27 |
|
| 28 |
/// Benchmark SI formatting with precision format |
| 29 |
#[divan::bench(args = [10_000])] |
| 30 |
fn numfmt_to_si_precision(bencher: Bencher, count: usize) { |
| 31 |
bencher |
| 32 |
.with_inputs(|| { |
| 33 |
let numbers: Vec<String> = (1..=count).map(|n| n.to_string()).collect(); |
| 34 |
let mut args: Vec<String> = vec!["--to=si".to_string(), "--format=%.6f".to_string()]; |
| 35 |
args.extend(numbers); |
| 36 |
args |
| 37 |
}) |
| 38 |
.bench_values(|args| { |
| 39 |
let arg_refs: Vec<&str> = args.iter().map(String::as_str).collect(); |
| 40 |
black_box(run_util_function(uumain, &arg_refs)); |
| 41 |
}); |
| 42 |
} |
| 43 |
|
| 44 |
/// Benchmark IEC (binary) formatting |
| 45 |
#[divan::bench(args = [10_000])] |
| 46 |
fn numfmt_to_iec(bencher: Bencher, count: usize) { |
| 47 |
bencher |
| 48 |
.with_inputs(|| { |
| 49 |
let numbers: Vec<String> = (1..=count).map(|n| n.to_string()).collect(); |
| 50 |
let mut args: Vec<String> = vec!["--to=iec".to_string()]; |
| 51 |
args.extend(numbers); |
| 52 |
args |
| 53 |
}) |
| 54 |
.bench_values(|args| { |
| 55 |
let arg_refs: Vec<&str> = args.iter().map(String::as_str).collect(); |
| 56 |
black_box(run_util_function(uumain, &arg_refs)); |
| 57 |
}); |
| 58 |
} |
| 59 |
|
| 60 |
/// Benchmark parsing from SI format back to raw numbers |
| 61 |
#[divan::bench(args = [10_000])] |
| 62 |
fn numfmt_from_si(bencher: Bencher, count: usize) { |
| 63 |
bencher |
| 64 |
.with_inputs(|| { |
| 65 |
// Generate SI formatted data (e.g., "1K", "2K", etc.) |
| 66 |
let numbers: Vec<String> = (1..=count).map(|n| format!("{n}K")).collect(); |
| 67 |
let mut args: Vec<String> = vec!["--from=si".to_string()]; |
| 68 |
args.extend(numbers); |
| 69 |
args |
| 70 |
}) |
| 71 |
.bench_values(|args| { |
| 72 |
let arg_refs: Vec<&str> = args.iter().map(String::as_str).collect(); |
| 73 |
black_box(run_util_function(uumain, &arg_refs)); |
| 74 |
}); |
| 75 |
} |
| 76 |
|
| 77 |
/// Benchmark large numbers with SI formatting |
| 78 |
#[divan::bench(args = [10_000])] |
| 79 |
fn numfmt_large_numbers_si(bencher: Bencher, count: usize) { |
| 80 |
bencher |
| 81 |
.with_inputs(|| { |
| 82 |
// Generate numbers that all produce uniform SI output lengths (all in 1-9M range) |
| 83 |
// This avoids variance from variable output string lengths |
| 84 |
let numbers: Vec<String> = (1..=count) |
| 85 |
.map(|n| ((n % 9) + 1) * 1_000_000) |
| 86 |
.map(|n| n.to_string()) |
| 87 |
.collect(); |
| 88 |
let mut args: Vec<String> = vec!["--to=si".to_string()]; |
| 89 |
args.extend(numbers); |
| 90 |
args |
| 91 |
}) |
| 92 |
.bench_values(|args| { |
| 93 |
let arg_refs: Vec<&str> = args.iter().map(String::as_str).collect(); |
| 94 |
black_box(run_util_function(uumain, &arg_refs)); |
| 95 |
}); |
| 96 |
} |
| 97 |
|
| 98 |
/// Benchmark different padding widths |
| 99 |
#[divan::bench(args = [(10_000, 50)])] |
| 100 |
fn numfmt_padding(bencher: Bencher, (count, padding): (usize, usize)) { |
| 101 |
bencher |
| 102 |
.with_inputs(|| { |
| 103 |
let numbers: Vec<String> = (1..=count).map(|n| n.to_string()).collect(); |
| 104 |
let mut args: Vec<String> = vec!["--to=si".to_string(), format!("--padding={padding}")]; |
| 105 |
args.extend(numbers); |
| 106 |
args |
| 107 |
}) |
| 108 |
.bench_values(|args| { |
| 109 |
let arg_refs: Vec<&str> = args.iter().map(String::as_str).collect(); |
| 110 |
black_box(run_util_function(uumain, &arg_refs)); |
| 111 |
}); |
| 112 |
} |
| 113 |
|
| 114 |
/// Benchmark round modes with SI formatting |
| 115 |
#[divan::bench(args = [("up", 10_000), ("down", 10_000), ("towards-zero", 10_000)])] |
| 116 |
fn numfmt_round_modes(bencher: Bencher, (round_mode, count): (&str, usize)) { |
| 117 |
bencher |
| 118 |
.with_inputs(|| { |
| 119 |
let numbers: Vec<String> = (1..=count).map(|n| n.to_string()).collect(); |
| 120 |
let mut args: Vec<String> = |
| 121 |
vec!["--to=si".to_string(), format!("--round={round_mode}")]; |
| 122 |
args.extend(numbers); |
| 123 |
args |
| 124 |
}) |
| 125 |
.bench_values(|args| { |
| 126 |
let arg_refs: Vec<&str> = args.iter().map(String::as_str).collect(); |
| 127 |
black_box(run_util_function(uumain, &arg_refs)); |
| 128 |
}); |
| 129 |
} |
| 130 |
|
| 131 |
/// Run `uumain` with `args`, `data` on stdin and the output thrown away. |
| 132 |
#[cfg(unix)] |
| 133 |
fn bench_with_stdin(bencher: Bencher, data: &[u8], args: &[&str]) { |
| 134 |
use rustix::stdio::{dup2_stdin, dup2_stdout}; |
| 135 |
|
| 136 |
let file = std::fs::File::open(uucore::benchmark::setup_test_file(data)).unwrap(); |
| 137 |
let devnull = std::fs::OpenOptions::new() |
| 138 |
.write(true) |
| 139 |
.open("/dev/null") |
| 140 |
.unwrap(); |
| 141 |
let stdin_bak = rustix::io::dup(rustix::stdio::stdin()).unwrap(); |
| 142 |
let stdout_bak = rustix::io::dup(rustix::stdio::stdout()).unwrap(); |
| 143 |
|
| 144 |
bencher.bench_local(|| { |
| 145 |
rustix::fs::seek(&file, rustix::fs::SeekFrom::Start(0)).unwrap(); |
| 146 |
dup2_stdin(&file).unwrap(); |
| 147 |
dup2_stdout(&devnull).unwrap(); |
| 148 |
black_box(run_util_function(uumain, args)); |
| 149 |
dup2_stdin(&stdin_bak).unwrap(); |
| 150 |
dup2_stdout(&stdout_bak).unwrap(); |
| 151 |
}); |
| 152 |
} |
| 153 |
|
| 154 |
/// Benchmark SI formatting with the numbers on stdin |
| 155 |
#[cfg(unix)] |
| 156 |
#[divan::bench] |
| 157 |
fn numfmt_stream_to_si(bencher: Bencher) { |
| 158 |
let data: Vec<u8> = (1..=100_000u64) |
| 159 |
.flat_map(|n| format!("{}\n", n * 7919).into_bytes()) |
| 160 |
.collect(); |
| 161 |
bench_with_stdin(bencher, &data, &["--to=si"]); |
| 162 |
} |
| 163 |
|
| 164 |
/// The same, with a precision: formats through the exact float path |
| 165 |
#[cfg(unix)] |
| 166 |
#[divan::bench] |
| 167 |
fn numfmt_stream_to_si_precision(bencher: Bencher) { |
| 168 |
let data: Vec<u8> = (1..=100_000u64) |
| 169 |
.flat_map(|n| format!("{}\n", n * 7919).into_bytes()) |
| 170 |
.collect(); |
| 171 |
bench_with_stdin(bencher, &data, &["--to=si", "--format=%.2f"]); |
| 172 |
} |
| 173 |
|
| 174 |
fn main() { |
| 175 |
divan::main(); |
| 176 |
} |