Skip to content

Commit

Permalink
fix: use performance.mark/measure instead of console.timeStamp()
Browse files Browse the repository at this point in the history
This switches us from the non-standard console.timeStamp() API to the standard
performance.mark()/performance.measure() API.
  • Loading branch information
nolanlawson committed Feb 28, 2019
1 parent 81f892e commit 0daf847
Showing 1 changed file with 19 additions and 18 deletions.
37 changes: 19 additions & 18 deletions packages/best-runtime/src/run_iteration.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,25 @@ const _initHooks = hooks =>

const _forceGC = () => window.gc && window.gc();

// Temporary fix
// TODO (dbajric): Use https://www.npmjs.com/package/console-polyfill
if (!console.timeStamp) {
console.timeStamp = function () { };
function startMeasure(markName) {
performance.mark(markName);
}

const executeBenchmark = async (benchmarkNode, { useMacroTaskAfterBenchmark }) => {
function endMeasure(markName) {
performance.measure(markName, markName);
performance.clearMarks(markName);
performance.clearMeasures(markName);
}

const executeBenchmark = async (benchmarkNode, markName, { useMacroTaskAfterBenchmark }) => {
// Force garbage collection before executing an iteration (--js-flags=--expose-gc)
_forceGC();
return new Promise((resolve, reject) => {
raf(async () => {
benchmarkNode.startedAt = formatTime(time());

if (process.env.NODE_ENV !== 'production') {
console.timeStamp('iteration_start');
startMeasure(markName);
}

try {
Expand All @@ -41,21 +45,21 @@ const executeBenchmark = async (benchmarkNode, { useMacroTaskAfterBenchmark }) =
await nextTick();
benchmarkNode.duration = formatTime(time() - benchmarkNode.startedAt);
if (process.env.NODE_ENV !== 'production') {
console.timeStamp('iteration_end');
endMeasure(markName);
}
resolve();
})();
} else {
benchmarkNode.duration = formatTime(time() - benchmarkNode.startedAt);
if (process.env.NODE_ENV !== 'production') {
console.timeStamp('iteration_end');
endMeasure(markName);
}
resolve();
}
} catch (e) {
benchmarkNode.duration = -1;
if (process.env.NODE_ENV !== 'production') {
console.timeStamp('iteration_end');
endMeasure(markName);
}
reject();
}
Expand Down Expand Up @@ -92,34 +96,31 @@ export const runBenchmarkIteration = async (node, opts) => {

if (run) {
// -- Before ----
const markName = run.parent.name;
if (process.env.NODE_ENV !== 'production') {
console.timeStamp('before_hooks_start');
startMeasure(`before_${markName}`);
}
for (const hook of hookHandlers[HOOKS.BEFORE]) {
await hook();
}
if (process.env.NODE_ENV !== 'production') {
console.timeStamp('before_hooks_end');
}

if (process.env.NODE_ENV !== 'production') {
console.timeStamp('iteration_start');
endMeasure(`before_${markName}`);
}

// -- Run ----
node.startedAt = formatTime(time());
await executeBenchmark(run, opts);
await executeBenchmark(run, markName, opts);
node.duration = formatTime(time() - node.startedAt);

// -- After ----
if (process.env.NODE_ENV !== 'production') {
console.timeStamp('after_hooks_start');
startMeasure(`after_${markName}`);
}
for (const hook of hookHandlers[HOOKS.AFTER]) {
await hook();
}
if (process.env.NODE_ENV !== 'production') {
console.timeStamp('after_hooks_end');
endMeasure(`after_${markName}`);
}
}

Expand Down

0 comments on commit 0daf847

Please sign in to comment.