Skip to content

Commit 4a88dde

Browse files
jasnellMylesBorins
authored andcommitted
perf_hooks: introduce createHistogram
Adds a new `perf_hooks.createHistogram()` API for creating histogram instances that allow user recording. Makes Histogram instances cloneable via MessagePort. This allows, for instance, an event loop delay monitor to be running on the main thread while the histogram data can be monitored actively from a worker thread. Signed-off-by: James M Snell <jasnell@gmail.com> PR-URL: #37155 Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
1 parent 32de361 commit 4a88dde

15 files changed

+774
-322
lines changed

doc/api/perf_hooks.md

+78-36
Original file line numberDiff line numberDiff line change
@@ -651,6 +651,22 @@ performance.mark('test');
651651
performance.mark('meow');
652652
```
653653

654+
## `perf_hooks.createHistogram([options])`
655+
<!-- YAML
656+
added: REPLACEME
657+
-->
658+
659+
* `options` {Object}
660+
* `min` {number|bigint} The minimum recordable value. Must be an integer
661+
value greater than 0. **Defaults**: `1`.
662+
* `max` {number|bigint} The maximum recordable value. Must be an integer
663+
value greater than `min`. **Defaults**: `Number.MAX_SAFE_INTEGER`.
664+
* `figures` {number} The number of accuracy digits. Must be a number between
665+
`1` and `5`. **Defaults**: `3`.
666+
* Returns {RecordableHistogram}
667+
668+
Returns a {RecordableHistogram}.
669+
654670
## `perf_hooks.monitorEventLoopDelay([options])`
655671
<!-- YAML
656672
added: v11.10.0
@@ -659,12 +675,12 @@ added: v11.10.0
659675
* `options` {Object}
660676
* `resolution` {number} The sampling rate in milliseconds. Must be greater
661677
than zero. **Default:** `10`.
662-
* Returns: {Histogram}
678+
* Returns: {IntervalHistogram}
663679

664680
_This property is an extension by Node.js. It is not available in Web browsers._
665681

666-
Creates a `Histogram` object that samples and reports the event loop delay
667-
over time. The delays will be reported in nanoseconds.
682+
Creates an `IntervalHistogram` object that samples and reports the event loop
683+
delay over time. The delays will be reported in nanoseconds.
668684

669685
Using a timer to detect approximate event loop delay works because the
670686
execution of timers is tied specifically to the lifecycle of the libuv
@@ -687,36 +703,12 @@ console.log(h.percentile(50));
687703
console.log(h.percentile(99));
688704
```
689705

690-
### Class: `Histogram`
691-
<!-- YAML
692-
added: v11.10.0
693-
-->
694-
Tracks the event loop delay at a given sampling rate. The constructor of
695-
this class not exposed to users.
696-
697-
_This property is an extension by Node.js. It is not available in Web browsers._
698-
699-
#### `histogram.disable()`
700-
<!-- YAML
701-
added: v11.10.0
702-
-->
703-
704-
* Returns: {boolean}
705-
706-
Disables the event loop delay sample timer. Returns `true` if the timer was
707-
stopped, `false` if it was already stopped.
708-
709-
#### `histogram.enable()`
706+
## Class: `Histogram`
710707
<!-- YAML
711708
added: v11.10.0
712709
-->
713710

714-
* Returns: {boolean}
715-
716-
Enables the event loop delay sample timer. Returns `true` if the timer was
717-
started, `false` if it was already started.
718-
719-
#### `histogram.exceeds`
711+
### `histogram.exceeds`
720712
<!-- YAML
721713
added: v11.10.0
722714
-->
@@ -726,7 +718,7 @@ added: v11.10.0
726718
The number of times the event loop delay exceeded the maximum 1 hour event
727719
loop delay threshold.
728720

729-
#### `histogram.max`
721+
### `histogram.max`
730722
<!-- YAML
731723
added: v11.10.0
732724
-->
@@ -735,7 +727,7 @@ added: v11.10.0
735727

736728
The maximum recorded event loop delay.
737729

