use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::LazyLock;
#[derive(Debug)]
pub struct Metrics {
pub rough: AtomicU64,
pub precise: AtomicU64,
pub query: AtomicU64,
pub miss: AtomicU64,
}
impl Metrics {
fn new() -> Self {
Self {
rough: AtomicU64::new(0),
precise: AtomicU64::new(0),
query: AtomicU64::new(0),
miss: AtomicU64::new(0),
}
}
pub fn to_str(&self) -> String {
let rough = self.rough.load(Ordering::Relaxed);
let precise = self.precise.load(Ordering::Relaxed);
format!(
"query: {}, rough: {}, precise: {}, ratio: {:.2}, cache miss: {}",
self.query.load(Ordering::Relaxed),
rough,
precise,
rough as f64 / precise as f64,
self.miss.load(Ordering::Relaxed),
)
}
pub fn add_rough_count(&self, count: u64) {
self.rough.fetch_add(count, Ordering::Relaxed);
}
pub fn add_precise_count(&self, count: u64) {
self.precise.fetch_add(count, Ordering::Relaxed);
}
pub fn add_query_count(&self, count: u64) {
self.query.fetch_add(count, Ordering::Relaxed);
}
pub fn add_cache_miss_count(&self, count: u64) {
self.miss.fetch_add(count, Ordering::Relaxed);
}
}
pub static METRICS: LazyLock<Metrics> = LazyLock::new(Metrics::new);