create {Benchmark, Stats, fn} mappings

This commit is contained in:
hinto.janai 2024-03-29 20:48:21 -04:00
parent 79ed8a5267
commit e0f5dc6043
No known key found for this signature in database
GPG key ID: D47CE05FA175A499
4 changed files with 64 additions and 35 deletions

View file

@ -5,6 +5,7 @@ use std::{
borrow::Cow, borrow::Cow,
collections::BTreeSet, collections::BTreeSet,
path::{Path, PathBuf}, path::{Path, PathBuf},
time::Instant,
}; };
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@ -37,24 +38,43 @@ use crate::config::Config;
IntoStaticStr, IntoStaticStr,
VariantArray, VariantArray,
)] )]
pub(crate) enum Benchmarks { pub(crate) enum Benchmark {
/// TODO /// Maps to [`env_open`].
EnvOpen, 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 //---------------------------------------------------------------------------------------------------- Stats
/// TODO /// TODO
#[derive(Clone, Copy, PartialEq, PartialOrd, Serialize, Deserialize, Debug)] #[derive(Clone, Copy, PartialEq, PartialOrd, Serialize, Deserialize, Debug)]
struct Stats { struct Stats {
/// TODO /// Timings for [`env_open`].
env_open: Option<f32>, env_open: Option<f32>,
} }
impl Stats { impl Stats {
/// TODO /// Create a new [`Stats`] with no benchmark timings.
const fn new() -> Self { const fn new() -> Self {
Self { env_open: None } 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 //---------------------------------------------------------------------------------------------------- TODO
@ -79,6 +99,8 @@ impl Drop for Benchmarker {
//---------------------------------------------------------------------------------------------------- Impl //---------------------------------------------------------------------------------------------------- Impl
impl Benchmarker { impl Benchmarker {
/// TODO /// TODO
#[cold]
#[inline(never)]
pub(crate) const fn new(env: ConcreteEnv, config: Config) -> Self { pub(crate) const fn new(env: ConcreteEnv, config: Config) -> Self {
Self { Self {
env, env,
@ -87,13 +109,33 @@ impl Benchmarker {
} }
} }
/// TODO /// Start all benchmark that are selected by the user.
pub(crate) fn bench(self) { /// `main()` calls this once.
todo!() #[cold]
} #[inline(never)]
pub(crate) fn bench_all(mut self) {
/// TODO for benchmark in &self.config.benchmark_set {
pub(crate) fn todo(&mut self) { bench(*benchmark, &self.env, &mut self.stats);
println!("TODO"); }
} }
} }
//---------------------------------------------------------------------------------------------------- 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");
}

View file

@ -10,16 +10,16 @@ use std::{
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use strum::IntoEnumIterator; use strum::IntoEnumIterator;
use crate::{benchmarks::Benchmarks, cli::Cli}; use crate::{bench::Benchmark, cli::Cli};
//---------------------------------------------------------------------------------------------------- Config //---------------------------------------------------------------------------------------------------- Config
/// TODO /// TODO
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Serialize, Deserialize)] #[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Serialize, Deserialize)]
pub struct Config { pub(crate) struct Config {
/// TODO /// TODO
iterations: usize, pub(crate) iterations: usize,
/// TODO /// TODO
benchmark_set: BTreeSet<Benchmarks>, pub(crate) benchmark_set: BTreeSet<Benchmark>,
} }
impl Config { impl Config {
@ -27,7 +27,7 @@ impl Config {
pub(crate) fn new() -> Self { pub(crate) fn new() -> Self {
Self { Self {
iterations: 100_000, iterations: 100_000,
benchmark_set: Benchmarks::iter().collect(), benchmark_set: Benchmark::iter().collect(),
} }
} }

View file

@ -57,6 +57,7 @@
// TODO: should be removed after all `todo!()`'s are gone. // TODO: should be removed after all `todo!()`'s are gone.
clippy::diverging_sub_expression, clippy::diverging_sub_expression,
clippy::inline_always,
clippy::module_name_repetitions, clippy::module_name_repetitions,
clippy::module_inception, clippy::module_inception,
clippy::redundant_pub_crate, clippy::redundant_pub_crate,
@ -69,12 +70,11 @@
// Import private modules, export public types. // Import private modules, export public types.
// //
// Documentation for each module is located in the respective file. // Documentation for each module is located in the respective file.
mod benchmarks; mod bench;
mod cli; mod cli;
mod config; mod config;
mod constants; mod constants;
mod free; mod free;
mod state;
//---------------------------------------------------------------------------------------------------- Private //---------------------------------------------------------------------------------------------------- Private
@ -113,7 +113,7 @@ fn main() {
}; };
// Print config before starting. // Print config before starting.
println!("{config:#?}"); eprintln!("cuprate-database-benchmark configuration:\n{config:#?}");
// If `dry_run`, exit cleanly. // If `dry_run`, exit cleanly.
if cli.dry_run { if cli.dry_run {
@ -124,6 +124,6 @@ fn main() {
let env = cuprate_database::ConcreteEnv::open(db_config).unwrap(); let env = cuprate_database::ConcreteEnv::open(db_config).unwrap();
// Start benchmarking/tests. // Start benchmarking/tests.
let mut benchmarker = crate::benchmarks::Benchmarker::new(env, todo!()); let mut benchmarker = crate::bench::Benchmarker::new(env, todo!());
benchmarker.todo(); benchmarker.bench_all();
} }

View file

@ -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 {}