Skip to content

perf_hooks: make the event loop display histogram disposable #58384

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions doc/api/perf_hooks.md
Original file line number Diff line number Diff line change
Expand Up @@ -1973,6 +1973,23 @@ added: v11.10.0
Enables the update interval timer. Returns `true` if the timer was
started, `false` if it was already started.

### `histogram[Symbol.dispose]()`

<!-- YAML
added: REPLACEME
-->

Disables the update interval timer when the histogram is disposed.

```js
const { monitorEventLoopDelay } = require('node:perf_hooks');
{
using hist = monitorEventLoopDelay({ resolution: 20 });
hist.enable();
// The histogram will be disabled when the block is exited.
}
```

### Cloning an `IntervalHistogram`

{IntervalHistogram} instances can be cloned via {MessagePort}. On the receiving
Expand Down
7 changes: 6 additions & 1 deletion lib/internal/perf/event_loop_delay.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const {
ReflectConstruct,
SafeMap,
Symbol,
SymbolDispose,
} = primordials;

const {
Expand Down Expand Up @@ -38,7 +39,7 @@ const {
const kEnabled = Symbol('kEnabled');

class ELDHistogram extends Histogram {
constructor(i) {
constructor() {
throw new ERR_ILLEGAL_CONSTRUCTOR();
}

Expand All @@ -65,6 +66,10 @@ class ELDHistogram extends Histogram {
this[kHandle].stop();
return true;
}

[SymbolDispose]() {
this.disable();
}
}

/**
Expand Down
11 changes: 11 additions & 0 deletions test/parallel/test-perf-hooks-histogram.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,17 @@ const { inspect } = require('util');
}, 50);
}

{
// Tests that the ELD histogram is disposable
let histogram;
{
using hi = monitorEventLoopDelay();
histogram = hi;
}
// The histogram should already be disabled.
strictEqual(histogram.disable(), false);
}

{
const h = createHistogram();
ok(inspect(h, { depth: null }).startsWith('Histogram'));
Expand Down
Loading