Skip to content

Commit

Permalink
Merge pull request Expensify#11218 from hannojg/dev/fix-ios-error-on-…
Browse files Browse the repository at this point in the history
…start-with-metrics
  • Loading branch information
roryabraham authored Sep 23, 2022
2 parents 3629078 + 93239a0 commit b75faf0
Showing 1 changed file with 30 additions and 24 deletions.
54 changes: 30 additions & 24 deletions src/libs/Performance.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,27 @@ if (Metrics.canCapturePerformanceMetrics()) {
perfModule.setResourceLoggingEnabled(true);
rnPerformance = perfModule.default;

const measureFailSafe = (measureName, startOrMeasureOptions, endMark) => {
try {
rnPerformance.measure(measureName, startOrMeasureOptions, endMark);
} catch (error) {
// Sometimes there might be no start mark recorded and the measure will fail with an error
console.debug(error.message);
}
};

// Monitor some native marks that we want to put on the timeline
new perfModule.PerformanceObserver((list, observer) => {
list.getEntries()
.forEach((entry) => {
if (entry.name === 'nativeLaunchEnd') {
rnPerformance.measure('nativeLaunch', 'nativeLaunchStart', 'nativeLaunchEnd');
measureFailSafe('nativeLaunch', 'nativeLaunchStart', 'nativeLaunchEnd');
}
if (entry.name === 'downloadEnd') {
rnPerformance.measure('jsBundleDownload', 'downloadStart', 'downloadEnd');
measureFailSafe('jsBundleDownload', 'downloadStart', 'downloadEnd');
}
if (entry.name === 'runJsBundleEnd') {
rnPerformance.measure('runJsBundle', 'runJsBundleStart', 'runJsBundleEnd');
measureFailSafe('runJsBundle', 'runJsBundleStart', 'runJsBundleEnd');
}

// We don't need to keep the observer past this point
Expand All @@ -82,27 +91,22 @@ if (Metrics.canCapturePerformanceMetrics()) {
new perfModule.PerformanceObserver((list) => {
list.getEntriesByType('mark')
.forEach((mark) => {
try {
if (mark.name.endsWith('_end')) {
const end = mark.name;
const name = end.replace(/_end$/, '');
const start = `${name}_start`;
rnPerformance.measure(name, start, end);
}

// Capture any custom measures or metrics below
if (mark.name === `${CONST.TIMING.SIDEBAR_LOADED}_end`) {
// Make sure TTI is captured when the app is really usable
InteractionManager.runAfterInteractions(() => {
requestAnimationFrame(() => {
rnPerformance.measure('TTI', 'nativeLaunchStart', mark.name);
Performance.printPerformanceMetrics();
});
if (mark.name.endsWith('_end')) {
const end = mark.name;
const name = end.replace(/_end$/, '');
const start = `${name}_start`;
measureFailSafe(name, start, end);
}

// Capture any custom measures or metrics below
if (mark.name === `${CONST.TIMING.SIDEBAR_LOADED}_end`) {
// Make sure TTI is captured when the app is really usable
InteractionManager.runAfterInteractions(() => {
requestAnimationFrame(() => {
measureFailSafe('TTI', 'nativeLaunchStart', mark.name);
Performance.printPerformanceMetrics();
});
}
} catch (error) {
// Sometimes there might be no start mark recorded and the measure will fail with an error
console.debug(error.message);
});
}
});
}).observe({type: 'mark', buffered: true});
Expand All @@ -122,7 +126,9 @@ if (Metrics.canCapturePerformanceMetrics()) {
.map(entry => `\u2022 ${entry.name}: ${entry.duration.toFixed(1)}ms`)
.value();

Alert.alert('Performance', stats.join('\n'));
if (stats.length > 0) {
Alert.alert('Performance', stats.join('\n'));
}
};

/**
Expand Down

0 comments on commit b75faf0

Please sign in to comment.