use std::cell::{Cell, RefCell};
use std::rc::Rc;
use std::sync::Arc;
use std::sync::atomic::{AtomicU64, Ordering};
use std::time::{Duration, Instant};
use objc2_app_kit::NSApplication;
pub struct Rng(u64);
impl Rng {
pub fn new(seed: u64) -> Rng {
Rng(seed.max(1))
}
pub fn below(&mut self, n: usize) -> usize {
self.0 ^= self.0 << 13;
self.0 ^= self.0 >> 7;
self.0 ^= self.0 << 17;
if n == 0 { 0 } else { (self.0 % n as u64) as usize }
}
pub fn chance(&mut self, p: f64) -> bool {
self.below(10_000) < (p * 10_000.0) as usize
}
pub fn pick<'a, T>(&mut self, items: &'a [T]) -> &'a T {
&items[self.below(items.len())]
}
}
pub struct Action {
name: &'static str,
weight: u32,
run: Rc<dyn Fn(&mut Rng)>,
}
pub fn action(name: &'static str, weight: u32, run: impl Fn(&mut Rng) + 'static) -> Action {
Action { name, weight, run: Rc::new(run) }
}
#[derive(Debug, Clone, Copy)]
pub struct Settings {
pub duration: Duration,
pub interval: Duration,
pub seed: u64,
}
impl Settings {
pub fn from_env() -> Settings {
let env = |k: &str| std::env::var(k).ok().and_then(|v| v.parse::<f64>().ok());
let seed = std::env::var("SOAK_SEED").ok().and_then(|v| v.trim().parse::<u64>().ok()).unwrap_or_else(|| {
std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).map(|d| d.as_nanos() as u64).unwrap_or(1)
});
Settings {
duration: Duration::from_secs_f64(env("SOAK").unwrap_or(10.0) * 60.0),
interval: Duration::from_millis(env("SOAK_MS").unwrap_or(120.0) as u64),
seed,
}
}
}
type Sample = (u64, f64, usize, usize);
struct State {
rng: Rng,
actions: Vec<Action>,
total: u32,
done: u64,
paused: u64,
recent: Vec<&'static str>,
start: Instant,
settings: Settings,
samples: Vec<Sample>,
first: (usize, usize),
last: (usize, usize),
hangs: Rc<Cell<u32>>,
}
pub fn run(settings: Settings, actions: Vec<Action>) {
let actions = match std::env::var("SOAK_ONLY") {
Ok(only) => actions.into_iter().filter(|a| a.name == only).collect(),
Err(_) => actions,
};
let total = actions.iter().map(|a| a.weight).sum::<u32>().max(1);
eprintln!(
"[soak] start seed {} for {:.0} min, an action every {} ms, {} kinds",
settings.seed,
settings.duration.as_secs_f64() / 60.0,
settings.interval.as_millis(),
actions.len()
);
let beat = Arc::new(AtomicU64::new(now_ms()));
let hangs = Rc::new(Cell::new(0));
{
let beat = beat.clone();
std::thread::spawn(move || {
let mut reported = 0u64;
loop {
std::thread::sleep(Duration::from_millis(500));
let gap = now_ms().saturating_sub(beat.load(Ordering::Relaxed));
if gap > 2000 && gap / 1000 > reported {
reported = gap / 1000;
eprintln!("[soak] hang {:.1} s (main thread not answering)", gap as f64 / 1000.0);
} else if gap <= 2000 {
reported = 0;
}
}
});
}
let app = NSApplication::sharedApplication(crate::mtm());
#[allow(deprecated)]
app.activateIgnoringOtherApps(true);
if let Some(w) = crate::windows().into_iter().rev().find(|w| w.isVisible()) {
w.makeKeyAndOrderFront(None);
}
let counts = (crate::testing::live_nodes(), crate::testing::live_signals());
let state = Rc::new(RefCell::new(State {
rng: Rng::new(settings.seed),
actions,
total,
done: 0,
paused: 0,
recent: Vec::new(),
start: Instant::now(),
settings,
samples: Vec::new(),
first: counts,
last: counts,
hangs: hangs.clone(),
}));
heartbeat(beat, hangs);
sample(state.clone());
step(state);
}
fn now_ms() -> u64 {
std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).map(|d| d.as_millis() as u64).unwrap_or(0)
}
fn heartbeat(beat: Arc<AtomicU64>, hangs: Rc<Cell<u32>>) {
let last = beat.swap(now_ms(), Ordering::Relaxed);
if now_ms().saturating_sub(last) > 2000 {
hangs.set(hangs.get() + 1);
}
crate::spawn(|| std::thread::sleep(Duration::from_millis(250)), move |()| heartbeat(beat, hangs));
}
fn sample(state: Rc<RefCell<State>>) {
crate::spawn(super::footprint_mb, move |mb| {
let mut s = state.borrow_mut();
let minutes = s.start.elapsed().as_secs_f64() / 60.0;
s.last = (crate::testing::live_nodes(), crate::testing::live_signals());
let (done, last) = (s.done, s.last);
s.samples.push((done, mb, last.0, last.1));
eprintln!("[soak] sample {minutes:.1} min footprint {mb:.0} MB nodes {} signals {} windows {} actions {}", s.last.0, s.last.1, crate::windows().len(), s.done);
drop(s);
crate::spawn(|| std::thread::sleep(Duration::from_secs(30)), move |()| sample(state));
});
}
fn step(state: Rc<RefCell<State>>) {
let (interval, over) = {
let s = state.borrow();
(s.settings.interval, s.start.elapsed() >= s.settings.duration)
};
if over {
return finish(&state);
}
let active = NSApplication::sharedApplication(crate::mtm()).isActive();
if active {
let (run, mut rng) = {
let mut s = state.borrow_mut();
let total = s.total as usize;
let mut r = s.rng.below(total) as u32;
let index = s.actions.iter().position(|a| {
if r < a.weight {
return true;
}
r -= a.weight;
false
});
let index = index.unwrap_or(0);
let name = s.actions[index].name;
s.done += 1;
s.recent.push(name);
if s.recent.len() > 20 {
s.recent.remove(0);
}
if s.done.is_multiple_of(200) {
eprintln!("[soak] action {} {name}", s.done);
}
let seed = s.rng.below(usize::MAX) as u64 + 1;
(s.actions[index].run.clone(), Rng::new(seed))
};
run(&mut rng);
} else {
let mut s = state.borrow_mut();
if s.paused.is_multiple_of(250) {
eprintln!("[soak] paused: another app is in front (windows: {:?})", crate::windows().iter().filter(|w| w.isVisible()).map(|w| w.title().to_string()).collect::<Vec<_>>());
}
s.paused += 1;
}
crate::spawn(move || std::thread::sleep(interval), move |()| step(state));
}
fn finish(state: &Rc<RefCell<State>>) {
let s = state.borrow();
let half = s.done / 2;
let late: Vec<_> = s.samples.iter().filter(|x| x.0 >= half).collect();
let per_k = |f: &dyn Fn(&Sample) -> f64| match (late.first(), late.last()) {
(Some(a), Some(b)) if b.0 > a.0 => (f(b) - f(a)) / (b.0 - a.0) as f64 * 1000.0,
_ => 0.0,
};
let (mb_k, nodes_k, signals_k) = (per_k(&|x| x.1), per_k(&|x| x.2 as f64), per_k(&|x| x.3 as f64));
let peak = s.samples.iter().map(|x| x.1).fold(0.0, f64::max);
eprintln!(
"[soak] growth per 1000 actions over the second half: footprint {mb_k:.1} MB, nodes {nodes_k:.1}, signals {signals_k:.1}"
);
eprintln!(
"[soak] done {} actions in {:.1} min ({} ticks paused); hangs {}; footprint peak {peak:.0} MB; nodes {} → {}, signals {} → {}",
s.done,
s.start.elapsed().as_secs_f64() / 60.0,
s.paused,
s.hangs.get(),
s.first.0,
s.last.0,
s.first.1,
s.last.1
);
eprintln!("[soak] last actions: {}", s.recent.join(", "));
let code = if s.hangs.get() == 0 { 0 } else { 1 };
drop(s);
std::process::exit(code);
}