Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

3.1 KB
Raw Blame
use crate::stats::{StatsCollector, StatsSnapshot};

fn format_us(us: u64) -> String {
    if us < 1_000 {
        format!("{us}us")
    } else if us < 1_000_000 {
        format!("{:.1}ms", us as f64 / 1_000.0)
    } else {
        format!("{:.2}s", us as f64 / 1_000_000.0)
    }
}

fn print_stats_row(name: &str, snap: &StatsSnapshot) {
    println!(
        "  {:<10} {:>8} {:>8} {:>7} {:>8} {:>8} {:>8} {:>9.1}",
        name,
        snap.total,
        snap.successes,
        snap.failures,
        format_us(snap.p50_us),
        format_us(snap.p95_us),
        format_us(snap.p99_us),
        snap.throughput_rps,
    );
}

pub fn print_report(
    title: &str,
    collector: &StatsCollector,
    team_id: i64,
    num_persons: usize,
    violations: &[ConsistencyViolation],
) {
    let write_snap = collector.writes.snapshot();
    let read_snap = collector.reads.snapshot();
    let elapsed = write_snap.elapsed.max(read_snap.elapsed);

    println!();
    println!("=== personhog-test-harness {title} results ===");
    println!(
        "  Duration: {:.2}s | Team: {} | Persons: {}",
        elapsed.as_secs_f64(),
        team_id,
        num_persons
    );
    println!();
    println!(
        "  {:<10} {:>8} {:>8} {:>7} {:>8} {:>8} {:>8} {:>9}",
        "Operation", "Total", "Success", "Failed", "p50", "p95", "p99", "RPS"
    );
    if write_snap.total > 0 {
        print_stats_row("writes", &write_snap);
    }
    if read_snap.total > 0 {
        print_stats_row("reads", &read_snap);
    }
    let merge_snap = collector.merges.snapshot();
    if merge_snap.total > 0 {
        print_stats_row("merges", &merge_snap);
    }
    let wide_snap = collector.wide_merges.snapshot();
    if wide_snap.total > 0 {
        print_stats_row("merges_wide", &wide_snap);
    }
    println!();
    let rejected = write_snap.lifecycle_rejections + read_snap.lifecycle_rejections;
    if rejected > 0 {
        println!(
            "  Lifecycle rejections (counted in Total, not Failed): {rejected} writes refused \
             for a fenced or destroyed person"
        );
    }
    let outcomes = collector.merge_outcomes();
    if !outcomes.is_empty() {
        let rendered: Vec<String> = outcomes
            .iter()
            .map(|(outcome, count)| format!("{outcome}={count}"))
            .collect();
        println!("  Merge outcomes per source: {}", rendered.join(" "));
    }
    if rejected > 0 || !outcomes.is_empty() {
        println!();
    }

    if violations.is_empty() {
        println!("  Consistency violations: 0");
    } else {
        println!("  Consistency violations: {}", violations.len());
        println!();
        for v in violations.iter().take(20) {
            println!(
                "    person_id={} key={:?} expected={} actual={}",
                v.person_id, v.key, v.expected, v.actual,
            );
        }
        if violations.len() > 20 {
            println!("    ... and {} more", violations.len() - 20);
        }
    }
    println!();
}

pub struct ConsistencyViolation {
    pub person_id: i64,
    pub key: String,
    pub expected: serde_json::Value,
    pub actual: serde_json::Value,
}