-
Notifications
You must be signed in to change notification settings - Fork 29.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf,src: add HistogramBase and internal/histogram.js
Separating this out from the QUIC PR to allow it to be separately reviewed. The QUIC implementation makes use of the hdr_histogram for dynamic performance monitoring. This introduces a BaseObject class that allows the internal histograms to be accessed on the JavaScript side and adds a generic Histogram class that will be used by both QUIC and perf_hooks (for the event loop delay monitoring). Signed-off-by: James M Snell <jasnell@gmail.com> PR-URL: #31988 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
- Loading branch information
Showing
8 changed files
with
355 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
'use strict'; | ||
|
||
const { | ||
customInspectSymbol: kInspect, | ||
} = require('internal/util'); | ||
|
||
const { format } = require('util'); | ||
const { Map, Symbol } = primordials; | ||
|
||
const { | ||
ERR_INVALID_ARG_TYPE, | ||
ERR_INVALID_ARG_VALUE, | ||
} = require('internal/errors').codes; | ||
|
||
const kDestroy = Symbol('kDestroy'); | ||
const kHandle = Symbol('kHandle'); | ||
|
||
// Histograms are created internally by Node.js and used to | ||
// record various metrics. This Histogram class provides a | ||
// generally read-only view of the internal histogram. | ||
class Histogram { | ||
#handle = undefined; | ||
#map = new Map(); | ||
|
||
constructor(internal) { | ||
this.#handle = internal; | ||
} | ||
|
||
[kInspect]() { | ||
const obj = { | ||
min: this.min, | ||
max: this.max, | ||
mean: this.mean, | ||
exceeds: this.exceeds, | ||
stddev: this.stddev, | ||
percentiles: this.percentiles, | ||
}; | ||
return `Histogram ${format(obj)}`; | ||
} | ||
|
||
get min() { | ||
return this.#handle ? this.#handle.min() : undefined; | ||
} | ||
|
||
get max() { | ||
return this.#handle ? this.#handle.max() : undefined; | ||
} | ||
|
||
get mean() { | ||
return this.#handle ? this.#handle.mean() : undefined; | ||
} | ||
|
||
get exceeds() { | ||
return this.#handle ? this.#handle.exceeds() : undefined; | ||
} | ||
|
||
get stddev() { | ||
return this.#handle ? this.#handle.stddev() : undefined; | ||
} | ||
|
||
percentile(percentile) { | ||
if (typeof percentile !== 'number') | ||
throw new ERR_INVALID_ARG_TYPE('percentile', 'number', percentile); | ||
|
||
if (percentile <= 0 || percentile > 100) | ||
throw new ERR_INVALID_ARG_VALUE.RangeError('percentile', percentile); | ||
|
||
return this.#handle ? this.#handle.percentile(percentile) : undefined; | ||
} | ||
|
||
get percentiles() { | ||
this.#map.clear(); | ||
if (this.#handle) | ||
this.#handle.percentiles(this.#map); | ||
return this.#map; | ||
} | ||
|
||
reset() { | ||
if (this.#handle) | ||
this.#handle.reset(); | ||
} | ||
|
||
[kDestroy]() { | ||
this.#handle = undefined; | ||
} | ||
|
||
get [kHandle]() { return this.#handle; } | ||
} | ||
|
||
module.exports = { | ||
Histogram, | ||
kDestroy, | ||
kHandle, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.