use std::cell::Cell;
use std::cell::RefCell;
use deno_core::GarbageCollected;
use deno_core::op2;
#[derive(Debug, thiserror::Error, deno_error::JsError)]
pub enum PerfHooksError {
#[class(generic)]
#[error(transparent)]
TokioEld(#[from] tokio_eld::Error),
}
pub struct EldHistogram {
eld: RefCell<tokio_eld::EldHistogram<u64>>,
started: Cell<bool>,
}
unsafe impl GarbageCollected for EldHistogram {
fn trace(&self, _visitor: &mut deno_core::v8::cppgc::Visitor) {}
fn get_name(&self) -> &'static std::ffi::CStr {
c"EldHistogram"
}
}
#[op2]
impl EldHistogram {
#[constructor]
#[cppgc]
pub fn new(#[smi] resolution: u32) -> Result<EldHistogram, PerfHooksError> {
Ok(EldHistogram {
eld: RefCell::new(tokio_eld::EldHistogram::new(resolution as usize)?),
started: Cell::new(false),
})
}
#[fast]
fn enable(&self) -> bool {
if self.started.get() {
return false;
}
self.eld.borrow().start();
self.started.set(true);
true
}
#[fast]
fn disable(&self) -> bool {
if !self.started.get() {
return false;
}
self.eld.borrow().stop();
self.started.set(false);
true
}
#[fast]
fn reset(&self) {
self.eld.borrow_mut().reset();
}
#[fast]
#[number]
fn percentile(&self, percentile: f64) -> u64 {
self.eld.borrow().value_at_percentile(percentile)
}
#[fast]
#[bigint]
fn percentile_big_int(&self, percentile: f64) -> u64 {
self.eld.borrow().value_at_percentile(percentile)
}
#[getter]
#[number]
fn count(&self) -> u64 {
self.eld.borrow().len()
}
#[getter]
#[bigint]
fn count_big_int(&self) -> u64 {
self.eld.borrow().len()
}
#[getter]
#[number]
fn max(&self) -> u64 {
self.eld.borrow().max()
}
#[getter]
#[bigint]
fn max_big_int(&self) -> u64 {
self.eld.borrow().max()
}
#[getter]
fn mean(&self) -> f64 {
self.eld.borrow().mean()
}
#[getter]
#[number]
fn min(&self) -> u64 {
self.eld.borrow().min()
}
#[getter]
#[bigint]
fn min_big_int(&self) -> u64 {
self.eld.borrow().min()
}
#[getter]
fn stddev(&self) -> f64 {
self.eld.borrow().stdev()
}
}