forked from denoland/deno
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathperf_hooks.rs
135 lines (113 loc) · 2.76 KB
/
perf_hooks.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
use deno_core::op2;
use deno_core::GarbageCollected;
use std::cell::Cell;
#[derive(Debug, thiserror::Error)]
pub enum PerfHooksError {
#[error(transparent)]
TokioEld(#[from] tokio_eld::Error),
}
pub struct EldHistogram {
eld: tokio_eld::EldHistogram<u64>,
started: Cell<bool>,
}
impl GarbageCollected for EldHistogram {}
#[op2]
impl EldHistogram {
// Creates an interval EldHistogram object that samples and reports the event
// loop delay over time.
//
// The delays will be reported in nanoseconds.
#[constructor]
#[cppgc]
pub fn new(#[smi] resolution: u32) -> Result<EldHistogram, PerfHooksError> {
Ok(EldHistogram {
eld: tokio_eld::EldHistogram::new(resolution as usize)?,
started: Cell::new(false),
})
}
// Disables the update interval timer.
//
// Returns true if the timer was stopped, false if it was already stopped.
#[fast]
fn enable(&self) -> bool {
if self.started.get() {
return false;
}
self.eld.start();
self.started.set(true);
true
}
// Enables the update interval timer.
//
// Returns true if the timer was started, false if it was already started.
#[fast]
fn disable(&self) -> bool {
if !self.started.get() {
return false;
}
self.eld.stop();
self.started.set(false);
true
}
// Returns the value at the given percentile.
//
// `percentile` ∈ (0, 100]
#[fast]
#[number]
fn percentile(&self, percentile: f64) -> u64 {
self.eld.value_at_percentile(percentile)
}
// Returns the value at the given percentile as a bigint.
#[fast]
#[bigint]
fn percentile_big_int(&self, percentile: f64) -> u64 {
self.eld.value_at_percentile(percentile)
}
// The number of samples recorded by the histogram.
#[getter]
#[number]
fn count(&self) -> u64 {
self.eld.len()
}
// The number of samples recorded by the histogram as a bigint.
#[getter]
#[bigint]
fn count_big_int(&self) -> u64 {
self.eld.len()
}
// The maximum recorded event loop delay.
#[getter]
#[number]
fn max(&self) -> u64 {
self.eld.max()
}
// The maximum recorded event loop delay as a bigint.
#[getter]
#[bigint]
fn max_big_int(&self) -> u64 {
self.eld.max()
}
// The mean of the recorded event loop delays.
#[getter]
fn mean(&self) -> f64 {
self.eld.mean()
}
// The minimum recorded event loop delay.
#[getter]
#[number]
fn min(&self) -> u64 {
self.eld.min()
}
// The minimum recorded event loop delay as a bigint.
#[getter]
#[bigint]
fn min_big_int(&self) -> u64 {
self.eld.min()
}
// The standard deviation of the recorded event loop delays.
#[getter]
fn stddev(&self) -> f64 {
self.eld.stdev()
}
}