diff options
author | Aria <me@aria.rip> | 2023-10-31 15:36:06 +0000 |
---|---|---|
committer | Aria <me@aria.rip> | 2023-10-31 15:36:06 +0000 |
commit | e576cb9af52c68177f1b40abc2e194b1570ca35e (patch) | |
tree | 42899aa397530b648be3ce8dd73b142fdb4debc0 /primrose/crates/candelabra-benchmarker/src | |
parent | 5465499f2cfe4ec105f9abcf4fb779e8ecbd04d7 (diff) |
feat(benchmarker): benchmark indexable trait
Diffstat (limited to 'primrose/crates/candelabra-benchmarker/src')
-rw-r--r-- | primrose/crates/candelabra-benchmarker/src/indexable.rs | 109 | ||||
-rw-r--r-- | primrose/crates/candelabra-benchmarker/src/lib.rs | 3 |
2 files changed, 112 insertions, 0 deletions
diff --git a/primrose/crates/candelabra-benchmarker/src/indexable.rs b/primrose/crates/candelabra-benchmarker/src/indexable.rs new file mode 100644 index 0000000..122ec58 --- /dev/null +++ b/primrose/crates/candelabra-benchmarker/src/indexable.rs @@ -0,0 +1,109 @@ +use std::{any::type_name, collections::HashMap}; + +use log::debug; +use primrose_library::traits::{Container, Indexable}; +use rand::{distributions::Standard, prelude::Distribution, random}; + +use crate::{benchmark_op, Results, RunResults, SingleNResults}; + +pub trait IndexableExt<E> { + /// Benchmark at the given `ns`. + fn benchmark_indexable(ns: &[usize]) -> Results; + + /// Benchmark at a single `n`. + fn benchmark_indexable_at(n: usize) -> SingleNResults; + + /// Benchmark `first` at a single `n`. + fn benchmark_indexable_first(n: usize) -> RunResults; + + /// Benchmark `last` at a single `n`. + fn benchmark_indexable_last(n: usize) -> RunResults; + + /// Benchmark `nth` at a single `n`. + fn benchmark_indexable_nth(n: usize) -> RunResults; +} + +impl<T, E> IndexableExt<E> for T +where + T: Container<E> + Indexable<E> + Default, + Standard: Distribution<E>, +{ + fn benchmark_indexable(ns: &[usize]) -> Results { + debug!( + "Benchmarking {} at {} different n values", + type_name::<T>(), + ns.len() + ); + + let mut by_n = HashMap::new(); + for n in ns { + by_n.insert(*n, Self::benchmark_indexable_at(*n)); + } + + Results { by_n } + } + + fn benchmark_indexable_at(n: usize) -> SingleNResults { + let mut by_op = HashMap::new(); + + debug!("Benchmarking {} at n = {}", type_name::<T>(), n); + + debug!("...first"); + by_op.insert("first", Self::benchmark_indexable_first(n)); + debug!("...last"); + by_op.insert("last", Self::benchmark_indexable_last(n)); + debug!("...nth"); + by_op.insert("nth", Self::benchmark_indexable_nth(n)); + debug!("--- done!"); + + SingleNResults { by_op } + } + + fn benchmark_indexable_first(n: usize) -> RunResults { + benchmark_op( + || { + let mut c = T::default(); + for _ in 0..n { + c.insert(random()); + } + c + }, + |c| { + c.first(); + }, + |_| (), + ) + } + + fn benchmark_indexable_last(n: usize) -> RunResults { + benchmark_op( + || { + let mut c = T::default(); + for _ in 0..n { + c.insert(random()); + } + c + }, + |c| { + c.last(); + }, + |_| (), + ) + } + + fn benchmark_indexable_nth(n: usize) -> RunResults { + benchmark_op( + || { + let mut c = T::default(); + for _ in 0..n { + c.insert(random()); + } + (c, random::<usize>()) + }, + |(c, fetch)| { + c.nth(*fetch); + }, + |_| (), + ) + } +} diff --git a/primrose/crates/candelabra-benchmarker/src/lib.rs b/primrose/crates/candelabra-benchmarker/src/lib.rs index c69d699..87fee8d 100644 --- a/primrose/crates/candelabra-benchmarker/src/lib.rs +++ b/primrose/crates/candelabra-benchmarker/src/lib.rs @@ -6,6 +6,9 @@ pub use bench::benchmark_op; mod container; pub use container::ContainerExt; +mod indexable; +pub use indexable::IndexableExt; + /// Results for a whole suite of benchmarks #[derive(Debug, Clone)] pub struct Results { |