Dump (benchmarking wip)

This commit is contained in:
Riley-Kilgore 2024-11-26 08:11:40 -08:00 committed by Riley
parent f569f213b2
commit e97e85a272
3 changed files with 68 additions and 11 deletions

View File

@ -435,6 +435,37 @@ impl PropertyTest {
None None
} }
} }
pub fn benchmark(
self,
seed: u32,
n: usize,
plutus_version: &PlutusVersion,
) -> Vec<BenchmarkResult> {
let mut results = Vec::with_capacity(n);
let mut remaining = n;
let mut prng = Prng::from_seed(seed);
while remaining > 0 {
match prng.sample(&self.fuzzer.program) {
Ok(Some((new_prng, value))) => {
prng = new_prng;
let mut eval_result = self.eval(&value, plutus_version);
results.push(BenchmarkResult {
test: self.clone(),
cost: eval_result.cost(),
success: !eval_result.failed(false),
traces: eval_result.logs().to_vec(),
});
}
Ok(None) => {}
Err(_) => break,
}
remaining -= 1;
}
results
}
} }
/// ----- PRNG ----------------------------------------------------------------- /// ----- PRNG -----------------------------------------------------------------
@ -941,10 +972,11 @@ where
// //
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
#[derive(Debug)] #[derive(Debug, Clone)]
pub enum TestResult<U, T> { pub enum TestResult<U, T> {
UnitTestResult(UnitTestResult<U>), UnitTestResult(UnitTestResult<U>),
PropertyTestResult(PropertyTestResult<T>), PropertyTestResult(PropertyTestResult<T>),
Benchmark(BenchmarkResult),
} }
unsafe impl<U, T> Send for TestResult<U, T> {} unsafe impl<U, T> Send for TestResult<U, T> {}
@ -959,6 +991,7 @@ impl TestResult<(Constant, Rc<Type>), PlutusData> {
TestResult::PropertyTestResult(test) => { TestResult::PropertyTestResult(test) => {
TestResult::PropertyTestResult(test.reify(data_types)) TestResult::PropertyTestResult(test.reify(data_types))
} }
TestResult::Benchmark(result) => TestResult::Benchmark(result),
} }
} }
} }
@ -981,6 +1014,7 @@ impl<U, T> TestResult<U, T> {
} }
OnTestFailure::SucceedImmediately => counterexample.is_some(), OnTestFailure::SucceedImmediately => counterexample.is_some(),
}, },
TestResult::Benchmark(BenchmarkResult { success, .. }) => *success,
} }
} }
@ -990,6 +1024,7 @@ impl<U, T> TestResult<U, T> {
TestResult::PropertyTestResult(PropertyTestResult { ref test, .. }) => { TestResult::PropertyTestResult(PropertyTestResult { ref test, .. }) => {
test.module.as_str() test.module.as_str()
} }
TestResult::Benchmark(BenchmarkResult { ref test, .. }) => test.module.as_str(),
} }
} }
@ -999,20 +1034,20 @@ impl<U, T> TestResult<U, T> {
TestResult::PropertyTestResult(PropertyTestResult { ref test, .. }) => { TestResult::PropertyTestResult(PropertyTestResult { ref test, .. }) => {
test.name.as_str() test.name.as_str()
} }
TestResult::Benchmark(BenchmarkResult { ref test, .. }) => test.name.as_str(),
} }
} }
pub fn traces(&self) -> &[String] { pub fn traces(&self) -> &[String] {
match self { match self {
TestResult::UnitTestResult(UnitTestResult { ref traces, .. }) TestResult::UnitTestResult(UnitTestResult { traces, .. })
| TestResult::PropertyTestResult(PropertyTestResult { ref traces, .. }) => { | TestResult::PropertyTestResult(PropertyTestResult { traces, .. }) => traces,
traces.as_slice() TestResult::Benchmark(BenchmarkResult { traces, .. }) => traces,
}
} }
} }
} }
#[derive(Debug)] #[derive(Debug, Clone)]
pub struct UnitTestResult<T> { pub struct UnitTestResult<T> {
pub success: bool, pub success: bool,
pub spent_budget: ExBudget, pub spent_budget: ExBudget,
@ -1058,7 +1093,7 @@ impl UnitTestResult<(Constant, Rc<Type>)> {
} }
} }
#[derive(Debug)] #[derive(Debug, Clone)]
pub struct PropertyTestResult<T> { pub struct PropertyTestResult<T> {
pub test: PropertyTest, pub test: PropertyTest,
pub counterexample: Result<Option<T>, uplc::machine::Error>, pub counterexample: Result<Option<T>, uplc::machine::Error>,
@ -1343,6 +1378,17 @@ impl Assertion<UntypedExpr> {
} }
} }
#[derive(Debug, Clone)]
pub struct BenchmarkResult {
pub test: PropertyTest,
pub cost: ExBudget,
pub success: bool,
pub traces: Vec<String>,
}
unsafe impl Send for BenchmarkResult {}
unsafe impl Sync for BenchmarkResult {}
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use super::*; use super::*;

View File

@ -193,6 +193,11 @@ impl Error {
test.input_path.to_path_buf(), test.input_path.to_path_buf(),
test.program.to_pretty(), test.program.to_pretty(),
), ),
TestResult::Benchmark(_) => (
"benchmark".to_string(),
PathBuf::new(),
String::new(),
),
}; };
Error::TestFailure { Error::TestFailure {

View File

@ -40,7 +40,7 @@ use aiken_lang::{
format::{Formatter, MAX_COLUMNS}, format::{Formatter, MAX_COLUMNS},
gen_uplc::CodeGenerator, gen_uplc::CodeGenerator,
line_numbers::LineNumbers, line_numbers::LineNumbers,
test_framework::{Test, TestResult}, test_framework::{Test, TestResult, BenchmarkResult},
tipo::{Type, TypeInfo}, tipo::{Type, TypeInfo},
utils, IdGenerator, utils, IdGenerator,
}; };
@ -61,7 +61,7 @@ use std::{
use telemetry::EventListener; use telemetry::EventListener;
use uplc::{ use uplc::{
ast::{Constant, Name, Program}, ast::{Constant, Name, Program},
PlutusData, PlutusData
}; };
#[derive(Debug)] #[derive(Debug)]
@ -303,15 +303,20 @@ where
exact_match: bool, exact_match: bool,
seed: u32, seed: u32,
property_max_success: usize, property_max_success: usize,
env: Option<String>,
output: PathBuf,
) -> Result<(), Vec<Error>> { ) -> Result<(), Vec<Error>> {
let options = Options { let options = Options {
tracing: Tracing::silent(), tracing: Tracing::silent(),
env,
code_gen_mode: CodeGenMode::Benchmark { code_gen_mode: CodeGenMode::Benchmark {
match_tests, match_tests,
exact_match, exact_match,
seed, seed,
property_max_success, property_max_success,
output,
} }
}; };
self.compile(options) self.compile(options)
@ -412,8 +417,9 @@ where
error, error,
path: options.blueprint_path, path: options.blueprint_path,
} }
.into() })?;
})
Ok(())
} }
CodeGenMode::Test { CodeGenMode::Test {
match_tests, match_tests,