use crate::types::Receipt;
use anyhow::Result;
use std::time::{Duration, Instant};
pub fn bench_throughput(iterations: u32) -> Result<()> {
let mut total_duration = Duration::ZERO;
for i in 0..iterations {
let start = Instant::now();
let mut asm = crate::chain::ChainAssembler::new();
let mut counter = crate::ocel::SeqCounter::new();
let event = crate::ocel::build_event(
"bench-op",
vec![crate::ocel::object_ref(&format!("obj-{}", i), "artifact")],
b"bench-payload",
&mut counter,
)?;
asm.append(event)?;
let receipt = asm.finalize();
let _verdict = crate::verifier::verify(&receipt);
total_duration += start.elapsed();
}
let avg = total_duration / iterations;
let ops_per_sec = 1.0 / avg.as_secs_f64();
eprintln!("Throughput Results:");
eprintln!(" Total iterations: {}", iterations);
eprintln!(" Avg latency: {:?}", avg);
eprintln!(" Throughput: {:.2} ops/sec", ops_per_sec);
Ok(())
}
pub fn bench_variance_on_receipt(path: &str, iterations: u32) -> Result<()> {
let receipt_json = std::fs::read_to_string(path)?;
let receipt: Receipt = serde_json::from_str(&receipt_json)?;
let start = Instant::now();
for _ in 0..iterations {
#[cfg(feature = "discovery")]
{
let (_nodes, _edges, _s, _e) = crate::discovery::discover_dfg_summary(&receipt);
let (_f, _a, _s) = crate::discovery::quality_metrics(&receipt);
}
#[cfg(not(feature = "discovery"))]
let _ = &receipt;
}
let elapsed = start.elapsed();
eprintln!("Variance Analysis ({}):", path);
eprintln!(" Iterations: {}", iterations);
eprintln!(" Avg time: {:?}", elapsed / iterations);
#[cfg(not(feature = "discovery"))]
eprintln!(" (Note: discovery metrics disabled, benchmark only measured deserialization loop)");
Ok(())
}
pub fn bench_variance_suite(_iterations: u32) -> Result<()> {
eprintln!("Running standard variance suite...");
Ok(())
}
pub fn run_profile_workload(seconds: u64, _receipt_path: Option<&str>) -> Result<()> {
let duration = Duration::from_secs(seconds);
let start = Instant::now();
let mut count = 0;
eprintln!("Profiling workload active for {}s...", seconds);
while start.elapsed() < duration {
let _ = bench_throughput(1)?;
count += 1;
}
eprintln!("Profile workload complete. Executed {} cycles.", count);
Ok(())
}