我正在使用node.js测试性能。我尝试了原始请求以及无头的chrome(puppeteer)。第一个字节在这两个解决方案之间始终有间隔。我想知道哪个更准确?或者它们都是正确的,但是计算方法不同。
有人可以解释一下吗?
const request = require('request');
const puppeteer = require('puppeteer');
const url = 'https://www.google.com';
async function fromRequest() {
return new Promise((resolve, reject) => {
request({
uri: url,
method: 'GET',
time: true
}, (err, resp) => {
if (err) {
return reject(err);
}
if (!resp.timings || !resp.timingPhases) {
return resolve(null);
}
return resolve(resp);
});
});
}
async function fromHeadless() {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto(url);
const perf = JSON.parse(await page.evaluate(() => {
return JSON.stringify(performance.timing);
}));
await browser.close();
return perf;
}
(async _ => {
const ret1 = await fromRequest();
console.log(`request first byte: ${ret1.timingPhases.firstByte}`);
const ret2 = await fromHeadless();
console.log(`headless chrome first byte: ${ret2.responseStart - ret2.navigationStart}`);
})();