aboutsummaryrefslogtreecommitdiff
path: root/src/crates
diff options
context:
space:
mode:
authorAria Shrimpton <me@aria.rip>2024-02-05 14:03:00 +0000
committerAria Shrimpton <me@aria.rip>2024-02-05 14:03:00 +0000
commit3b7919007fa70ec28dfed911d4cc15e9eea372b8 (patch)
tree2468dafd7bcb33dc740420106dd0dc9c32c8e1ec /src/crates
parent0e4934c578ecfc022ffc72c9bb2253290ce966af (diff)
fixed seed for benchmarks
Diffstat (limited to 'src/crates')
-rw-r--r--src/crates/benchmarker/benches/hashmap.rs7
-rw-r--r--src/crates/benchmarker/benches/linked_list.rs15
-rw-r--r--src/crates/benchmarker/benches/vec.rs9
-rw-r--r--src/crates/benchmarker/src/container.rs34
-rw-r--r--src/crates/benchmarker/src/indexable.rs24
-rw-r--r--src/crates/benchmarker/src/lib.rs2
-rw-r--r--src/crates/benchmarker/src/mapping.rs39
-rw-r--r--src/crates/benchmarker/src/stack.rs16
-rw-r--r--src/crates/candelabra/src/cost/benchmark.rs14
9 files changed, 90 insertions, 70 deletions
diff --git a/src/crates/benchmarker/benches/hashmap.rs b/src/crates/benchmarker/benches/hashmap.rs
index dbd498a..908a5e6 100644
--- a/src/crates/benchmarker/benches/hashmap.rs
+++ b/src/crates/benchmarker/benches/hashmap.rs
@@ -1,7 +1,12 @@
+use rand::{rngs::StdRng, SeedableRng};
+
fn main() {
let ns = [
64, 128, 256, 512, 1024, 2048, 3072, 4096, 5120, 6144, 7168, 8192, 16384, 24576, 32768,
40960, 49152, 57344, 65536,
];
- candelabra_benchmarker::benchmark_mapping::<std::collections::HashMap<usize, usize>, _, _>(&ns);
+ let mut rng = StdRng::seed_from_u64(42);
+ candelabra_benchmarker::benchmark_mapping::<std::collections::HashMap<usize, usize>, _, _>(
+ &mut rng, &ns,
+ );
}
diff --git a/src/crates/benchmarker/benches/linked_list.rs b/src/crates/benchmarker/benches/linked_list.rs
index f2997e4..1c5de1f 100644
--- a/src/crates/benchmarker/benches/linked_list.rs
+++ b/src/crates/benchmarker/benches/linked_list.rs
@@ -1,9 +1,18 @@
+use rand::{rngs::StdRng, SeedableRng};
+
fn main() {
let ns = [
64, 128, 256, 512, 1024, 2048, 3072, 4096, 5120, 6144, 7168, 8192, 16384, 24576, 32768,
40960, 49152, 57344, 65536,
];
- candelabra_benchmarker::benchmark_container::<std::collections::LinkedList<usize>, _>(&ns);
- candelabra_benchmarker::benchmark_indexable::<std::collections::LinkedList<usize>, _>(&ns);
- candelabra_benchmarker::benchmark_stack::<std::collections::LinkedList<usize>, _>(&ns);
+ let mut rng = StdRng::seed_from_u64(42);
+ candelabra_benchmarker::benchmark_container::<std::collections::LinkedList<usize>, _>(
+ &mut rng, &ns,
+ );
+ candelabra_benchmarker::benchmark_indexable::<std::collections::LinkedList<usize>, _>(
+ &mut rng, &ns,
+ );
+ candelabra_benchmarker::benchmark_stack::<std::collections::LinkedList<usize>, _>(
+ &mut rng, &ns,
+ );
}
diff --git a/src/crates/benchmarker/benches/vec.rs b/src/crates/benchmarker/benches/vec.rs
index 0dd4637..f0f4d39 100644
--- a/src/crates/benchmarker/benches/vec.rs
+++ b/src/crates/benchmarker/benches/vec.rs
@@ -1,9 +1,12 @@
+use rand::{rngs::StdRng, SeedableRng};
+
fn main() {
let ns = [
64, 128, 256, 512, 1024, 2048, 3072, 4096, 5120, 6144, 7168, 8192, 16384, 24576, 32768,
40960, 49152, 57344, 65536,
];
- candelabra_benchmarker::benchmark_container::<Vec<usize>, _>(&ns);
- candelabra_benchmarker::benchmark_indexable::<Vec<usize>, _>(&ns);
- candelabra_benchmarker::benchmark_stack::<Vec<usize>, _>(&ns);
+ let mut rng = StdRng::seed_from_u64(42);
+ candelabra_benchmarker::benchmark_container::<Vec<usize>, _>(&mut rng, &ns);
+ candelabra_benchmarker::benchmark_indexable::<Vec<usize>, _>(&mut rng, &ns);
+ candelabra_benchmarker::benchmark_stack::<Vec<usize>, _>(&mut rng, &ns);
}
diff --git a/src/crates/benchmarker/src/container.rs b/src/crates/benchmarker/src/container.rs
index 157ab90..82fa6f2 100644
--- a/src/crates/benchmarker/src/container.rs
+++ b/src/crates/benchmarker/src/container.rs
@@ -1,30 +1,30 @@
use primrose_library::traits::Container;
-use rand::{distributions::Standard, prelude::Distribution, random, thread_rng, Rng};
+use rand::{distributions::Standard, prelude::Distribution, rngs::StdRng, Rng};
use crate::{benchmark_op, print_result};
-pub fn benchmark_container<T, E>(ns: &[usize])
+pub fn benchmark_container<T, E>(rng: &mut StdRng, ns: &[usize])
where
T: Container<E> + Default + Clone,
E: Copy,
Standard: Distribution<E>,
{
for n in ns {
- scenario_populate::<T, E>(*n);
- scenario_contains::<T, E>(*n);
- scenario_remove::<T, E>(*n);
- scenario_clear::<T, E>(*n);
+ scenario_populate::<T, E>(rng, *n);
+ scenario_contains::<T, E>(rng, *n);
+ scenario_remove::<T, E>(rng, *n);
+ scenario_clear::<T, E>(rng, *n);
}
}
-fn scenario_populate<T, E>(n: usize)
+fn scenario_populate<T, E>(rng: &mut StdRng, n: usize)
where
T: Container<E> + Default + Clone,
E: Copy,
Standard: Distribution<E>,
{
let mut results = benchmark_op(
- || (T::default(), (0..n).map(|_| random()).collect::<Vec<E>>()),
+ || (T::default(), (0..n).map(|_| rng.gen()).collect::<Vec<E>>()),
|(c, xs)| {
for x in xs {
c.insert(*x);
@@ -40,7 +40,7 @@ where
print_result("insert", n, results);
}
-fn scenario_contains<T, E>(n: usize)
+fn scenario_contains<T, E>(rng: &mut StdRng, n: usize)
where
T: Container<E> + Default + Clone,
E: Copy,
@@ -48,7 +48,6 @@ where
{
let results = benchmark_op(
|| {
- let mut rng = thread_rng();
let mut c = T::default();
// decide where the element that we will search for will be
@@ -56,12 +55,12 @@ where
// insert the element at pivot, and keep track of what it is
for _ in 0..pivot {
- c.insert(random());
+ c.insert(rng.gen());
}
let chosen = rng.gen();
c.insert(chosen);
for _ in pivot..n {
- c.insert(random());
+ c.insert(rng.gen());
}
(c, chosen)
@@ -74,7 +73,7 @@ where
print_result("contains", n, results);
}
-fn scenario_remove<T, E>(n: usize)
+fn scenario_remove<T, E>(rng: &mut StdRng, n: usize)
where
T: Container<E> + Default + Clone,
E: Copy,
@@ -82,7 +81,6 @@ where
{
let results = benchmark_op(
|| {
- let mut rng = thread_rng();
let mut c = T::default();
// decide where the element that we will search for will be
@@ -90,12 +88,12 @@ where
// insert the element at pivot, and keep track of what it is
for _ in 0..pivot {
- c.insert(random());
+ c.insert(rng.gen());
}
let chosen = rng.gen();
c.insert(chosen);
for _ in pivot..n {
- c.insert(random());
+ c.insert(rng.gen());
}
(c, chosen)
@@ -108,7 +106,7 @@ where
print_result("remove", n, results);
}
-fn scenario_clear<T, E>(n: usize)
+fn scenario_clear<T, E>(rng: &mut StdRng, n: usize)
where
T: Container<E> + Default + Clone,
E: Copy,
@@ -118,7 +116,7 @@ where
|| {
let mut c = T::default();
for _ in 0..n {
- c.insert(random());
+ c.insert(rng.gen());
}
c
diff --git a/src/crates/benchmarker/src/indexable.rs b/src/crates/benchmarker/src/indexable.rs
index 196de6c..e00a8bc 100644
--- a/src/crates/benchmarker/src/indexable.rs
+++ b/src/crates/benchmarker/src/indexable.rs
@@ -1,24 +1,24 @@
use std::hint::black_box;
use primrose_library::traits::{Container, Indexable};
-use rand::{distributions::Standard, prelude::Distribution, random, thread_rng, Rng};
+use rand::{distributions::Standard, prelude::Distribution, rngs::StdRng, Rng};
use crate::{benchmark_op, print_result};
-pub fn benchmark_indexable<T, E>(ns: &[usize])
+pub fn benchmark_indexable<T, E>(rng: &mut StdRng, ns: &[usize])
where
T: Indexable<E> + Container<E> + Default + Clone,
E: Copy,
Standard: Distribution<E>,
{
for n in ns {
- scenario_first::<T, E>(*n);
- scenario_last::<T, E>(*n);
- scenario_nth::<T, E>(*n);
+ scenario_first::<T, E>(rng, *n);
+ scenario_last::<T, E>(rng, *n);
+ scenario_nth::<T, E>(rng, *n);
}
}
-fn scenario_first<T, E>(n: usize)
+fn scenario_first<T, E>(rng: &mut StdRng, n: usize)
where
T: Container<E> + Indexable<E> + Default + Clone,
E: Copy,
@@ -28,7 +28,7 @@ where
|| {
let mut c = T::default();
for _ in 0..n {
- c.insert(random());
+ c.insert(rng.gen());
}
c
@@ -42,7 +42,7 @@ where
print_result("first", n, results);
}
-fn scenario_last<T, E>(n: usize)
+fn scenario_last<T, E>(rng: &mut StdRng, n: usize)
where
T: Container<E> + Indexable<E> + Default + Clone,
E: Copy,
@@ -52,7 +52,7 @@ where
|| {
let mut c = T::default();
for _ in 0..n {
- c.insert(random());
+ c.insert(rng.gen());
}
c
@@ -66,7 +66,7 @@ where
print_result("last", n, results);
}
-fn scenario_nth<T, E>(n: usize)
+fn scenario_nth<T, E>(rng: &mut StdRng, n: usize)
where
T: Container<E> + Indexable<E> + Default + Clone,
E: Copy,
@@ -76,10 +76,10 @@ where
|| {
let mut c = T::default();
for _ in 0..n {
- c.insert(random());
+ c.insert(rng.gen());
}
- (c, thread_rng().gen_range(0..n))
+ (c, rng.gen_range(0..n))
},
|(c, idx)| {
let v = black_box(c.nth(*idx));
diff --git a/src/crates/benchmarker/src/lib.rs b/src/crates/benchmarker/src/lib.rs
index f449527..ed2e85c 100644
--- a/src/crates/benchmarker/src/lib.rs
+++ b/src/crates/benchmarker/src/lib.rs
@@ -8,6 +8,8 @@ use std::{
time::{Duration, Instant},
};
+pub use rand;
+
pub use container::*;
pub use indexable::*;
pub use mapping::*;
diff --git a/src/crates/benchmarker/src/mapping.rs b/src/crates/benchmarker/src/mapping.rs
index b567b30..0932a50 100644
--- a/src/crates/benchmarker/src/mapping.rs
+++ b/src/crates/benchmarker/src/mapping.rs
@@ -1,12 +1,13 @@
use primrose_library::traits::Mapping;
use rand::{
distributions::{Distribution, Standard},
- random, thread_rng, Rng,
+ rngs::StdRng,
+ Rng,
};
use crate::{benchmark_op, print_result};
-pub fn benchmark_mapping<T, K, V>(ns: &[usize])
+pub fn benchmark_mapping<T, K, V>(rng: &mut StdRng, ns: &[usize])
where
T: Mapping<K, V> + Default + Clone,
K: Copy,
@@ -14,14 +15,14 @@ where
Standard: Distribution<K> + Distribution<V>,
{
for n in ns {
- scenario_populate::<T, K, V>(*n);
- scenario_contains::<T, K, V>(*n);
- scenario_remove::<T, K, V>(*n);
- scenario_clear::<T, K, V>(*n);
+ scenario_populate::<T, K, V>(rng, *n);
+ scenario_contains::<T, K, V>(rng, *n);
+ scenario_remove::<T, K, V>(rng, *n);
+ scenario_clear::<T, K, V>(rng, *n);
}
}
-fn scenario_populate<T, K, V>(n: usize)
+fn scenario_populate<T, K, V>(rng: &mut StdRng, n: usize)
where
T: Mapping<K, V> + Default + Clone,
K: Copy,
@@ -33,7 +34,7 @@ where
(
T::default(),
(0..n)
- .map(|_| (random(), random()))
+ .map(|_| (rng.gen(), rng.gen()))
.collect::<Vec<(K, V)>>(),
)
},
@@ -52,7 +53,7 @@ where
print_result("insert", n, results);
}
-fn scenario_contains<T, K, V>(n: usize)
+fn scenario_contains<T, K, V>(rng: &mut StdRng, n: usize)
where
T: Mapping<K, V> + Default + Clone,
K: Copy,
@@ -61,7 +62,6 @@ where
{
let results = benchmark_op(
|| {
- let mut rng = thread_rng();
let mut c = T::default();
// decide where the element that we will search for will be
@@ -69,12 +69,12 @@ where
// insert the element at pivot, and keep track of what it is
for _ in 0..pivot {
- c.insert(random(), random());
+ c.insert(rng.gen(), rng.gen());
}
let chosen = rng.gen();
- c.insert(chosen, random());
+ c.insert(chosen, rng.gen());
for _ in pivot..n {
- c.insert(random(), random());
+ c.insert(rng.gen(), rng.gen());
}
(c, chosen)
@@ -85,7 +85,7 @@ where
print_result("contains", n, results);
}
-fn scenario_remove<T, K, V>(n: usize)
+fn scenario_remove<T, K, V>(rng: &mut StdRng, n: usize)
where
T: Mapping<K, V> + Default + Clone,
K: Copy,
@@ -94,7 +94,6 @@ where
{
let results = benchmark_op(
|| {
- let mut rng = thread_rng();
let mut c = T::default();
// decide where the element that we will search for will be
@@ -102,12 +101,12 @@ where
// insert the element at pivot, and keep track of what it is
for _ in 0..pivot {
- c.insert(random(), random());
+ c.insert(rng.gen(), rng.gen());
}
let chosen = rng.gen();
- c.insert(chosen, random());
+ c.insert(chosen, rng.gen());
for _ in pivot..n {
- c.insert(random(), random());
+ c.insert(rng.gen(), rng.gen());
}
(c, chosen)
@@ -118,7 +117,7 @@ where
print_result("remove", n, results);
}
-fn scenario_clear<T, K, V>(n: usize)
+fn scenario_clear<T, K, V>(rng: &mut StdRng, n: usize)
where
T: Mapping<K, V> + Default + Clone,
K: Copy,
@@ -130,7 +129,7 @@ where
let mut c = T::default();
for _ in 0..n {
- c.insert(random(), random());
+ c.insert(rng.gen(), rng.gen());
}
c
},
diff --git a/src/crates/benchmarker/src/stack.rs b/src/crates/benchmarker/src/stack.rs
index 93ba962..c5cf913 100644
--- a/src/crates/benchmarker/src/stack.rs
+++ b/src/crates/benchmarker/src/stack.rs
@@ -1,30 +1,30 @@
use std::hint::black_box;
use primrose_library::traits::{Container, Stack};
-use rand::{distributions::Standard, prelude::Distribution, random};
+use rand::{distributions::Standard, prelude::Distribution, rngs::StdRng, Rng};
use crate::{benchmark_op, print_result};
-pub fn benchmark_stack<T, E>(ns: &[usize])
+pub fn benchmark_stack<T, E>(rng: &mut StdRng, ns: &[usize])
where
T: Stack<E> + Container<E> + Default + Clone,
E: Copy,
Standard: Distribution<E>,
{
for n in ns {
- scenario_populate::<T, E>(*n);
- scenario_drain::<T, E>(*n);
+ scenario_populate::<T, E>(rng, *n);
+ scenario_drain::<T, E>(rng, *n);
}
}
-fn scenario_populate<T, E>(n: usize)
+fn scenario_populate<T, E>(rng: &mut StdRng, n: usize)
where
T: Stack<E> + Container<E> + Default + Clone,
E: Copy,
Standard: Distribution<E>,
{
let mut results = benchmark_op(
- || (T::default(), (0..n).map(|_| random()).collect::<Vec<E>>()),
+ || (T::default(), (0..n).map(|_| rng.gen()).collect::<Vec<E>>()),
|(c, xs)| {
for x in xs {
c.push(*x);
@@ -40,7 +40,7 @@ where
print_result("push", n, results);
}
-fn scenario_drain<T, E>(n: usize)
+fn scenario_drain<T, E>(rng: &mut StdRng, n: usize)
where
T: Stack<E> + Container<E> + Default + Clone,
E: Copy,
@@ -50,7 +50,7 @@ where
|| {
let mut c = T::default();
for _ in 0..n {
- c.push(random());
+ c.push(rng.gen());
}
c
},
diff --git a/src/crates/candelabra/src/cost/benchmark.rs b/src/crates/candelabra/src/cost/benchmark.rs
index 79e2a16..885bc4a 100644
--- a/src/crates/candelabra/src/cost/benchmark.rs
+++ b/src/crates/candelabra/src/cost/benchmark.rs
@@ -25,9 +25,12 @@ pub const ELEM_TYPE: &str = "usize";
/// String representation of the array of N values we use for benchmarking
pub const NS: &str = "[
- 64, 128, 256, 512, 2048, 5120, 16384, 32768, 65536,
+ 64, 256, 2048, 5120, 16384, 65536, 131072
]";
+/// Fixed seed for benchmarking
+pub const SEED: usize = 1337;
+
/// Results for a whole suite of benchmarks
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Results {
@@ -201,7 +204,7 @@ primrose-library = {{ path = \"{}\" }}
let implemented_traits = lib_spec.interface_provide_map.keys();
for tr in implemented_traits {
benchmark_statements += &format!(
- "candelabra_benchmarker::benchmark_{}::<{}<{}>, _>(&NS);",
+ "candelabra_benchmarker::benchmark_{}::<{}<{}>, _>(&mut rng, &NS);",
tr.to_lowercase(),
name,
ELEM_TYPE,
@@ -217,12 +220,13 @@ primrose-library = {{ path = \"{}\" }}
.write_all(
format!(
"
-const NS: &[usize] = &{};
+use candelabra_benchmarker::rand::{{rngs::StdRng, SeedableRng}};
+const NS: &[usize] = &{NS};
fn main() {{
- {}
+ let mut rng = StdRng::seed_from_u64({SEED});
+ {benchmark_statements}
}}
",
- NS, benchmark_statements
)
.as_bytes(),
)