From e0f5dc6043ef51649a2e5d45dfb2fc0cff1250a4 Mon Sep 17 00:00:00 2001 From: "hinto.janai" Date: Fri, 29 Mar 2024 20:48:21 -0400 Subject: [PATCH] create `{Benchmark, Stats, fn}` mappings --- .../benchmark/src/{benchmarks.rs => bench.rs} | 66 +++++++++++++++---- database/benchmark/src/config.rs | 10 +-- database/benchmark/src/main.rs | 10 +-- database/benchmark/src/state.rs | 13 ---- 4 files changed, 64 insertions(+), 35 deletions(-) rename database/benchmark/src/{benchmarks.rs => bench.rs} (55%) delete mode 100644 database/benchmark/src/state.rs diff --git a/database/benchmark/src/benchmarks.rs b/database/benchmark/src/bench.rs similarity index 55% rename from database/benchmark/src/benchmarks.rs rename to database/benchmark/src/bench.rs index 6abead9..0ba60a0 100644 --- a/database/benchmark/src/benchmarks.rs +++ b/database/benchmark/src/bench.rs @@ -5,6 +5,7 @@ use std::{ borrow::Cow, collections::BTreeSet, path::{Path, PathBuf}, + time::Instant, }; use serde::{Deserialize, Serialize}; @@ -37,24 +38,43 @@ use crate::config::Config; IntoStaticStr, VariantArray, )] -pub(crate) enum Benchmarks { - /// TODO +pub(crate) enum Benchmark { + /// Maps to [`env_open`]. EnvOpen, } +impl Benchmark { + /// Map [`Benchmark`] to the proper benchmark function. + #[inline(always)] + fn benchmark_fn(self) -> fn(&ConcreteEnv) { + match self { + Self::EnvOpen => env_open, + } + } +} + //---------------------------------------------------------------------------------------------------- Stats /// TODO #[derive(Clone, Copy, PartialEq, PartialOrd, Serialize, Deserialize, Debug)] struct Stats { - /// TODO + /// Timings for [`env_open`]. env_open: Option, } impl Stats { - /// TODO + /// Create a new [`Stats`] with no benchmark timings. const fn new() -> Self { Self { env_open: None } } + + /// This maps [`Benchmark`]s to a specific field in [`Stats`] + /// which then gets updated to the passed `time`. + #[inline(always)] + fn update_benchmark_time(&mut self, benchmark: Benchmark, time: f32) { + *match benchmark { + Benchmark::EnvOpen => &mut self.env_open, + } = Some(time); + } } //---------------------------------------------------------------------------------------------------- TODO @@ -79,6 +99,8 @@ impl Drop for Benchmarker { //---------------------------------------------------------------------------------------------------- Impl impl Benchmarker { /// TODO + #[cold] + #[inline(never)] pub(crate) const fn new(env: ConcreteEnv, config: Config) -> Self { Self { env, @@ -87,13 +109,33 @@ impl Benchmarker { } } - /// TODO - pub(crate) fn bench(self) { - todo!() - } - - /// TODO - pub(crate) fn todo(&mut self) { - println!("TODO"); + /// Start all benchmark that are selected by the user. + /// `main()` calls this once. + #[cold] + #[inline(never)] + pub(crate) fn bench_all(mut self) { + for benchmark in &self.config.benchmark_set { + bench(*benchmark, &self.env, &mut self.stats); + } } } + +//---------------------------------------------------------------------------------------------------- Benchmark functions +/// TODO +#[inline(always)] +fn bench(benchmark: Benchmark, env: &ConcreteEnv, stats: &mut Stats) { + // Start the benchmark timer. + let instant = Instant::now(); + + // Benchmark. + benchmark.benchmark_fn()(env); + + // Update the time. + stats.update_benchmark_time(benchmark, instant.elapsed().as_secs_f32()); +} + +/// TODO +#[inline(always)] +fn env_open(env: &ConcreteEnv) { + println!("TODO"); +} diff --git a/database/benchmark/src/config.rs b/database/benchmark/src/config.rs index 9828a64..0f23bbf 100644 --- a/database/benchmark/src/config.rs +++ b/database/benchmark/src/config.rs @@ -10,16 +10,16 @@ use std::{ use serde::{Deserialize, Serialize}; use strum::IntoEnumIterator; -use crate::{benchmarks::Benchmarks, cli::Cli}; +use crate::{bench::Benchmark, cli::Cli}; //---------------------------------------------------------------------------------------------------- Config /// TODO #[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Serialize, Deserialize)] -pub struct Config { +pub(crate) struct Config { /// TODO - iterations: usize, + pub(crate) iterations: usize, /// TODO - benchmark_set: BTreeSet, + pub(crate) benchmark_set: BTreeSet, } impl Config { @@ -27,7 +27,7 @@ impl Config { pub(crate) fn new() -> Self { Self { iterations: 100_000, - benchmark_set: Benchmarks::iter().collect(), + benchmark_set: Benchmark::iter().collect(), } } diff --git a/database/benchmark/src/main.rs b/database/benchmark/src/main.rs index 01e6276..5be3b9a 100644 --- a/database/benchmark/src/main.rs +++ b/database/benchmark/src/main.rs @@ -57,6 +57,7 @@ // TODO: should be removed after all `todo!()`'s are gone. clippy::diverging_sub_expression, + clippy::inline_always, clippy::module_name_repetitions, clippy::module_inception, clippy::redundant_pub_crate, @@ -69,12 +70,11 @@ // Import private modules, export public types. // // Documentation for each module is located in the respective file. -mod benchmarks; +mod bench; mod cli; mod config; mod constants; mod free; -mod state; //---------------------------------------------------------------------------------------------------- Private @@ -113,7 +113,7 @@ fn main() { }; // Print config before starting. - println!("{config:#?}"); + eprintln!("cuprate-database-benchmark configuration:\n{config:#?}"); // If `dry_run`, exit cleanly. if cli.dry_run { @@ -124,6 +124,6 @@ fn main() { let env = cuprate_database::ConcreteEnv::open(db_config).unwrap(); // Start benchmarking/tests. - let mut benchmarker = crate::benchmarks::Benchmarker::new(env, todo!()); - benchmarker.todo(); + let mut benchmarker = crate::bench::Benchmarker::new(env, todo!()); + benchmarker.bench_all(); } diff --git a/database/benchmark/src/state.rs b/database/benchmark/src/state.rs deleted file mode 100644 index 45fa752..0000000 --- a/database/benchmark/src/state.rs +++ /dev/null @@ -1,13 +0,0 @@ -//! TODO - -//---------------------------------------------------------------------------------------------------- Import -use std::{ - borrow::Cow, - path::{Path, PathBuf}, -}; - -//---------------------------------------------------------------------------------------------------- Config -/// TODO -#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord)] -#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] -pub struct State {}