nstdlib-nightly
Version:
Node.js standard library converted to runtime-agnostic ES modules.
151 lines (131 loc) • 3.58 kB
JavaScript
// Source: https://github.com/nodejs/node/blob/65eff1eb/lib/internal/perf/nodetiming.js
import { PerformanceEntry } from "nstdlib/lib/internal/perf/performance_entry";
import { now, getMilestoneTimestamp } from "nstdlib/lib/internal/perf/utils";
import { customInspectSymbol as kInspect } from "nstdlib/lib/internal/util";
import { inspect } from "nstdlib/lib/util";
import {
constants as __constants__,
loopIdleTime,
} from "nstdlib/stub/binding/performance";
const {
NODE_PERFORMANCE_MILESTONE_NODE_START,
NODE_PERFORMANCE_MILESTONE_V8_START,
NODE_PERFORMANCE_MILESTONE_LOOP_START,
NODE_PERFORMANCE_MILESTONE_LOOP_EXIT,
NODE_PERFORMANCE_MILESTONE_BOOTSTRAP_COMPLETE,
NODE_PERFORMANCE_MILESTONE_ENVIRONMENT,
} = __constants__;
class PerformanceNodeTiming {
constructor() {
Object.defineProperties(this, {
name: {
__proto__: null,
enumerable: true,
configurable: true,
value: "node",
},
entryType: {
__proto__: null,
enumerable: true,
configurable: true,
value: "node",
},
startTime: {
__proto__: null,
enumerable: true,
configurable: true,
value: 0,
},
duration: {
__proto__: null,
enumerable: true,
configurable: true,
get: now,
},
nodeStart: {
__proto__: null,
enumerable: true,
configurable: true,
get() {
return getMilestoneTimestamp(NODE_PERFORMANCE_MILESTONE_NODE_START);
},
},
v8Start: {
__proto__: null,
enumerable: true,
configurable: true,
get() {
return getMilestoneTimestamp(NODE_PERFORMANCE_MILESTONE_V8_START);
},
},
environment: {
__proto__: null,
enumerable: true,
configurable: true,
get() {
return getMilestoneTimestamp(NODE_PERFORMANCE_MILESTONE_ENVIRONMENT);
},
},
loopStart: {
__proto__: null,
enumerable: true,
configurable: true,
get() {
return getMilestoneTimestamp(NODE_PERFORMANCE_MILESTONE_LOOP_START);
},
},
loopExit: {
__proto__: null,
enumerable: true,
configurable: true,
get() {
return getMilestoneTimestamp(NODE_PERFORMANCE_MILESTONE_LOOP_EXIT);
},
},
bootstrapComplete: {
__proto__: null,
enumerable: true,
configurable: true,
get() {
return getMilestoneTimestamp(
NODE_PERFORMANCE_MILESTONE_BOOTSTRAP_COMPLETE,
);
},
},
idleTime: {
__proto__: null,
enumerable: true,
configurable: true,
get: loopIdleTime,
},
});
}
[kInspect](depth, options) {
if (depth < 0) return this;
const opts = {
...options,
depth: options.depth == null ? null : options.depth - 1,
};
return `PerformanceNodeTiming ${inspect(this.toJSON(), opts)}`;
}
toJSON() {
return {
name: "node",
entryType: "node",
startTime: this.startTime,
duration: this.duration,
nodeStart: this.nodeStart,
v8Start: this.v8Start,
bootstrapComplete: this.bootstrapComplete,
environment: this.environment,
loopStart: this.loopStart,
loopExit: this.loopExit,
idleTime: this.idleTime,
};
}
}
Object.setPrototypeOf(
PerformanceNodeTiming.prototype,
PerformanceEntry.prototype,
);
export default new PerformanceNodeTiming();