in browsers perf util is just a wrapper around native performance

This commit is contained in:
Johannes Rieken
2020-12-15 10:25:02 +01:00
parent 1537b5bcc2
commit 460b6e720e
2 changed files with 43 additions and 45 deletions
+41 -22
View File
@@ -9,31 +9,50 @@
function _factory(sharedObj) {
sharedObj.MonacoPerformanceMarks = sharedObj.MonacoPerformanceMarks || [];
const _dataLen = 2;
const _nativeMark = typeof performance === 'object' && typeof performance.mark === 'function' ? performance.mark.bind(performance) : () => { };
function getMarks() {
const result = [];
const entries = sharedObj.MonacoPerformanceMarks;
for (let i = 0; i < entries.length; i += _dataLen) {
result.push({
name: entries[i],
startTime: entries[i + 1],
if (typeof performance === 'object' && typeof performance.mark === 'function') {
// in a browser context, reuse performance-util
function mark(name) {
performance.mark(name);
}
function getMarks() {
let timeOrigin = performance.timeOrigin;
if (!timeOrigin) {
// polyfill for Safari
const entry = performance.timing;
timeOrigin = entry.navigationStart || entry.redirectStart || entry.fetchStart;
}
return performance.getEntriesByType('mark').map(entry => {
return {
name: entry.name,
startTime: Math.round(timeOrigin + entry.startTime)
};
});
}
return result;
return { mark, getMarks };
} else {
// node.js context, use mock and a shared obj that's share between module systems
sharedObj.MonacoPerformanceMarks = sharedObj.MonacoPerformanceMarks || [];
function mark(name) {
sharedObj.MonacoPerformanceMarks.push(name, Date.now());
}
function getMarks() {
const result = [];
const entries = sharedObj.MonacoPerformanceMarks;
for (let i = 0; i < entries.length; i += 2) {
result.push({
name: entries[i],
startTime: entries[i + 1],
});
}
return result;
}
return { mark, getMarks };
}
function mark(name) {
sharedObj.MonacoPerformanceMarks.push(name, Date.now());
_nativeMark(name);
}
const exports = { mark, getMarks };
return exports;
}
// This module can be loaded in an amd and commonjs-context.
@@ -399,13 +399,8 @@ export abstract class AbstractTimerService implements ITimerService {
_lifecycleService.when(LifecyclePhase.Restored)
]).then(() => {
// import native-browser marks
this._submitNativeMarks();
// because "our" perf.mark-util also adds native performance marks
// no extra import of "our" marks is needed, they are already imported
// by importing native perf marks (see above)
// this.submitPerformanceMarks(perf.getMarks());
// set perf mark from renderer
this.setPerformanceMarks('renderer', perf.getMarks());
return this._computeStartupMetrics();
}).then(metrics => {
@@ -414,22 +409,6 @@ export abstract class AbstractTimerService implements ITimerService {
});
}
private _submitNativeMarks(): void {
let timeOrigin = performance.timeOrigin;
if (!timeOrigin) {
// polyfill for Safari
const entry = performance.timing;
timeOrigin = entry.navigationStart || entry.redirectStart || entry.fetchStart;
}
const marks: perf.PerformanceMark[] = performance.getEntriesByType('mark').map(entry => {
return {
name: entry.name,
startTime: Math.round(timeOrigin + entry.startTime)
};
});
this.setPerformanceMarks('renderer', marks);
}
setPerformanceMarks(source: string, marks: perf.PerformanceMark[]): void {
this._marks.setMarks(source, marks);
}