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

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

165 lines
5.5 KiB
JavaScript
Raw Normal View History

2021-09-04 01:27:46 +05:30
import '../webpack';
2022-05-07 20:08:51 +05:30
import { isEmpty } from 'lodash';
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,
2022-04-04 11:22:00 +05:30
name: 'PerformanceBarRoot',
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() {
2022-05-07 20:08:51 +05:30
PerformanceBarService.registerInterceptor(this.peekUrl, this.addRequest);
2018-11-18 11:00:15 +05:30
2022-05-07 20:08:51 +05:30
this.addRequest(this.requestId, window.location.href);
this.loadRequestDetails(this.requestId);
2018-11-18 11:00:15 +05:30
},
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 {
2022-05-07 20:08:51 +05:30
this.addRequest(urlOrRequestId, urlOrRequestId);
2019-12-26 22:10:19 +05:30
}
},
2022-05-07 20:08:51 +05:30
addRequest(requestId, requestUrl) {
2018-11-18 11:00:15 +05:30
if (!this.store.canTrackRequest(requestUrl)) {
return;
}
this.store.addRequest(requestId, requestUrl);
2022-05-07 20:08:51 +05:30
},
loadRequestDetails(requestId) {
const request = this.store.findRequest(requestId);
if (request && isEmpty(request.details)) {
return PerformanceBarService.fetchRequestDetails(this.peekUrl, requestId)
.then((res) => {
this.store.addRequestDetails(requestId, res.data);
if (this.requestId === requestId) this.collectFrontendPerformanceMetrics();
})
.catch(() =>
// eslint-disable-next-line no-console
console.warn(`Error getting performance bar results for ${requestId}`),
);
}
2018-11-18 11:00:15 +05:30
2022-05-07 20:08:51 +05:30
return Promise.resolve();
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);
2022-05-07 20:08:51 +05:30
const firstContentfulPaint = Math.round(
paintEntries.find((entry) => entry.name === 'first-contentful-paint')?.startTime,
);
2021-04-29 21:17:54 +05:30
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,
2022-05-07 20:08:51 +05:30
'change-request': this.loadRequestDetails,
2019-12-26 22:10:19 +05:30
},
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;