use std::io::Write;
use serde::{Deserialize, Serialize};
use crate::{
Benchmark, Checker, Checkpoint, Input, Output, benchmark::MatchContext, benchmark::Score,
};
use super::{dim::DimInput, typed::TypeInput};
#[derive(Debug)]
pub(super) struct GatedBench;
impl Benchmark for GatedBench {
type Input = TypeInput;
type Output = String;
fn try_match(&self, _input: &TypeInput, context: &MatchContext) -> Score {
context.success(0)
}
fn description(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str("A gated benchmark")
}
fn run(
&self,
input: &TypeInput,
_checkpoint: Checkpoint<'_>,
mut output: &mut dyn Output,
) -> anyhow::Result<Self::Output> {
write!(output, "gated bench: {:?}", input)?;
Ok(format!("{:?}", input))
}
}
#[derive(Debug)]
pub(super) struct AnotherGatedBench;
impl Benchmark for AnotherGatedBench {
type Input = DimInput;
type Output = usize;
fn try_match(&self, _input: &DimInput, context: &MatchContext) -> Score {
context.success(0)
}
fn description(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str("Another gated benchmark")
}
fn run(
&self,
input: &DimInput,
_checkpoint: Checkpoint<'_>,
mut output: &mut dyn Output,
) -> anyhow::Result<Self::Output> {
write!(output, "another gated bench: {:?}", input)?;
Ok(0)
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub(super) struct SampleInput {
value: String,
}
impl Input for SampleInput {
type Raw = Self;
fn tag() -> &'static str {
"sample-input"
}
fn from_raw(raw: Self::Raw, _checker: &mut Checker) -> anyhow::Result<Self> {
Ok(raw)
}
fn serialize(&self) -> anyhow::Result<serde_json::Value> {
Ok(serde_json::to_value(self)?)
}
fn example() -> Self::Raw {
Self {
value: "hello world".into(),
}
}
}
#[derive(Debug)]
pub(super) struct GatedWithIndependentInput;
impl Benchmark for GatedWithIndependentInput {
type Input = SampleInput;
type Output = String;
fn try_match(&self, _input: &SampleInput, context: &MatchContext) -> Score {
context.success(0)
}
fn description(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str("A gated benchmark that always registers its input")
}
fn run(
&self,
input: &SampleInput,
_checkpoint: Checkpoint<'_>,
mut output: &mut dyn Output,
) -> anyhow::Result<Self::Output> {
write!(output, "partially-gated bench: {:?}", input)?;
Ok(input.value.clone())
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub(super) struct PhantomInput {
value: usize,
}
impl Input for PhantomInput {
type Raw = Self;
fn tag() -> &'static str {
"phantom-input"
}
fn from_raw(raw: Self::Raw, _checker: &mut Checker) -> anyhow::Result<Self> {
Ok(raw)
}
fn serialize(&self) -> anyhow::Result<serde_json::Value> {
Ok(serde_json::to_value(self)?)
}
fn example() -> Self::Raw {
Self { value: 42 }
}
}
#[derive(Debug)]
pub(super) struct FullyGatedBench;
impl Benchmark for FullyGatedBench {
type Input = PhantomInput;
type Output = usize;
fn try_match(&self, _input: &PhantomInput, context: &MatchContext) -> Score {
context.success(0)
}
fn description(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str("A fully gated benchmark")
}
fn run(
&self,
input: &PhantomInput,
_checkpoint: Checkpoint<'_>,
mut output: &mut dyn Output,
) -> anyhow::Result<Self::Output> {
write!(output, "fully gated bench: {:?}", input)?;
Ok(input.value)
}
}