use std::collections::BTreeMap;
use std::sync::Mutex;
use std::sync::atomic::{AtomicBool, Ordering};
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, serde::Serialize)]
pub struct OperationCount {
pub calls: u64,
pub input_bytes: u64,
pub output_bytes: u64,
}
static ENABLED: AtomicBool = AtomicBool::new(false);
static COUNTS: Mutex<BTreeMap<String, OperationCount>> = Mutex::new(BTreeMap::new());
#[cfg_attr(
not(any(
feature = "boa",
feature = "quickjs",
feature = "v8",
feature = "fuzzing"
)),
allow(dead_code)
)]
pub(crate) fn record(name: &str, input_bytes: usize, output_bytes: usize) {
if !ENABLED.load(Ordering::Relaxed) {
return;
}
let mut counts = COUNTS
.lock()
.unwrap_or_else(|poisoned| poisoned.into_inner());
let count = counts.entry(name.to_owned()).or_default();
count.calls += 1;
count.input_bytes += input_bytes as u64;
count.output_bytes += output_bytes as u64;
}
pub fn start_operation_counts() {
take_operation_counts();
ENABLED.store(true, Ordering::Relaxed);
}
pub fn take_operation_counts() -> BTreeMap<String, OperationCount> {
let mut counts = COUNTS
.lock()
.unwrap_or_else(|poisoned| poisoned.into_inner());
std::mem::take(&mut *counts)
}