From ecc7f14e7e87e2acb195ebbeca92148eee7d2546 Mon Sep 17 00:00:00 2001 From: Sylvestre Ledru Date: Sat, 15 Nov 2025 21:45:22 +0100 Subject: [PATCH] move the factor divan bench into the actual directory --- Cargo.lock | 18 +------- Cargo.toml | 1 - src/uu/factor/Cargo.toml | 1 + src/uu/factor/benches/factor_bench.rs | 56 ++++++++++++++++++++++++ tests/benches/factor/Cargo.toml | 20 --------- tests/benches/factor/benches/table.rs | 62 --------------------------- 6 files changed, 58 insertions(+), 100 deletions(-) delete mode 100644 tests/benches/factor/Cargo.toml delete mode 100644 tests/benches/factor/benches/table.rs diff --git a/Cargo.lock b/Cargo.lock index 406b3be90cb..199569ec9e0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -115,12 +115,6 @@ dependencies = [ "derive_arbitrary", ] -[[package]] -name = "array-init" -version = "2.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3d62b7694a562cdf5a74227903507c56ab2cc8bdd1f781ed5cb4cf9c9f810bfc" - [[package]] name = "arrayref" version = "0.3.9" @@ -3335,18 +3329,8 @@ dependencies = [ "num-bigint", "num-prime", "num-traits", - "uucore", -] - -[[package]] -name = "uu_factor_benches" -version = "0.0.0" -dependencies = [ - "array-init", - "codspeed-divan-compat", - "num-prime", "rand 0.9.2", - "rand_chacha 0.9.0", + "uucore", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 499eb87412c..5d0da6da12f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -280,7 +280,6 @@ members = [ "src/uu/stdbuf/src/libstdbuf", "src/uucore", "src/uucore_procs", - "tests/benches/factor", "tests/uutests", # "fuzz", # TODO ] diff --git a/src/uu/factor/Cargo.toml b/src/uu/factor/Cargo.toml index 15d09f7a01b..ef672bf9308 100644 --- a/src/uu/factor/Cargo.toml +++ b/src/uu/factor/Cargo.toml @@ -31,6 +31,7 @@ path = "src/main.rs" [dev-dependencies] divan = { workspace = true } +rand = { workspace = true } uucore = { workspace = true, features = ["benchmark"] } [lib] diff --git a/src/uu/factor/benches/factor_bench.rs b/src/uu/factor/benches/factor_bench.rs index 89498e0ae1c..0346f9787c0 100644 --- a/src/uu/factor/benches/factor_bench.rs +++ b/src/uu/factor/benches/factor_bench.rs @@ -3,6 +3,8 @@ // For the full copyright and license information, please view the LICENSE // file that was distributed with this source code. +// spell-checker:ignore funcs + use divan::{Bencher, black_box}; use uu_factor::uumain; use uucore::benchmark::run_util_function; @@ -55,6 +57,60 @@ fn factor_multiple_big_uint(bencher: Bencher) { } */ +#[divan::bench()] +fn factor_table(bencher: Bencher) { + #[cfg(target_os = "linux")] + check_personality(); + + const INPUT_SIZE: usize = 128; + + let inputs = { + // Deterministic RNG; use an explicitly-named RNG to guarantee stability + use rand::{RngCore, SeedableRng}; + const SEED: u64 = 0xdead_bebe_ea75_cafe; // spell-checker:disable-line + let mut rng = rand::rngs::StdRng::seed_from_u64(SEED); + + std::iter::repeat_with(move || { + let mut array = [0u64; INPUT_SIZE]; + for item in &mut array { + *item = rng.next_u64(); + } + array + }) + .take(10) + .collect::>() + }; + + bencher.bench(|| { + for a in &inputs { + for n in a { + divan::black_box(num_prime::nt_funcs::factors(*n, None)); + } + } + }); +} + +#[cfg(target_os = "linux")] +fn check_personality() { + use std::fs; + const ADDR_NO_RANDOMIZE: u64 = 0x0040000; + const PERSONALITY_PATH: &str = "/proc/self/personality"; + + let p_string = fs::read_to_string(PERSONALITY_PATH) + .unwrap_or_else(|_| panic!("Couldn't read '{PERSONALITY_PATH}'")) + .strip_suffix('\n') + .unwrap() + .to_owned(); + + let personality = u64::from_str_radix(&p_string, 16) + .unwrap_or_else(|_| panic!("Expected a hex value for personality, got '{p_string:?}'")); + if personality & ADDR_NO_RANDOMIZE == 0 { + eprintln!( + "WARNING: Benchmarking with ASLR enabled (personality is {personality:x}), results might not be reproducible." + ); + } +} + fn main() { divan::main(); } diff --git a/tests/benches/factor/Cargo.toml b/tests/benches/factor/Cargo.toml deleted file mode 100644 index 02e59925c5b..00000000000 --- a/tests/benches/factor/Cargo.toml +++ /dev/null @@ -1,20 +0,0 @@ -[package] -name = "uu_factor_benches" -version = "0.0.0" -authors = ["nicoo "] -description = "Benchmarks for the uu_factor integer factorization tool" -edition.workspace = true -homepage.workspace = true -license.workspace = true -publish = false - -[dev-dependencies] -array-init = "2.0.0" -divan = { workspace = true } -rand = "0.9.1" -rand_chacha = "0.9.0" -num-prime = "0.4.4" - -[[bench]] -name = "table" -harness = false diff --git a/tests/benches/factor/benches/table.rs b/tests/benches/factor/benches/table.rs deleted file mode 100644 index 4d89282fae8..00000000000 --- a/tests/benches/factor/benches/table.rs +++ /dev/null @@ -1,62 +0,0 @@ -// This file is part of the uutils coreutils package. -// -// For the full copyright and license information, please view the LICENSE -// file that was distributed with this source code. - -// spell-checker:ignore funcs - -use array_init::array_init; -use divan::Bencher; - -fn main() { - divan::main(); -} - -#[divan::bench()] -fn factor_table(bencher: Bencher) { - #[cfg(target_os = "linux")] - check_personality(); - - const INPUT_SIZE: usize = 128; - - let inputs = { - // Deterministic RNG; use an explicitly-named RNG to guarantee stability - use rand::{RngCore, SeedableRng}; - use rand_chacha::ChaCha8Rng; - const SEED: u64 = 0xdead_bebe_ea75_cafe; // spell-checker:disable-line - let mut rng = ChaCha8Rng::seed_from_u64(SEED); - - std::iter::repeat_with(move || array_init::<_, _, INPUT_SIZE>(|_| rng.next_u64())) - .take(10) - .collect::>() - }; - - bencher.bench(|| { - for a in &inputs { - for n in a { - divan::black_box(num_prime::nt_funcs::factors(*n, None)); - } - } - }); -} - -#[cfg(target_os = "linux")] -fn check_personality() { - use std::fs; - const ADDR_NO_RANDOMIZE: u64 = 0x0040000; - const PERSONALITY_PATH: &str = "/proc/self/personality"; - - let p_string = fs::read_to_string(PERSONALITY_PATH) - .unwrap_or_else(|_| panic!("Couldn't read '{PERSONALITY_PATH}'")) - .strip_suffix('\n') - .unwrap() - .to_owned(); - - let personality = u64::from_str_radix(&p_string, 16) - .unwrap_or_else(|_| panic!("Expected a hex value for personality, got '{p_string:?}'")); - if personality & ADDR_NO_RANDOMIZE == 0 { - eprintln!( - "WARNING: Benchmarking with ASLR enabled (personality is {personality:x}), results might not be reproducible." - ); - } -}