116 lines
3.6 KiB
Rust
116 lines
3.6 KiB
Rust
|
|
|
|
use std::sync::atomic::{AtomicU32, Ordering::Relaxed};
|
|
use std::sync::{Mutex, OnceLock};
|
|
|
|
struct Asked {
|
|
seconds: f64,
|
|
switches: Vec<String>,
|
|
}
|
|
|
|
fn parse(text: &str) -> Option<Asked> {
|
|
let mut parts = text.split(',').map(str::trim);
|
|
let seconds = parts.next()?.parse::<f64>().ok().filter(|s| *s > 1.0)?;
|
|
let switches: Vec<String> = parts.filter(|s| !s.is_empty()).map(str::to_string).collect();
|
|
(!switches.is_empty() && switches.len() <= 8).then_some(Asked { seconds, switches })
|
|
}
|
|
|
|
fn asked() -> Option<&'static Asked> {
|
|
static ASKED: OnceLock<Option<Asked>> = OnceLock::new();
|
|
ASKED.get_or_init(|| parse(&std::env::var("EONACAT_XS_AB").ok()?)).as_ref()
|
|
}
|
|
|
|
static BITS: AtomicU32 = AtomicU32::new(0);
|
|
|
|
pub(crate) fn pick(switch: &str) -> Option<bool> {
|
|
bit(switch).map(on)
|
|
}
|
|
|
|
pub(crate) fn bit(switch: &str) -> Option<u32> {
|
|
Some(asked()?.switches.iter().position(|s| s == switch)? as u32)
|
|
}
|
|
|
|
pub(crate) fn on(bit: u32) -> bool {
|
|
BITS.load(Relaxed) >> bit & 1 != 0
|
|
}
|
|
|
|
struct Phase {
|
|
started: std::time::Instant,
|
|
number: u64,
|
|
began: std::time::Instant,
|
|
last: std::time::Instant,
|
|
drawn: u64,
|
|
frames: Vec<f32>,
|
|
draws: u64,
|
|
}
|
|
|
|
fn summary(frames: &mut [f32]) -> (f32, f32, f32) {
|
|
if frames.is_empty() {
|
|
return (0.0, 0.0, 0.0);
|
|
}
|
|
let mean = frames.iter().sum::<f32>() / frames.len() as f32;
|
|
frames.sort_by(f32::total_cmp);
|
|
(mean, frames[frames.len() / 2], frames[frames.len() * 9 / 10])
|
|
}
|
|
|
|
pub(crate) fn presented(change: impl Fn(&str, bool, bool)) {
|
|
let Some(asked) = asked() else {
|
|
return;
|
|
};
|
|
static PHASE: Mutex<Option<Phase>> = Mutex::new(None);
|
|
let now = std::time::Instant::now();
|
|
let drawn = crate::draw::drawn();
|
|
let mut phase = PHASE.lock().unwrap_or_else(|e| e.into_inner());
|
|
let p = phase.get_or_insert_with(|| Phase {
|
|
started: now,
|
|
number: 0,
|
|
began: now,
|
|
last: now,
|
|
drawn,
|
|
frames: Vec::new(),
|
|
draws: 0,
|
|
});
|
|
if (now - p.began).as_secs_f64() > 1.0 {
|
|
p.frames.push((now - p.last).as_secs_f32() * 1000.0);
|
|
p.draws += drawn.saturating_sub(p.drawn);
|
|
}
|
|
(p.last, p.drawn) = (now, drawn);
|
|
let number = ((now - p.started).as_secs_f64() / asked.seconds) as u64;
|
|
if number == p.number {
|
|
return;
|
|
}
|
|
let bits = |number: u64| (number % (1 << asked.switches.len())) as u32;
|
|
let (was, will) = (bits(p.number), bits(number));
|
|
let (mean, median, most) = summary(&mut p.frames);
|
|
let on: Vec<&str> = asked.switches.iter().enumerate().filter(|(n, _)| was >> n & 1 != 0).map(|(_, s)| s.as_str()).collect();
|
|
tracing::info!(
|
|
"A/B phase {} bits {was} on [{}]: {} frames, mean {mean:.1} ms, median {median:.1}, p90 {most:.1}, {:.0} draws a frame; the next begins after frame {}",
|
|
p.number,
|
|
on.join(" "),
|
|
p.frames.len(),
|
|
p.draws as f64 / p.frames.len().max(1) as f64,
|
|
crate::queue::presented()
|
|
);
|
|
for (n, switch) in asked.switches.iter().enumerate() {
|
|
change(switch, was >> n & 1 != 0, will >> n & 1 != 0);
|
|
}
|
|
BITS.store(will, Relaxed);
|
|
(p.number, p.began, p.draws) = (number, now, 0);
|
|
p.frames.clear();
|
|
drop(phase);
|
|
|
|
let hooks = HOOKS.lock().unwrap_or_else(|e| e.into_inner()).clone();
|
|
for (n, switch) in asked.switches.iter().enumerate() {
|
|
if was >> n & 1 != will >> n & 1 {
|
|
hooks.iter().for_each(|hook| hook(switch, will >> n & 1 != 0));
|
|
}
|
|
}
|
|
}
|
|
|
|
static HOOKS: Mutex<Vec<fn(&str, bool)>> = Mutex::new(Vec::new());
|
|
|
|
pub(crate) fn on_change(hook: fn(&str, bool)) {
|
|
HOOKS.lock().unwrap_or_else(|e| e.into_inner()).push(hook);
|
|
}
|
|
|