2020-03-18 15:09:45 +00:00
|
|
|
/* eslint-disable @gitlab/require-i18n-strings */
|
2018-03-19 19:06:09 +00:00
|
|
|
import Vue from 'vue';
|
2019-10-25 00:06:14 +00:00
|
|
|
import axios from '~/lib/utils/axios_utils';
|
2021-02-14 18:09:20 +00:00
|
|
|
import Translate from '~/vue_shared/translate';
|
2019-10-25 00:06:14 +00:00
|
|
|
|
2021-02-14 18:09:20 +00:00
|
|
|
import initPerformanceBarLog from './performance_bar_log';
|
2018-07-13 09:57:20 +00:00
|
|
|
import PerformanceBarService from './services/performance_bar_service';
|
2018-03-19 19:06:09 +00:00
|
|
|
import PerformanceBarStore from './stores/performance_bar_store';
|
|
|
|
|
2020-12-11 09:09:48 +00:00
|
|
|
Vue.use(Translate);
|
|
|
|
|
2020-12-23 21:10:24 +00:00
|
|
|
const initPerformanceBar = (el) => {
|
2021-01-14 15:10:46 +00:00
|
|
|
if (!el) {
|
|
|
|
return undefined;
|
|
|
|
}
|
2020-09-12 03:09:29 +00:00
|
|
|
const performanceBarData = el.dataset;
|
|
|
|
|
|
|
|
return new Vue({
|
|
|
|
el,
|
2018-03-19 19:06:09 +00:00
|
|
|
components: {
|
2019-09-30 21:06:41 +00:00
|
|
|
PerformanceBarApp: () => import('./components/performance_bar_app.vue'),
|
2018-03-19 19:06:09 +00:00
|
|
|
},
|
|
|
|
data() {
|
|
|
|
const store = new PerformanceBarStore();
|
|
|
|
|
|
|
|
return {
|
|
|
|
store,
|
|
|
|
env: performanceBarData.env,
|
|
|
|
requestId: performanceBarData.requestId,
|
|
|
|
peekUrl: performanceBarData.peekUrl,
|
|
|
|
profileUrl: performanceBarData.profileUrl,
|
2021-02-18 12:09:34 +00:00
|
|
|
statsUrl: performanceBarData.statsUrl,
|
2018-03-19 19:06:09 +00:00
|
|
|
};
|
|
|
|
},
|
2018-07-13 09:57:20 +00:00
|
|
|
mounted() {
|
2020-08-24 12:10:17 +00:00
|
|
|
PerformanceBarService.registerInterceptor(this.peekUrl, this.loadRequestDetails);
|
2018-07-13 09:57:20 +00:00
|
|
|
|
|
|
|
this.loadRequestDetails(this.requestId, window.location.href);
|
|
|
|
},
|
|
|
|
beforeDestroy() {
|
2020-08-24 12:10:17 +00:00
|
|
|
PerformanceBarService.removeInterceptor();
|
2018-07-13 09:57:20 +00:00
|
|
|
},
|
|
|
|
methods: {
|
2019-10-25 00:06:14 +00:00
|
|
|
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-07-13 09:57:20 +00:00
|
|
|
loadRequestDetails(requestId, requestUrl) {
|
|
|
|
if (!this.store.canTrackRequest(requestUrl)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.store.addRequest(requestId, requestUrl);
|
|
|
|
|
|
|
|
PerformanceBarService.fetchRequestDetails(this.peekUrl, requestId)
|
2020-12-23 21:10:24 +00:00
|
|
|
.then((res) => {
|
2019-09-30 21:06:41 +00:00
|
|
|
this.store.addRequestDetails(requestId, res.data);
|
2019-11-29 09:06:31 +00:00
|
|
|
|
|
|
|
if (this.requestId === requestId) this.collectFrontendPerformanceMetrics();
|
2018-07-13 09:57:20 +00:00
|
|
|
})
|
|
|
|
.catch(() =>
|
2018-08-29 11:06:49 +00:00
|
|
|
// eslint-disable-next-line no-console
|
|
|
|
console.warn(`Error getting performance bar results for ${requestId}`),
|
2018-07-13 09:57:20 +00:00
|
|
|
);
|
|
|
|
},
|
2019-11-29 09:06:31 +00:00
|
|
|
collectFrontendPerformanceMetrics() {
|
|
|
|
if (performance) {
|
|
|
|
const navigationEntries = performance.getEntriesByType('navigation');
|
|
|
|
const paintEntries = performance.getEntriesByType('paint');
|
|
|
|
const resourceEntries = performance.getEntriesByType('resource');
|
|
|
|
|
|
|
|
let durationString = '';
|
|
|
|
if (navigationEntries.length > 0) {
|
2019-12-05 00:07:50 +00:00
|
|
|
durationString = `${Math.round(navigationEntries[0].responseEnd)} | `;
|
|
|
|
durationString += `${Math.round(paintEntries[1].startTime)} | `;
|
|
|
|
durationString += ` ${Math.round(navigationEntries[0].domContentLoadedEventEnd)}`;
|
2019-11-29 09:06:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let newEntries = resourceEntries.map(this.transformResourceEntry);
|
|
|
|
|
|
|
|
this.updateFrontendPerformanceMetrics(durationString, newEntries);
|
|
|
|
|
|
|
|
if ('PerformanceObserver' in window) {
|
|
|
|
// We start observing for more incoming timings
|
2020-12-23 21:10:24 +00:00
|
|
|
const observer = new PerformanceObserver((list) => {
|
2019-11-29 09:06:31 +00:00
|
|
|
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-07-13 09:57:20 +00:00
|
|
|
},
|
2018-03-19 19:06:09 +00:00
|
|
|
render(createElement) {
|
|
|
|
return createElement('performance-bar-app', {
|
|
|
|
props: {
|
|
|
|
store: this.store,
|
|
|
|
env: this.env,
|
|
|
|
requestId: this.requestId,
|
|
|
|
peekUrl: this.peekUrl,
|
|
|
|
profileUrl: this.profileUrl,
|
2021-02-18 12:09:34 +00:00
|
|
|
statsUrl: this.statsUrl,
|
2018-03-19 19:06:09 +00:00
|
|
|
},
|
2019-10-25 00:06:14 +00:00
|
|
|
on: {
|
|
|
|
'add-request': this.addRequestManually,
|
|
|
|
},
|
2018-03-19 19:06:09 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
});
|
2020-09-12 03:09:29 +00:00
|
|
|
};
|
2020-09-09 21:08:33 +00:00
|
|
|
|
2021-01-14 15:10:46 +00:00
|
|
|
initPerformanceBar(document.querySelector('#js-peek'));
|
2020-09-16 12:10:15 +00:00
|
|
|
initPerformanceBarLog();
|
|
|
|
|
2020-09-09 21:08:33 +00:00
|
|
|
export default initPerformanceBar;
|