Skip to content

Commit 67dea09

Browse files
committed
⚡ perf: optimize the accuracy of pageLoadTime metrics collection
1 parent 702846b commit 67dea09

File tree

2 files changed

+18
-10
lines changed

2 files changed

+18
-10
lines changed

packages/tools/benchmark.js

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -127,11 +127,17 @@ async function openBrowser(bundler) {
127127
}
128128

129129
await giveSomeRest();
130-
const loadPromise = page.waitForEvent("load", { timeout: 30000 });
131-
const pageLoadStart = Date.now();
132-
page.goto(`http://localhost:${bundler.port}`);
133-
await loadPromise;
134-
return { page, time: Date.now() - pageLoadStart };
130+
await page.goto(`http://localhost:${bundler.port}`, {
131+
timeout: 30000,
132+
waitUntil: "load",
133+
});
134+
const loadTime = await page.evaluate(() => {
135+
return (
136+
window.performance.timing.loadEventEnd -
137+
window.performance.timing.navigationStart
138+
);
139+
});
140+
return { page, time: loadTime };
135141
}
136142

137143
async function closePage() {
@@ -203,10 +209,7 @@ async function build(bundler) {
203209
cleanDistDir(bundler);
204210
await cleanServerCache(bundler);
205211
await giveSomeRest(1000);
206-
const productionStart = Date.now();
207-
await startProductionBuild(bundler);
208-
const productionEnd = Date.now();
209-
return productionEnd - productionStart;
212+
return await startProductionBuild(bundler);
210213
}
211214
return -1;
212215
}

packages/tools/buildTools.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,16 +94,21 @@ export class BuildTool {
9494
);
9595
this.child = child;
9696
return new Promise((resolve, reject) => {
97+
let startTime = null;
98+
child.on("spawn", () => {
99+
startTime = new Date();
100+
});
97101
child.on("error", (error) => {
98102
console.log(`${this.name} error: ${error.message}`);
99103
reject(error);
100104
});
101105
child.on("exit", (code) => {
106+
const endTime = new Date();
102107
if (code !== null && code !== 0 && code !== 1) {
103108
console.log(`${this.name} exit: ${code}`);
104109
reject(code);
105110
}
106-
resolve();
111+
resolve(endTime - startTime);
107112
});
108113
});
109114
}

0 commit comments

Comments
 (0)