affidavit 26.6.22

Provenance Layer — receipt assembly and certification (verify a witness against a format standard; never decide honesty).
//! COMBINATORIAL MAXIMALISM: Benchmarking Core
//!
//! Provides CLI-accessible performance measurement and variance analysis.

use crate::types::Receipt;
use anyhow::Result;
use std::time::{Duration, Instant};

/// Measure throughput: emit -> assemble -> verify latency.
pub fn bench_throughput(iterations: u32) -> Result<()> {
    let mut total_duration = Duration::ZERO;

    for i in 0..iterations {
        let start = Instant::now();

        // 1. Emit
        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)?;

        // 2. Assemble
        let receipt = asm.finalize();

        // 3. Verify
        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(())
}

/// Measure control-flow surprise for a specific receipt.
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 {
        // Compute metrics
        #[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(())
}
/// Run standard variance benchmark suite.
pub fn bench_variance_suite(_iterations: u32) -> Result<()> {
    eprintln!("Running standard variance suite...");
    // Implementation would iterate through fixtures
    Ok(())
}

/// Run sustained workload for profiling.
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 {
        // High-intensity work loop
        let _ = bench_throughput(1)?;
        count += 1;
    }

    eprintln!("Profile workload complete. Executed {} cycles.", count);
    Ok(())
}