2020-03-29 09:23:49 +02:00
|
|
|
import { Logger } from 'util/logger';
|
|
|
|
|
|
|
|
const logger = new Logger('start-profiler');
|
|
|
|
|
2020-03-29 09:55:17 +02:00
|
|
|
let lastTs = window.htmlLoadTime;
|
2020-03-29 09:23:49 +02:00
|
|
|
|
2020-03-29 11:06:05 +02:00
|
|
|
const operations = [];
|
2020-03-29 09:23:49 +02:00
|
|
|
|
|
|
|
const StartProfiler = {
|
|
|
|
milestone(name) {
|
|
|
|
const ts = logger.ts();
|
|
|
|
const elapsed = ts - lastTs;
|
|
|
|
lastTs = ts;
|
|
|
|
operations.push({ name, elapsed });
|
|
|
|
},
|
|
|
|
|
|
|
|
report() {
|
2020-03-29 11:06:05 +02:00
|
|
|
const networkTime = this.getNetworkTime();
|
|
|
|
operations.unshift(
|
|
|
|
{ name: 'fetching', elapsed: networkTime },
|
|
|
|
{ name: 'parsing', elapsed: window.htmlLoadTime - networkTime }
|
|
|
|
);
|
|
|
|
|
2020-03-29 09:23:49 +02:00
|
|
|
const time = Math.round(performance.now());
|
2020-03-29 10:59:40 +02:00
|
|
|
const details = operations.map(op => `${op.name}=${Math.round(op.elapsed)}ms`).join(', ');
|
|
|
|
let message = `Started in ${time}ms: ${details}.`;
|
|
|
|
|
|
|
|
if (this.appProfile) {
|
|
|
|
message += ` Electron app started in ${this.appProfile.totalTime}ms: `;
|
|
|
|
message +=
|
|
|
|
this.appProfile.timings
|
|
|
|
.map(op => `${op.name}=${Math.round(op.elapsed)}ms`)
|
|
|
|
.join(', ') + '.';
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.info(message);
|
|
|
|
},
|
|
|
|
|
|
|
|
reportAppProfile(data) {
|
|
|
|
this.appProfile = data;
|
2020-03-29 11:06:05 +02:00
|
|
|
},
|
2020-03-29 09:23:49 +02:00
|
|
|
|
2020-03-29 11:06:05 +02:00
|
|
|
getNetworkTime() {
|
|
|
|
let perfEntry;
|
2020-03-29 09:55:17 +02:00
|
|
|
|
2020-03-29 11:06:05 +02:00
|
|
|
if (performance.getEntriesByType) {
|
|
|
|
[perfEntry] = performance.getEntriesByType('navigation');
|
|
|
|
}
|
|
|
|
if (!perfEntry || !perfEntry.responseEnd || !perfEntry.fetchStart) {
|
|
|
|
perfEntry = performance.timing;
|
|
|
|
}
|
2020-03-29 09:55:17 +02:00
|
|
|
|
2020-03-29 11:06:05 +02:00
|
|
|
return perfEntry.responseEnd - perfEntry.fetchStart;
|
|
|
|
}
|
|
|
|
};
|
2020-03-29 09:55:17 +02:00
|
|
|
|
2020-03-29 09:23:49 +02:00
|
|
|
StartProfiler.milestone('pre-init');
|
|
|
|
|
|
|
|
export { StartProfiler };
|