Skip to content
Merged
Changes from 2 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
48 changes: 35 additions & 13 deletions ext/node/polyfills/perf_hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,34 +54,52 @@ class PerformanceObserver extends WebPerformanceObserver {
}
}

performance.eventLoopUtilization = () => {
const eventLoopUtilization = () => {
// TODO(@marvinhagemeister): Return actual non-stubbed values
return { idle: 0, active: 0, utilization: 0 };
};

performance.eventLoopUtilization = eventLoopUtilization;

performance.nodeTiming = {};

Comment thread
bartlomieju marked this conversation as resolved.
performance.timerify = (fn) => {
const timerify = (fn, options = {}) => {
if (typeof fn !== "function") {
throw new TypeError("The 'fn' argument must be of type function");
throw new ERR_INVALID_ARG_TYPE("fn", "function", fn);
}
const wrapped = (...args) => {
const start = performance.now();
const result = fn(...args);
const end = performance.now();

performance.measure(`timerify(${fn.name || "anonymous"})`, { start, end });
if (options.histogram !== undefined) {
if (
typeof options.histogram !== "object" ||
options.histogram === null ||
typeof options.histogram.record !== "function"
) {
throw new ERR_INVALID_ARG_TYPE(
"options.histogram",
"RecordableHistogram",
options.histogram,
);
}
Comment thread
bartlomieju marked this conversation as resolved.
}

return result;
};
function timerified(...args) {
// TODO(bartlomieju): emit PerformanceEntry with entryType 'function'
Comment thread
bartlomieju marked this conversation as resolved.
return new.target ? new fn(...args) : fn.apply(this, args);
}

Object.defineProperty(wrapped, "name", {
value: fn.name || "wrapped",
Object.defineProperty(timerified, "name", {
value: `timerified ${fn.name}`,
configurable: true,
});
Object.defineProperty(timerified, "length", {
value: fn.length,
configurable: true,
});

return wrapped;
return timerified;
};

performance.timerify = timerify;
// TODO(bartlomieju):
performance.markResourceTiming = () => {};

Expand All @@ -97,14 +115,18 @@ export default {
PerformanceObserverEntryList,
PerformanceEntry,
monitorEventLoopDelay,
eventLoopUtilization,
timerify,
constants,
};

export {
constants,
eventLoopUtilization,
monitorEventLoopDelay,
performance,
PerformanceEntry,
PerformanceObserver,
PerformanceObserverEntryList,
timerify,
};
Loading