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';
|
2019-12-26 22:10:19 +05:30
|
|
|
import axios from '~/lib/utils/axios_utils';
|
2021-03-11 19:13:27 +05:30
|
|
|
import Translate from '~/vue_shared/translate';
|
2019-12-26 22:10:19 +05:30
|
|
|
|
2021-03-11 19:13:27 +05:30
|
|
|
import initPerformanceBarLog from './performance_bar_log';
|
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';
|
|
|
|
|
2021-02-22 17:27:13 +05:30
|
|
|
Vue.use(Translate);
|
|
|
|
|
2021-03-08 18:12:59 +05:30
|
|
|
const initPerformanceBar = (el) => {
|
|
|
|
if (!el) {
|
|
|
|
return undefined;
|
|
|
|
}
|
2020-11-24 15:15:51 +05:30
|
|
|
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)
|
2021-03-08 18:12:59 +05:30
|
|
|
.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
|
2021-03-08 18:12:59 +05:30
|
|
|
const observer = new PerformanceObserver((list) => {
|
2020-01-01 13:55:28 +05:30
|
|
|
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-03-08 18:12:59 +05:30
|
|
|
initPerformanceBar(document.querySelector('#js-peek'));
|
2020-11-24 15:15:51 +05:30
|
|
|
initPerformanceBarLog();
|
|
|
|
|
|
|
|
export default initPerformanceBar;
|