From d9f944910a956f855e6e039b6c408bee15455d66 Mon Sep 17 00:00:00 2001 From: Aria Date: Tue, 28 Nov 2023 14:31:28 +0000 Subject: fix(benchmarker): convert rest to criterion --- src/crates/benchmarker/src/container.rs | 25 +----- src/crates/benchmarker/src/indexable.rs | 141 +++++++++----------------------- src/crates/benchmarker/src/lib.rs | 24 ++++++ src/crates/benchmarker/src/stack.rs | 87 ++++---------------- 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(c: &mut Criterion, ns: &[usize]) where T: Container + Indexable + Default + Clone, @@ -60,24 +62,3 @@ where b.iter_batched_ref(|| container.clone(), |c| c.clear(), BatchSize::LargeInput); }); } - -fn bench_with_ns( - c: &mut Criterion, - ns: &[usize], - name: &str, - mut f: impl FnMut(&mut Bencher<'_>, &T) -> (), -) where - T: Container + Default + Clone, - E: Clone, - Standard: Distribution, -{ - let mut g = c.benchmark_group(name); - for n in ns { - let mut container = T::default(); - for _ in 0..*n { - container.insert(random::()); - } - - 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(c: &mut Criterion) +pub fn benchmark_indexable(c: &mut Criterion, ns: &[usize]) where - T: Stack + Default, + T: Indexable + Container + Default + Clone, E: Clone, Standard: Distribution, { - todo!() + bench_with_ns::(c, ns, "first", |b, container| { + b.iter_batched_ref( + || container.clone(), + |c| { + black_box(c.first()); + }, + BatchSize::LargeInput, + ); + }); + + bench_with_ns::(c, ns, "last", |b, container| { + b.iter_batched_ref( + || container.clone(), + |c| { + black_box(c.last()); + }, + BatchSize::LargeInput, + ); + }); + + bench_with_ns::(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 { -// /// 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 IndexableExt for T -// where -// T: Container + Indexable + Default, -// Standard: Distribution, -// { -// fn benchmark_indexable_at(n: usize) -> Results { -// let mut by_op = HashMap::new(); - -// debug!("Benchmarking {} at n = {}", type_name::(), 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::()) -// }, -// |(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( + c: &mut Criterion, + ns: &[usize], + name: &str, + mut f: impl FnMut(&mut Bencher<'_>, &T) -> (), +) where + T: Container + Default + Clone, + E: Clone, + Standard: Distribution, +{ + let mut g = c.benchmark_group(name); + for n in ns { + let mut container = T::default(); + for _ in 0..*n { + container.insert(random::()); + } + + 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(c: &mut Criterion) +use crate::bench_with_ns; + +pub fn benchmark_stack(c: &mut Criterion, ns: &[usize]) where - T: Stack + Default, + T: Stack + Container + Default + Clone, E: Clone, Standard: Distribution, { - todo!() + bench_with_ns::(c, ns, "push", |b, container| { + b.iter_batched_ref( + || (container.clone(), random::()), + |(c, e)| c.push(e.clone()), + BatchSize::LargeInput, + ); + }); + + bench_with_ns::(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 { -// /// 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 StackExt for T -// where -// T: Stack + Default, -// Standard: Distribution, -// { -// fn benchmark_stack_at(n: usize) -> Results { -// let mut by_op = HashMap::new(); - -// debug!("Benchmarking {} at n = {}", type_name::(), 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(), -// ), -// ) -// } -// } -- cgit v1.2.3