aboutsummaryrefslogtreecommitdiff
path: root/src/crates/benchmarker
diff options
context:
space:
mode:
authorAria <me@aria.rip>2023-11-28 14:31:28 +0000
committerAria <me@aria.rip>2023-11-28 14:31:28 +0000
commitd9f944910a956f855e6e039b6c408bee15455d66 (patch)
tree7f85a0050ac75e70cde7f93125d0bafbcbd65120 /src/crates/benchmarker
parentb73cf91fd6debef13fe7ec9081f27c49a4707cd3 (diff)
fix(benchmarker): convert rest to criterion
Diffstat (limited to 'src/crates/benchmarker')
-rw-r--r--src/crates/benchmarker/src/container.rs25
-rw-r--r--src/crates/benchmarker/src/indexable.rs141
-rw-r--r--src/crates/benchmarker/src/lib.rs24
-rw-r--r--src/crates/benchmarker/src/stack.rs87
4 files changed, 82 insertions, 195 deletions
diff --git a/src/crates/benchmarker/src/container.rs b/src/crates/benchmarker/src/container.rs
index 9da58d6..4e0a111 100644
--- a/src/crates/benchmarker/src/container.rs
+++ b/src/crates/benchmarker/src/container.rs
@@ -1,7 +1,9 @@
-use criterion::{black_box, BatchSize, Bencher, BenchmarkId, Criterion};
+use criterion::{black_box, BatchSize, Criterion};
use primrose_library::traits::{Container, Indexable};
use rand::{distributions::Standard, prelude::Distribution, random, thread_rng, Rng};
+use crate::bench_with_ns;
+
pub fn benchmark_container<T, E>(c: &mut Criterion, ns: &[usize])
where
T: Container<E> + Indexable<E> + Default + Clone,
@@ -60,24 +62,3 @@ where
b.iter_batched_ref(|| container.clone(), |c| c.clear(), BatchSize::LargeInput);
});
}
-
-fn bench_with_ns<T, E>(
- c: &mut Criterion,
- ns: &[usize],
- name: &str,
- mut f: impl FnMut(&mut Bencher<'_>, &T) -> (),
-) where
- T: Container<E> + Default + Clone,
- E: Clone,
- Standard: Distribution<E>,
-{
- let mut g = c.benchmark_group(name);
- for n in ns {
- let mut container = T::default();
- for _ in 0..*n {
- container.insert(random::<E>());
- }
-
- g.bench_with_input(BenchmarkId::from_parameter(n), &container, |b, n| f(b, n));
- }
-}
diff --git a/src/crates/benchmarker/src/indexable.rs b/src/crates/benchmarker/src/indexable.rs
index 5a194f0..e993c93 100644
--- a/src/crates/benchmarker/src/indexable.rs
+++ b/src/crates/benchmarker/src/indexable.rs
@@ -1,109 +1,46 @@
-use std::{any::type_name, collections::HashMap, hint::black_box};
+use criterion::{black_box, BatchSize, Criterion};
+use primrose_library::traits::{Container, Indexable};
+use rand::{distributions::Standard, prelude::Distribution, thread_rng, Rng};
-use criterion::Criterion;
-use log::debug;
-use primrose_library::traits::{Container, Indexable, Stack};
-use rand::{distributions::Standard, prelude::Distribution, random};
+use crate::bench_with_ns;
-pub fn benchmark_indexable<T, E>(c: &mut Criterion)
+pub fn benchmark_indexable<T, E>(c: &mut Criterion, ns: &[usize])
where
- T: Stack<E> + Default,
+ T: Indexable<E> + Container<E> + Default + Clone,
E: Clone,
Standard: Distribution<E>,
{
- todo!()
+ bench_with_ns::<T, E>(c, ns, "first", |b, container| {
+ b.iter_batched_ref(
+ || container.clone(),
+ |c| {
+ black_box(c.first());
+ },
+ BatchSize::LargeInput,
+ );
+ });
+
+ bench_with_ns::<T, E>(c, ns, "last", |b, container| {
+ b.iter_batched_ref(
+ || container.clone(),
+ |c| {
+ black_box(c.last());
+ },
+ BatchSize::LargeInput,
+ );
+ });
+
+ bench_with_ns::<T, E>(c, ns, "nth", |b, container| {
+ b.iter_batched_ref(
+ || {
+ let mut container = container.clone();
+ let i = thread_rng().gen_range(0..container.len());
+ (container, i)
+ },
+ |(c, i)| {
+ black_box(c.nth(*i));
+ },
+ BatchSize::LargeInput,
+ );
+ });
}
-
-// /// Benchmark [`primrose_library::traits::Indexable`] operations
-// pub trait IndexableExt<E> {
-// /// Benchmark at a single `n`.
-// fn benchmark_indexable_at(n: usize) -> Results;
-
-// /// Benchmark `first` at a single `n`.
-// fn benchmark_indexable_first(n: usize) -> Observation;
-
-// /// Benchmark `last` at a single `n`.
-// fn benchmark_indexable_last(n: usize) -> Observation;
-
-// /// Benchmark `nth` at a single `n`.
-// fn benchmark_indexable_nth(n: usize) -> Observation;
-// }
-
-// impl<T, E> IndexableExt<E> for T
-// where
-// T: Container<E> + Indexable<E> + Default,
-// Standard: Distribution<E>,
-// {
-// fn benchmark_indexable_at(n: usize) -> Results {
-// let mut by_op = HashMap::new();
-
-// debug!("Benchmarking {} at n = {}", type_name::<T>(), n);
-
-// debug!("...first");
-// by_op.insert(
-// "first".to_string(),
-// vec![Self::benchmark_indexable_first(n)],
-// );
-// debug!("...last");
-// by_op.insert("last".to_string(), vec![Self::benchmark_indexable_last(n)]);
-// debug!("...nth");
-// by_op.insert("nth".to_string(), vec![Self::benchmark_indexable_nth(n)]);
-// debug!("--- done!");
-
-// Results { by_op }
-// }
-
-// fn benchmark_indexable_first(n: usize) -> Observation {
-// (
-// n,
-// benchmark_op(
-// || {
-// let mut c = T::default();
-// for _ in 0..n {
-// c.insert(random());
-// }
-// c
-// },
-// |c| {
-// black_box(c.first());
-// },
-// ),
-// )
-// }
-
-// fn benchmark_indexable_last(n: usize) -> Observation {
-// (
-// n,
-// benchmark_op(
-// || {
-// let mut c = T::default();
-// for _ in 0..n {
-// c.insert(random());
-// }
-// c
-// },
-// |c| {
-// black_box(c.last());
-// },
-// ),
-// )
-// }
-
-// fn benchmark_indexable_nth(n: usize) -> Observation {
-// (
-// n,
-// 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/src/crates/benchmarker/src/lib.rs b/src/crates/benchmarker/src/lib.rs
index eaaace6..a910e48 100644
--- a/src/crates/benchmarker/src/lib.rs
+++ b/src/crates/benchmarker/src/lib.rs
@@ -3,5 +3,29 @@ mod indexable;
mod stack;
pub use container::*;
+use criterion::{Bencher, BenchmarkId, Criterion};
pub use indexable::*;
+use primrose_library::traits::Container;
+use rand::{distributions::Standard, prelude::Distribution, random};
pub use stack::*;
+
+pub fn bench_with_ns<T, E>(
+ c: &mut Criterion,
+ ns: &[usize],
+ name: &str,
+ mut f: impl FnMut(&mut Bencher<'_>, &T) -> (),
+) where
+ T: Container<E> + Default + Clone,
+ E: Clone,
+ Standard: Distribution<E>,
+{
+ let mut g = c.benchmark_group(name);
+ for n in ns {
+ let mut container = T::default();
+ for _ in 0..*n {
+ container.insert(random::<E>());
+ }
+
+ g.bench_with_input(BenchmarkId::from_parameter(n), &container, |b, n| f(b, n));
+ }
+}
diff --git a/src/crates/benchmarker/src/stack.rs b/src/crates/benchmarker/src/stack.rs
index 7f32f0b..499c997 100644
--- a/src/crates/benchmarker/src/stack.rs
+++ b/src/crates/benchmarker/src/stack.rs
@@ -1,79 +1,24 @@
-use std::{any::type_name, collections::HashMap};
-
-use criterion::Criterion;
-use log::debug;
+use criterion::{BatchSize, Criterion};
use primrose_library::traits::{Container, Stack};
use rand::{distributions::Standard, prelude::Distribution, random};
-pub fn benchmark_stack<T, E>(c: &mut Criterion)
+use crate::bench_with_ns;
+
+pub fn benchmark_stack<T, E>(c: &mut Criterion, ns: &[usize])
where
- T: Stack<E> + Default,
+ T: Stack<E> + Container<E> + Default + Clone,
E: Clone,
Standard: Distribution<E>,
{
- todo!()
+ bench_with_ns::<T, E>(c, ns, "push", |b, container| {
+ b.iter_batched_ref(
+ || (container.clone(), random::<E>()),
+ |(c, e)| c.push(e.clone()),
+ BatchSize::LargeInput,
+ );
+ });
+
+ bench_with_ns::<T, E>(c, ns, "pop", |b, container| {
+ b.iter_batched_ref(|| container.clone(), |c| c.pop(), BatchSize::LargeInput);
+ });
}
-
-// /// Benchmark [`primrose_library::traits::Stack`] operations
-// pub trait StackExt<E> {
-// /// Benchmark at a single `n`.
-// fn benchmark_stack_at(n: usize) -> Results;
-
-// /// Benchmark `push` at a single `n`.
-// fn benchmark_stack_push(n: usize) -> Observation;
-
-// /// Benchmark `pop` at a single `n`.
-// fn benchmark_stack_pop(n: usize) -> Observation;
-// }
-
-// impl<T, E> StackExt<E> for T
-// where
-// T: Stack<E> + Default,
-// Standard: Distribution<E>,
-// {
-// fn benchmark_stack_at(n: usize) -> Results {
-// let mut by_op = HashMap::new();
-
-// debug!("Benchmarking {} at n = {}", type_name::<T>(), n);
-
-// debug!("...push");
-// by_op.insert("push".to_string(), vec![Self::benchmark_stack_push(n)]);
-// debug!("...pop");
-// by_op.insert("pop".to_string(), vec![Self::benchmark_stack_pop(n)]);
-// debug!("--- done!");
-
-// Results { by_op }
-// }
-
-// fn benchmark_stack_push(n: usize) -> Observation {
-// (
-// n,
-// benchmark_op(
-// || {
-// let mut c = T::default();
-// for _ in 0..n {
-// c.push(random());
-// }
-// c
-// },
-// |s| s.push(random()),
-// ),
-// )
-// }
-
-// fn benchmark_stack_pop(n: usize) -> Observation {
-// (
-// n,
-// benchmark_op(
-// || {
-// let mut c = T::default();
-// for _ in 0..n {
-// c.push(random());
-// }
-// c
-// },
-// |s| s.pop(),
-// ),
-// )
-// }
-// }