node/lib/internal/perf/perf.js
James M Snell f3eb224c83
perf_hooks: complete overhaul of the implementation
* Update the user timing implementation to conform to
  User Timing Level 3.
* Reimplement user timing and timerify with pure JavaScript
  implementations
* Simplify the C++ implementation for gc and http2 perf
* Runtime deprecate additional perf entry properties
  in favor of the standard detail argument
* Disable the `buffered` option on PerformanceObserver,
  all entries are queued and dispatched on setImmediate.
  Only entries with active observers are buffered.
* This does remove the user timing and timerify
  trace events. Because the trace_events are still
  considered experimental, those can be removed without
  a deprecation cycle. They are removed to improve
  performance and reduce complexity.

Old: `perf_hooks/usertiming.js n=100000: 92,378.01249733355`
New: perf_hooks/usertiming.js n=100000: 270,393.5280638482`

PR-URL: https://github.com/nodejs/node/pull/37136
Refs: https://github.com/nodejs/diagnostics/issues/464
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
2021-02-22 08:46:11 -08:00

95 lines
1.9 KiB
JavaScript

'use strict';
const {
ObjectSetPrototypeOf,
Symbol,
TypeError,
} = primordials;
const {
timeOrigin,
} = internalBinding('performance');
const {
customInspectSymbol: kInspect,
} = require('internal/util');
const { inspect } = require('util');
const kName = Symbol('kName');
const kType = Symbol('kType');
const kStart = Symbol('kStart');
const kDuration = Symbol('kDuration');
const kDetail = Symbol('kDetail');
const kReadOnlyAttributes = Symbol('kReadOnlyAttributes');
function now() {
const hr = process.hrtime();
return (hr[0] * 1000 + hr[1] / 1e6) - timeOrigin;
}
function isPerformanceEntry(obj) {
return obj?.[kName] !== undefined;
}
class PerformanceEntry {
constructor() {
// eslint-disable-next-line no-restricted-syntax
throw new TypeError('illegal constructor');
}
get name() { return this[kName]; }
get entryType() { return this[kType]; }
get startTime() { return this[kStart]; }
get duration() { return this[kDuration]; }
get detail() { return this[kDetail]; }
[kInspect](depth, options) {
if (depth < 0) return this;
const opts = {
...options,
depth: options.depth == null ? null : options.depth - 1
};
return `${this.constructor.name} ${inspect(this.toJSON(), opts)}`;
}
toJSON() {
return {
name: this.name,
entryType: this.entryType,
startTime: this.startTime,
duration: this.duration,
detail: this.detail,
};
}
}
class InternalPerformanceEntry {
constructor(name, type, start, duration, detail) {
this[kName] = name;
this[kType] = type;
this[kStart] = start;
this[kDuration] = duration;
this[kDetail] = detail;
}
}
InternalPerformanceEntry.prototype.constructor = PerformanceEntry;
ObjectSetPrototypeOf(
InternalPerformanceEntry.prototype,
PerformanceEntry.prototype);
module.exports = {
InternalPerformanceEntry,
PerformanceEntry,
kReadOnlyAttributes,
isPerformanceEntry,
now,
};