738-
#### `histogram.mean`
730+
### `histogram.mean`
739731
<!-- YAML
740732
added: v11.10.0
741733
-->
@@ -744,7 +736,7 @@ added: v11.10.0
744736

745737
The mean of the recorded event loop delays.
746738

747-
#### `histogram.min`
739+
### `histogram.min`
748740
<!-- YAML
749741
added: v11.10.0
750742
-->
@@ -753,7 +745,7 @@ added: v11.10.0
753745

754746
The minimum recorded event loop delay.
755747

756-
#### `histogram.percentile(percentile)`
748+
### `histogram.percentile(percentile)`
757749
<!-- YAML
758750
added: v11.10.0
759751
-->
@@ -763,7 +755,7 @@ added: v11.10.0
763755

764756
Returns the value at the given percentile.
765757

766-
#### `histogram.percentiles`
758+
### `histogram.percentiles`
767759
<!-- YAML
768760
added: v11.10.0
769761
-->
@@ -772,14 +764,14 @@ added: v11.10.0
772764

773765
Returns a `Map` object detailing the accumulated percentile distribution.
774766

775-
#### `histogram.reset()`
767+
### `histogram.reset()`
776768
<!-- YAML
777769
added: v11.10.0
778770
-->
779771

780772
Resets the collected histogram data.
781773

782-
#### `histogram.stddev`
774+
### `histogram.stddev`
783775
<!-- YAML
784776
added: v11.10.0
785777
-->
@@ -788,6 +780,56 @@ added: v11.10.0
788780

789781
The standard deviation of the recorded event loop delays.
790782

783+
## Class: `IntervalHistogram extends Histogram`
784+
785+
A `Histogram` that is periodically updated on a given interval.
786+
787+
### `histogram.disable()`
788+
<!-- YAML
789+
added: v11.10.0
790+
-->
791+
792+
* Returns: {boolean}
793+
794+
Disables the update interval timer. Returns `true` if the timer was
795+
stopped, `false` if it was already stopped.
796+
797+
### `histogram.enable()`
798+
<!-- YAML
799+
added: v11.10.0
800+
-->
801+
802+
* Returns: {boolean}
803+
804+
Enables the update interval timer. Returns `true` if the timer was
805+
started, `false` if it was already started.
806+
807+
### Cloning an `IntervalHistogram`
808+
809+
{IntervalHistogram} instances can be cloned via {MessagePort}. On the receiving
810+
end, the histogram is cloned as a plain {Histogram} object that does not
811+
implement the `enable()` and `disable()` methods.
812+
813+
## Class: `RecordableHistogram extends Histogram`
814+
<!-- YAML
815+
added: REPLACEME
816+
-->
817+
818+
### `histogram.record(val)`
819+
<!-- YAML
820+
added: REPLACEME
821+
-->
822+
823+
* `val` {number|bigint} The amount to record in the histogram.
824+
825+
### `histogram.recordDelta()`
826+
<!-- YAML
827+
added: REPLACEME
828+
-->
829+
830+
Calculates the amount of time (in nanoseconds) that has passed since the
831+
previous call to `recordDelta()` and records that amount in the histogram.
832+
791833
## Examples
792834

793835
### Measuring the duration of async operations

doc/api/worker_threads.md

+4
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,9 @@ changes:
381381
- version: REPLACEME
382382
pr-url: https://github.com/nodejs/node/pull/37917
383383
description: Add 'BlockList' to the list of cloneable types.
384+
- version: REPLACEME
385+
pr-url: https://github.com/nodejs/node/pull/37155
386+
description: Add 'Histogram' types to the list of cloneable types.
384387
- version: v14.5.0
385388
pr-url: https://github.com/nodejs/node/pull/33360
386389
description: Added `KeyObject` to the list of cloneable types.
@@ -406,6 +409,7 @@ In particular, the significant differences to `JSON` are:
406409
* `value` may contain [`WebAssembly.Module`][] instances.
407410
* `value` may not contain native (C++-backed) objects other than:
408411
* {FileHandle}s,
412+
* {Histogram}s,
409413
* {KeyObject}s,
410414
* {MessagePort}s,
411415
* {net.BlockList}s,

0 commit comments

Comments
 (0)