#![cfg(feature = "default-embedder")]
use std::sync::mpsc;
use std::sync::Arc;
use std::thread;
use std::time::{Duration, Instant};
use fathomdb_embedder::CandleBgeEmbedder;
use fathomdb_embedder_api::Embedder;
fn watchdog_embed(embedder: &Arc<dyn Embedder>, body: &str, timeout: Duration) -> Vec<f32> {
let (tx, rx) = mpsc::channel();
let e = Arc::clone(embedder);
let b = body.to_string();
thread::spawn(move || {
let _ = tx.send(e.embed(&b));
});
rx.recv_timeout(timeout).expect("recv").expect("embed ok")
}
fn mean_ms(times: &[Duration]) -> f64 {
times.iter().map(|d| d.as_secs_f64() * 1000.0).sum::<f64>() / times.len() as f64
}
#[test]
fn pr9_microbench_watchdog_overhead() {
if std::env::var_os("AGENT_LONG").is_none() {
eprintln!("[skip] AGENT_LONG not set; PR-9 micro-benchmark is opt-in");
return;
}
if std::env::var("FATHOMDB_SKIP_NETWORK_TESTS").is_ok() {
eprintln!("[skip] FATHOMDB_SKIP_NETWORK_TESTS set");
return;
}
let build = if cfg!(debug_assertions) { "debug" } else { "release" };
let embedder: Arc<dyn Embedder> =
Arc::new(CandleBgeEmbedder::new().expect("construct real bge embedder"));
let short = "the quick brown fox jumps over the lazy dog";
let long = short.repeat(80);
let _ = embedder.embed(short).expect("warmup");
const N: usize = 30;
let mut direct = Vec::with_capacity(N);
for _ in 0..N {
let t = Instant::now();
let _ = embedder.embed(short).expect("direct embed");
direct.push(t.elapsed());
}
let mut watchdog = Vec::with_capacity(N);
for _ in 0..N {
let t = Instant::now();
let _ = watchdog_embed(&embedder, short, Duration::from_secs(30));
watchdog.push(t.elapsed());
}
const LN: usize = 8;
let mut long_direct = Vec::with_capacity(LN);
for _ in 0..LN {
let t = Instant::now();
let _ = embedder.embed(&long).expect("long embed");
long_direct.push(t.elapsed());
}
let d = mean_ms(&direct);
let w = mean_ms(&watchdog);
let l = mean_ms(&long_direct);
eprintln!("PR9_BENCH build={build}");
eprintln!("PR9_BENCH short_direct_ms={d:.1} (n={N})");
eprintln!("PR9_BENCH short_watchdog_ms={w:.1} (n={N})");
eprintln!("PR9_BENCH watchdog_overhead_ms={:.2} ratio={:.3}", w - d, w / d);
eprintln!("PR9_BENCH long_direct_ms={l:.1} (n={LN}) long/short={:.1}x", l / d);
}