2021-06-16 14:10:35 -04:00
|
|
|
import '../webpack';
|
|
|
|
|
2022-03-11 16:07:59 -05:00
|
|
|
import { isEmpty } from 'lodash';
|
2018-03-19 15:06:09 -04:00
|
|
|
import Vue from 'vue';
|
2019-10-24 20:06:14 -04:00
|
|
|
import axios from '~/lib/utils/axios_utils';
|
2021-04-08 08:09:18 -04:00
|
|
|
import { numberToHumanSize } from '~/lib/utils/number_utils';
|
|
|
|
import { s__ } from '~/locale';
|
2021-02-14 13:09:20 -05:00
|
|
|
import Translate from '~/vue_shared/translate';
|
2019-10-24 20:06:14 -04:00
|
|
|
|
2021-02-14 13:09:20 -05:00
|
|
|
import initPerformanceBarLog from './performance_bar_log';
|
2018-07-13 05:57:20 -04:00
|
|
|
import PerformanceBarService from './services/performance_bar_service';
|
2018-03-19 15:06:09 -04:00
|
|
|
import PerformanceBarStore from './stores/performance_bar_store';
|
|
|
|
|
2020-12-11 04:09:48 -05:00
|
|
|
Vue.use(Translate);
|
|
|
|
|
2020-12-23 16:10:24 -05:00
|
|
|
const initPerformanceBar = (el) => {
|
2021-01-14 10:10:46 -05:00
|
|
|
if (!el) {
|
|
|
|
return undefined;
|
|
|
|
}
|
2020-09-11 23:09:29 -04:00
|
|
|
const performanceBarData = el.dataset;
|
|
|
|
|
|
|
|
return new Vue({
|
|
|
|
el,
|
2022-02-16 01:12:24 -05:00
|
|
|
name: 'PerformanceBarRoot',
|
2018-03-19 15:06:09 -04:00
|
|
|
components: {
|
2019-09-30 17:06:41 -04:00
|
|
|
PerformanceBarApp: () => import('./components/performance_bar_app.vue'),
|
2018-03-19 15:06:09 -04:00
|
|
|
},
|
|
|
|
data() {
|
|
|
|
const store = new PerformanceBarStore();
|
|
|
|
|
|
|
|
return {
|
|
|
|
store,
|
|
|
|
env: performanceBarData.env,
|
|
|
|
requestId: performanceBarData.requestId,
|
|
|
|
peekUrl: performanceBarData.peekUrl,
|
|
|
|
profileUrl: performanceBarData.profileUrl,
|
2021-02-18 07:09:34 -05:00
|
|
|
statsUrl: performanceBarData.statsUrl,
|
2018-03-19 15:06:09 -04:00
|
|
|
};
|
|
|
|
},
|
2018-07-13 05:57:20 -04:00
|
|
|
mounted() {
|
2022-03-11 16:07:59 -05:00
|
|
|
PerformanceBarService.registerInterceptor(this.peekUrl, this.addRequest);
|
2018-07-13 05:57:20 -04:00
|
|
|
|
2022-03-11 16:07:59 -05:00
|
|
|
this.addRequest(this.requestId, window.location.href);
|
|
|
|
this.loadRequestDetails(this.requestId);
|
2018-07-13 05:57:20 -04:00
|
|
|
},
|
|
|
|
beforeDestroy() {
|
2020-08-24 08:10:17 -04:00
|
|
|
PerformanceBarService.removeInterceptor();
|
2018-07-13 05:57:20 -04:00
|
|
|
},
|
|
|
|
methods: {
|
2019-10-24 20:06:14 -04: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 {
|
2022-03-11 16:07:59 -05:00
|
|
|
this.addRequest(urlOrRequestId, urlOrRequestId);
|
2019-10-24 20:06:14 -04:00
|
|
|
}
|
|
|
|
},
|
2022-03-11 16:07:59 -05:00
|
|
|
addRequest(requestId, requestUrl) {
|
2018-07-13 05:57:20 -04:00
|
|
|
if (!this.store.canTrackRequest(requestUrl)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.store.addRequest(requestId, requestUrl);
|
2022-03-11 16:07:59 -05:00
|
|
|
},
|
|
|
|
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-07-13 05:57:20 -04:00
|
|
|
|
2022-03-11 16:07:59 -05:00
|
|
|
return Promise.resolve();
|
2018-07-13 05:57:20 -04:00
|
|
|
},
|
2019-11-29 04:06:31 -05:00
|
|
|
collectFrontendPerformanceMetrics() {
|
|
|
|
if (performance) {
|
|
|
|
const navigationEntries = performance.getEntriesByType('navigation');
|
|
|
|
const paintEntries = performance.getEntriesByType('paint');
|
|
|
|
const resourceEntries = performance.getEntriesByType('resource');
|
|
|
|
|
|
|
|
let durationString = '';
|
2021-04-08 08:09:18 -04:00
|
|
|
let summary = {};
|
2019-11-29 04:06:31 -05:00
|
|
|
if (navigationEntries.length > 0) {
|
2021-04-08 08:09:18 -04:00
|
|
|
const backend = Math.round(navigationEntries[0].responseEnd);
|
2022-02-23 10:14:44 -05:00
|
|
|
const firstContentfulPaint = Math.round(
|
|
|
|
paintEntries.find((entry) => entry.name === 'first-contentful-paint')?.startTime,
|
|
|
|
);
|
2021-04-08 08:09:18 -04:00
|
|
|
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}`;
|
2019-11-29 04:06:31 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
let newEntries = resourceEntries.map(this.transformResourceEntry);
|
|
|
|
|
2021-04-08 08:09:18 -04:00
|
|
|
this.updateFrontendPerformanceMetrics(durationString, summary, newEntries);
|
2019-11-29 04:06:31 -05:00
|
|
|
|
|
|
|
if ('PerformanceObserver' in window) {
|
|
|
|
// We start observing for more incoming timings
|
2020-12-23 16:10:24 -05:00
|
|
|
const observer = new PerformanceObserver((list) => {
|
2019-11-29 04:06:31 -05:00
|
|
|
newEntries = newEntries.concat(list.getEntries().map(this.transformResourceEntry));
|
2021-04-08 08:09:18 -04:00
|
|
|
this.updateFrontendPerformanceMetrics(durationString, summary, newEntries);
|
2019-11-29 04:06:31 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
observer.observe({ entryTypes: ['resource'] });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2021-04-08 08:09:18 -04:00
|
|
|
updateFrontendPerformanceMetrics(durationString, summary, requestEntries) {
|
2019-11-29 04:06:31 -05:00
|
|
|
this.store.setRequestDetailsData(this.requestId, 'total', {
|
|
|
|
duration: durationString,
|
|
|
|
calls: requestEntries.length,
|
|
|
|
details: requestEntries,
|
2021-04-08 08:09:18 -04:00
|
|
|
summaryOptions: {
|
|
|
|
hideDuration: true,
|
|
|
|
},
|
|
|
|
summary,
|
2019-11-29 04:06:31 -05:00
|
|
|
});
|
|
|
|
},
|
|
|
|
transformResourceEntry(entry) {
|
|
|
|
return {
|
2021-04-08 08:09:18 -04:00
|
|
|
start: entry.startTime,
|
2019-11-29 04:06:31 -05:00
|
|
|
name: entry.name.replace(document.location.origin, ''),
|
|
|
|
duration: Math.round(entry.duration),
|
2021-04-08 08:09:18 -04:00
|
|
|
size: entry.transferSize ? numberToHumanSize(entry.transferSize) : 'cached',
|
2019-11-29 04:06:31 -05:00
|
|
|
};
|
|
|
|
},
|
2018-07-13 05:57:20 -04:00
|
|
|
},
|
2018-03-19 15:06:09 -04: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 07:09:34 -05:00
|
|
|
statsUrl: this.statsUrl,
|
2018-03-19 15:06:09 -04:00
|
|
|
},
|
2019-10-24 20:06:14 -04:00
|
|
|
on: {
|
|
|
|
'add-request': this.addRequestManually,
|
2022-03-11 16:07:59 -05:00
|
|
|
'change-request': this.loadRequestDetails,
|
2019-10-24 20:06:14 -04:00
|
|
|
},
|
2018-03-19 15:06:09 -04:00
|
|
|
});
|
|
|
|
},
|
|
|
|
});
|
2020-09-11 23:09:29 -04:00
|
|
|
};
|
2020-09-09 17:08:33 -04:00
|
|
|
|
2021-01-14 10:10:46 -05:00
|
|
|
initPerformanceBar(document.querySelector('#js-peek'));
|
2020-09-16 08:10:15 -04:00
|
|
|
initPerformanceBarLog();
|
|
|
|
|
2020-09-09 17:08:33 -04:00
|
|
|
export default initPerformanceBar;
|