fix profiling

This commit is contained in:
antelle 2020-03-29 11:06:05 +02:00
parent 1f5cd55272
commit 7c16a79629
No known key found for this signature in database
GPG Key ID: 094A2F2D6136A4EE
1 changed files with 20 additions and 18 deletions

View File

@ -2,13 +2,9 @@ import { Logger } from 'util/logger';
const logger = new Logger('start-profiler');
const networkTime = getNetworkTime();
let lastTs = window.htmlLoadTime;
const operations = [
{ name: 'fetching', elapsed: networkTime },
{ name: 'parsing', elapsed: lastTs - networkTime }
];
const operations = [];
const StartProfiler = {
milestone(name) {
@ -19,6 +15,12 @@ const StartProfiler = {
},
report() {
const networkTime = this.getNetworkTime();
operations.unshift(
{ name: 'fetching', elapsed: networkTime },
{ name: 'parsing', elapsed: window.htmlLoadTime - networkTime }
);
const time = Math.round(performance.now());
const details = operations.map(op => `${op.name}=${Math.round(op.elapsed)}ms`).join(', ');
let message = `Started in ${time}ms: ${details}.`;
@ -36,22 +38,22 @@ const StartProfiler = {
reportAppProfile(data) {
this.appProfile = data;
},
getNetworkTime() {
let perfEntry;
if (performance.getEntriesByType) {
[perfEntry] = performance.getEntriesByType('navigation');
}
if (!perfEntry || !perfEntry.responseEnd || !perfEntry.fetchStart) {
perfEntry = performance.timing;
}
return perfEntry.responseEnd - perfEntry.fetchStart;
}
};
function getNetworkTime() {
let perfEntry;
if (performance.getEntriesByType) {
[perfEntry] = performance.getEntriesByType('navigation');
}
if (!perfEntry || !perfEntry.responseEnd || !perfEntry.fetchStart) {
perfEntry = performance.timing;
}
return perfEntry.responseEnd - perfEntry.fetchStart;
}
StartProfiler.milestone('pre-init');
export { StartProfiler };