debian-mirror-gitlab/app/assets/javascripts/performance_bar/index.js

151 lines
4.9 KiB
JavaScript
Raw Normal View History

2020-04-22 19:07:51 +05:30
/* eslint-disable @gitlab/require-i18n-strings */
2018-05-09 12:01:36 +05:30
import Vue from 'vue';
2021-02-22 17:27:13 +05:30
import Translate from '~/vue_shared/translate';
2019-12-26 22:10:19 +05:30
import axios from '~/lib/utils/axios_utils';
2018-11-18 11:00:15 +05:30
import PerformanceBarService from './services/performance_bar_service';
2018-05-09 12:01:36 +05:30
import PerformanceBarStore from './stores/performance_bar_store';
2020-11-24 15:15:51 +05:30
import initPerformanceBarLog from './performance_bar_log';
2021-02-22 17:27:13 +05:30
Vue.use(Translate);
2020-11-24 15:15:51 +05:30
const initPerformanceBar = el => {
const performanceBarData = el.dataset;
return new Vue({
el,
2018-05-09 12:01:36 +05:30
components: {
2019-12-21 20:55:43 +05:30
PerformanceBarApp: () => import('./components/performance_bar_app.vue'),
2018-05-09 12:01:36 +05:30
},
data() {
const store = new PerformanceBarStore();
return {
store,
env: performanceBarData.env,
requestId: performanceBarData.requestId,
peekUrl: performanceBarData.peekUrl,
profileUrl: performanceBarData.profileUrl,
};
},
2018-11-18 11:00:15 +05:30
mounted() {
2020-11-24 15:15:51 +05:30
PerformanceBarService.registerInterceptor(this.peekUrl, this.loadRequestDetails);
2018-11-18 11:00:15 +05:30
this.loadRequestDetails(this.requestId, window.location.href);
},
beforeDestroy() {
2020-11-24 15:15:51 +05:30
PerformanceBarService.removeInterceptor();
2018-11-18 11:00:15 +05:30
},
methods: {
2019-12-26 22:10:19 +05:30
addRequestManually(urlOrRequestId) {
if (urlOrRequestId.startsWith('https://') || urlOrRequestId.startsWith('http://')) {
// We don't need to do anything with the response, we just
// want to trace the request.
axios.get(urlOrRequestId);
} else {
this.loadRequestDetails(urlOrRequestId, urlOrRequestId);
}
},
2018-11-18 11:00:15 +05:30
loadRequestDetails(requestId, requestUrl) {
if (!this.store.canTrackRequest(requestUrl)) {
return;
}
this.store.addRequest(requestId, requestUrl);
PerformanceBarService.fetchRequestDetails(this.peekUrl, requestId)
.then(res => {
2019-12-21 20:55:43 +05:30
this.store.addRequestDetails(requestId, res.data);
2020-01-01 13:55:28 +05:30
if (this.requestId === requestId) this.collectFrontendPerformanceMetrics();
2018-11-18 11:00:15 +05:30
})
.catch(() =>
2018-11-20 20:47:30 +05:30
// eslint-disable-next-line no-console
console.warn(`Error getting performance bar results for ${requestId}`),
2018-11-18 11:00:15 +05:30
);
},
2020-01-01 13:55:28 +05:30
collectFrontendPerformanceMetrics() {
if (performance) {
const navigationEntries = performance.getEntriesByType('navigation');
const paintEntries = performance.getEntriesByType('paint');
const resourceEntries = performance.getEntriesByType('resource');
let durationString = '';
if (navigationEntries.length > 0) {
durationString = `${Math.round(navigationEntries[0].responseEnd)} | `;
durationString += `${Math.round(paintEntries[1].startTime)} | `;
durationString += ` ${Math.round(navigationEntries[0].domContentLoadedEventEnd)}`;
}
let newEntries = resourceEntries.map(this.transformResourceEntry);
this.updateFrontendPerformanceMetrics(durationString, newEntries);
if ('PerformanceObserver' in window) {
// We start observing for more incoming timings
const observer = new PerformanceObserver(list => {
newEntries = newEntries.concat(list.getEntries().map(this.transformResourceEntry));
this.updateFrontendPerformanceMetrics(durationString, newEntries);
});
observer.observe({ entryTypes: ['resource'] });
}
}
},
updateFrontendPerformanceMetrics(durationString, requestEntries) {
this.store.setRequestDetailsData(this.requestId, 'total', {
duration: durationString,
calls: requestEntries.length,
details: requestEntries,
});
},
transformResourceEntry(entry) {
const nf = new Intl.NumberFormat();
return {
name: entry.name.replace(document.location.origin, ''),
duration: Math.round(entry.duration),
size: entry.transferSize ? `${nf.format(entry.transferSize)} bytes` : 'cached',
};
},
2018-11-18 11:00:15 +05:30
},
2018-05-09 12:01:36 +05:30
render(createElement) {
return createElement('performance-bar-app', {
props: {
store: this.store,
env: this.env,
requestId: this.requestId,
peekUrl: this.peekUrl,
profileUrl: this.profileUrl,
},
2019-12-26 22:10:19 +05:30
on: {
'add-request': this.addRequestManually,
},
2018-05-09 12:01:36 +05:30
});
},
});
2020-11-24 15:15:51 +05:30
};
2021-02-22 17:27:13 +05:30
let loadedPeekBar = false;
function loadBar() {
2020-11-24 15:15:51 +05:30
const jsPeek = document.querySelector('#js-peek');
2021-02-22 17:27:13 +05:30
if (!loadedPeekBar && jsPeek) {
loadedPeekBar = true;
2020-11-24 15:15:51 +05:30
initPerformanceBar(jsPeek);
}
2021-02-22 17:27:13 +05:30
}
// If js-peek is not loaded when this script is executed, this call will do nothing
// If this is the case, then it will loadBar on DOMContentLoaded. We would prefer it
// to be initialized before the DOMContetLoaded event in order to pick up all the
// requests sent from the page.
loadBar();
document.addEventListener('DOMContentLoaded', () => {
loadBar();
2020-11-24 15:15:51 +05:30
});
initPerformanceBarLog();
export default initPerformanceBar;