Skip to content

Commit ecc7f14

Browse files
committed
move the factor divan bench into the actual directory
1 parent 6580919 commit ecc7f14

File tree

6 files changed

+58
-100
lines changed

6 files changed

+58
-100
lines changed

Cargo.lock

Lines changed: 1 addition & 17 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,6 @@ members = [
280280
"src/uu/stdbuf/src/libstdbuf",
281281
"src/uucore",
282282
"src/uucore_procs",
283-
"tests/benches/factor",
284283
"tests/uutests",
285284
# "fuzz", # TODO
286285
]

src/uu/factor/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ path = "src/main.rs"
3131

3232
[dev-dependencies]
3333
divan = { workspace = true }
34+
rand = { workspace = true }
3435
uucore = { workspace = true, features = ["benchmark"] }
3536

3637
[lib]

src/uu/factor/benches/factor_bench.rs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
// For the full copyright and license information, please view the LICENSE
44
// file that was distributed with this source code.
55

6+
// spell-checker:ignore funcs
7+
68
use divan::{Bencher, black_box};
79
use uu_factor::uumain;
810
use uucore::benchmark::run_util_function;
@@ -55,6 +57,60 @@ fn factor_multiple_big_uint(bencher: Bencher) {
5557
}
5658
*/
5759

60+
#[divan::bench()]
61+
fn factor_table(bencher: Bencher) {
62+
#[cfg(target_os = "linux")]
63+
check_personality();
64+
65+
const INPUT_SIZE: usize = 128;
66+
67+
let inputs = {
68+
// Deterministic RNG; use an explicitly-named RNG to guarantee stability
69+
use rand::{RngCore, SeedableRng};
70+
const SEED: u64 = 0xdead_bebe_ea75_cafe; // spell-checker:disable-line
71+
let mut rng = rand::rngs::StdRng::seed_from_u64(SEED);
72+
73+
std::iter::repeat_with(move || {
74+
let mut array = [0u64; INPUT_SIZE];
75+
for item in &mut array {
76+
*item = rng.next_u64();
77+
}
78+
array
79+
})
80+
.take(10)
81+
.collect::<Vec<_>>()
82+
};
83+
84+
bencher.bench(|| {
85+
for a in &inputs {
86+
for n in a {
87+
divan::black_box(num_prime::nt_funcs::factors(*n, None));
88+
}
89+
}
90+
});
91+
}
92+
93+
#[cfg(target_os = "linux")]
94+
fn check_personality() {
95+
use std::fs;
96+
const ADDR_NO_RANDOMIZE: u64 = 0x0040000;
97+
const PERSONALITY_PATH: &str = "/proc/self/personality";
98+
99+
let p_string = fs::read_to_string(PERSONALITY_PATH)
100+
.unwrap_or_else(|_| panic!("Couldn't read '{PERSONALITY_PATH}'"))
101+
.strip_suffix('\n')
102+
.unwrap()
103+
.to_owned();
104+
105+
let personality = u64::from_str_radix(&p_string, 16)
106+
.unwrap_or_else(|_| panic!("Expected a hex value for personality, got '{p_string:?}'"));
107+
if personality & ADDR_NO_RANDOMIZE == 0 {
108+
eprintln!(
109+
"WARNING: Benchmarking with ASLR enabled (personality is {personality:x}), results might not be reproducible."
110+
);
111+
}
112+
}
113+
58114
fn main() {
59115
divan::main();
60116
}

tests/benches/factor/Cargo.toml

Lines changed: 0 additions & 20 deletions
This file was deleted.

tests/benches/factor/benches/table.rs

Lines changed: 0 additions & 62 deletions
This file was deleted.

0 commit comments

Comments
 (0)