add tiny util to allow performance mark collection (via perf_hooks) independent of amd or commonjs usage

This commit is contained in:
Johannes Rieken 2020-11-24 11:43:50 +01:00
parent 4ddf7bc0a7
commit 9a657db088
2 changed files with 60 additions and 0 deletions

12
src/vs/base/node/startupPerf.d.ts vendored Normal file
View file

@ -0,0 +1,12 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { PerformanceEntry } from 'perf_hooks';
/**
* Return all performance entries captured so far and stop startup
* performance recording.
*/
export function consumeAndStop(): PerformanceEntry[];

View file

@ -0,0 +1,48 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
'use strict';
//@ts-check
function _factory(sharedObj, nodeRequire) {
if (!sharedObj.MonacoStartupPerformanceMarks) {
const { PerformanceObserver } = nodeRequire('perf_hooks');
let startupEntries = [];
const startupObs = new PerformanceObserver(list => { startupEntries = startupEntries.concat(list.getEntries()); });
startupObs.observe({ buffered: true, entryTypes: ['mark'] });
sharedObj.MonacoStartupPerformanceMarks = {
startupEntries,
dispose() {
startupObs.disconnect();
startupEntries.length = 0;
delete sharedObj.MonacoStartupPerformanceMarks;
},
};
}
return {
consumeAndStop() {
const entries = sharedObj.startupEntries.slice(0);
sharedObj.MonacoStartupPerformanceMarks.dispose();
return entries;
}
};
}
// This module can be loaded in an amd and commonjs-context.
// Because we want both instances to use the same perf-data
// we store them globally
if (typeof define === 'function') {
// amd
define([], function () { return _factory(global, require.__$__nodeRequire); });
} else if (typeof module === 'object' && typeof module.exports === 'object') {
// commonjs
module.exports = _factory(global, require);
}