Merge pull request #9296 from sylvestre/divan

move the factor divan bench into the actual directory
This commit is contained in:
Daniel Hofstetter 2025-11-16 14:05:00 +01:00 committed by GitHub
commit 63ee6486d0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 58 additions and 100 deletions

18
Cargo.lock generated
View file

@ -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]]

View file

@ -280,7 +280,6 @@ members = [
"src/uu/stdbuf/src/libstdbuf",
"src/uucore",
"src/uucore_procs",
"tests/benches/factor",
"tests/uutests",
# "fuzz", # TODO
]

View file

@ -31,6 +31,7 @@ path = "src/main.rs"
[dev-dependencies]
divan = { workspace = true }
rand = { workspace = true }
uucore = { workspace = true, features = ["benchmark"] }
[lib]

View file

@ -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::<Vec<_>>()
};
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();
}

View file

@ -1,20 +0,0 @@
[package]
name = "uu_factor_benches"
version = "0.0.0"
authors = ["nicoo <nicoo@debian.org>"]
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

View file

@ -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::<Vec<_>>()
};
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."
);
}
}