diff --git a/src/uu/wc/BENCHMARKING.md b/src/uu/wc/BENCHMARKING.md index ff0c61a3f44..62f852f937e 100644 --- a/src/uu/wc/BENCHMARKING.md +++ b/src/uu/wc/BENCHMARKING.md @@ -80,6 +80,12 @@ candidate as it's fairly large. Use [`hyperfine`](https://github.com/sharkdp/hyperfine) to compare the performance. For example, `hyperfine 'wc somefile' 'uuwc somefile'`. +Run the UTF-8 character-count benchmark with an explicit UTF-8 locale: + +```shell +LC_ALL=C.UTF-8 cargo bench -p uu_wc --bench wc_bench -- wc_chars_utf8 +``` + If you want to get fancy and exhaustive, generate a table: | | moby64.txt | odyssey256.txt | 25Mshortlines | /usr/bin/docker | diff --git a/src/uu/wc/benches/wc_bench.rs b/src/uu/wc/benches/wc_bench.rs index 6abcb5efd4a..bc752bd7694 100644 --- a/src/uu/wc/benches/wc_bench.rs +++ b/src/uu/wc/benches/wc_bench.rs @@ -78,6 +78,17 @@ fn wc_chars_large_line_count(bencher: Bencher, num_lines: usize) { .bench_values(|args| black_box(uumain(args))); } +#[divan::bench(args = [100_000])] +fn wc_chars_utf8(bencher: Bencher, num_lines: usize) { + let temp_dir = tempfile::tempdir().unwrap(); + let data = "hello ä € 💩\n".repeat(num_lines); + let file_path = create_test_file(data.as_bytes(), temp_dir.path()); + + bencher + .with_inputs(|| get_bench_args(&[&"-m", &file_path]).into_iter()) + .bench_values(|args| black_box(uumain(args))); +} + /// Benchmark word counting on large line counts #[divan::bench(args = [100_000])] fn wc_words_large_line_count(bencher: Bencher, num_lines: usize) { @@ -115,5 +126,10 @@ fn wc_lines_extreme_line_lengths(bencher: Bencher, (num_lines, line_len): (usize } fn main() { + // Set the locale before its first use, since character encoding is cached. + // SAFETY: The benchmark runner has not started any threads yet. + unsafe { + std::env::set_var("LC_ALL", "C.UTF-8"); + } divan::main(); }