use std::collections::HashSet;
use crate::{Features, registry};
mod dim;
mod gated;
mod typed;
pub(crate) use typed::TypeInput;
#[derive(Debug, Default, Clone)]
pub struct TestConfig {
features: HashSet<String>,
}
impl TestConfig {
pub fn new() -> Self {
Self::default()
}
pub fn with_features<I, S>(features: I) -> Self
where
I: IntoIterator<Item = S>,
S: Into<String>,
{
Self {
features: features.into_iter().map(Into::into).collect(),
}
}
fn enabled(&self, features: &Features) -> bool {
features.satisfied_by(&self.features)
}
}
pub fn register_benchmarks(
registry: &mut registry::Registry,
config: &TestConfig,
) -> Result<(), registry::RegistryError> {
registry.register_regression("type-bench-f32", typed::TypeBench::<f32>::new())?;
registry.register_regression("type-bench-i8", typed::TypeBench::<i8>::new())?;
registry.register_regression(
"exact-type-bench-f32-1000",
typed::ExactTypeBench::<f32, 1000>::new(),
)?;
registry.register("simple-bench", dim::SimpleBench)?;
registry.register_regression("dim-bench", dim::DimBench)?;
let features = Features::new("gated-feature-0");
if config.enabled(&features) {
registry.register("gated-bench", gated::GatedBench)?;
} else {
registry.register_partially_gated::<TypeInput>(
"gated-bench",
features,
"A gated benchmark",
)?;
}
let features = Features::all(["gated-feature-0", "gated-feature-1"]);
if config.enabled(&features) {
registry.register("another-gated-bench", gated::AnotherGatedBench)?;
} else {
registry.register_partially_gated::<dim::DimInput>(
"another-gated-bench",
features,
"Another gated benchmark",
)?;
}
let features = Features::any(["gated-feature-2", "gated-feature-3"]);
if config.enabled(&features) {
registry.register("fully-gated-bench", gated::FullyGatedBench)?;
} else {
registry.register_gated(
"phantom-input",
"fully-gated-bench",
features,
"A fully gated benchmark",
)?;
}
let features = Features::new("gated-feature-3");
if config.enabled(&features) {
registry.register("something-else", gated::GatedWithIndependentInput)?;
} else {
registry.register_partially_gated::<gated::SampleInput>(
"something-else",
features.clone(),
"A gated benchmark that always registers its input",
)?;
registry.register_partially_gated::<gated::SampleInput>(
"something-else-v3",
features,
"A gated benchmark that always registers its input - a redundant registration.",
)?;
}
Ok(())
}