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

153 lines
5.1 KiB
JavaScript
Raw Normal View History

2021-09-04 01:27:46 +05:30
import '../webpack';
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-04-29 21:17:54 +05:30
import { numberToHumanSize } from '~/lib/utils/number_utils';
import { s__ } from '~/locale';
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,
2021-04-17 20:07:23 +05:30
statsUrl: performanceBarData.statsUrl,
2018-05-09 12:01:36 +05:30
};
},
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 = '';
2021-04-29 21:17:54 +05:30
let summary = {};
2020-01-01 13:55:28 +05:30
if (navigationEntries.length > 0) {
2021-04-29 21:17:54 +05:30
const backend = Math.round(navigationEntries[0].responseEnd);
const firstContentfulPaint = Math.round(paintEntries[1].startTime);
const domContentLoaded = Math.round(navigationEntries[0].domContentLoadedEventEnd);
summary = {
[s__('PerformanceBar|Backend')]: backend,
[s__('PerformanceBar|First Contentful Paint')]: firstContentfulPaint,
[s__('PerformanceBar|DOM Content Loaded')]: domContentLoaded,
};
durationString = `${backend} | ${firstContentfulPaint} | ${domContentLoaded}`;
2020-01-01 13:55:28 +05:30
}
let newEntries = resourceEntries.map(this.transformResourceEntry);
2021-04-29 21:17:54 +05:30
this.updateFrontendPerformanceMetrics(durationString, summary, newEntries);
2020-01-01 13:55:28 +05:30
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));
2021-04-29 21:17:54 +05:30
this.updateFrontendPerformanceMetrics(durationString, summary, newEntries);
2020-01-01 13:55:28 +05:30
});
observer.observe({ entryTypes: ['resource'] });
}
}
},
2021-04-29 21:17:54 +05:30
updateFrontendPerformanceMetrics(durationString, summary, requestEntries) {
2020-01-01 13:55:28 +05:30
this.store.setRequestDetailsData(this.requestId, 'total', {
duration: durationString,
calls: requestEntries.length,
details: requestEntries,
2021-04-29 21:17:54 +05:30
summaryOptions: {
hideDuration: true,
},
summary,
2020-01-01 13:55:28 +05:30
});
},
transformResourceEntry(entry) {
return {
2021-04-29 21:17:54 +05:30
start: entry.startTime,
2020-01-01 13:55:28 +05:30
name: entry.name.replace(document.location.origin, ''),
duration: Math.round(entry.duration),
2021-04-29 21:17:54 +05:30
size: entry.transferSize ? numberToHumanSize(entry.transferSize) : 'cached',
2020-01-01 13:55:28 +05:30
};
},
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,
2021-04-17 20:07:23 +05:30
statsUrl: this.statsUrl,
2018-05-09 12:01:36 +05:30
},
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;