Skip to content

Commit 0f65fed

Browse files
committed
perf_hooks: make the event loop display histogram disposable
1 parent 20c4b80 commit 0f65fed

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed

doc/api/perf_hooks.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1973,6 +1973,23 @@ added: v11.10.0
19731973
Enables the update interval timer. Returns `true` if the timer was
19741974
started, `false` if it was already started.
19751975

1976+
### `histogram[Symbol.dispose]()`
1977+
1978+
<!-- YAML
1979+
added: REPLACEME
1980+
-->
1981+
1982+
Disables the update interval timer when the histogram is disposed.
1983+
1984+
```js
1985+
const { monitorEventLoopDelay } = require('node:perf_hooks');
1986+
{
1987+
using hist = monitorEventLoopDelay({ resolution: 20 });
1988+
hist.enable();
1989+
// The histogram will be disabled when the block is exited.
1990+
}
1991+
```
1992+
19761993
### Cloning an `IntervalHistogram`
19771994

19781995
{IntervalHistogram} instances can be cloned via {MessagePort}. On the receiving

lib/internal/perf/event_loop_delay.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const {
33
ReflectConstruct,
44
SafeMap,
55
Symbol,
6+
SymbolDispose,
67
} = primordials;
78

89
const {
@@ -38,7 +39,7 @@ const {
3839
const kEnabled = Symbol('kEnabled');
3940

4041
class ELDHistogram extends Histogram {
41-
constructor(i) {
42+
constructor() {
4243
throw new ERR_ILLEGAL_CONSTRUCTOR();
4344
}
4445

@@ -65,6 +66,10 @@ class ELDHistogram extends Histogram {
6566
this[kHandle].stop();
6667
return true;
6768
}
69+
70+
[SymbolDispose]() {
71+
this.disable();
72+
}
6873
}
6974

7075
/**

test/parallel/test-perf-hooks-histogram.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,17 @@ const { inspect } = require('util');
9797
}, 50);
9898
}
9999

100+
{
101+
// Tests that the ELD histogram is disposable
102+
let histogram;
103+
{
104+
using hi = monitorEventLoopDelay();
105+
histogram = hi;
106+
}
107+
// The histogram should already be disabled.
108+
strictEqual(histogram.disable(), false);
109+
}
110+
100111
{
101112
const h = createHistogram();
102113
ok(inspect(h, { depth: null }).startsWith('Histogram'));

0 commit comments

Comments
 (0)