![](/img/trans.png)
[英]Using Puppeteer & Chrome DevTools Protocol, how to get the 4 Performance Timing metrics?
[英]How can I get the network traffic metrics using puppeteer
要獲取使用 Puppeteer 時 Chromium 傳輸的 kB 和使用的 MB 資源,可以使用performance.getEntries()
方法。 此方法返回一個性能條目列表,其中包括有關加載的資源和加載時間的信息。 然后您可以遍歷此列表並計算傳輸的總 kB 和使用的 MB 資源。
要以分鍾為單位獲取完成時間,可以使用performance.timing.loadEventEnd
屬性,該屬性指示當前頁面的加載事件完成的時間,以毫秒為單位。 然后,您可以將該值除以 1000 以將其轉換為秒,然后再除以 60 以將其轉換為分鍾。
下面是一個示例,說明如何使用這些方法和屬性來獲取傳輸的 kB、使用的 MB 資源以及以分鍾為單位的完成時間:
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto('https://example.com');
// Get the performance entries
const performanceEntries = await page.evaluate(() => performance.getEntries());
// Calculate the total kB transferred and MB resources used
let kBTransferred = 0;
let MBResources = 0;
performanceEntries.forEach((entry) => {
kBTransferred += entry.transferSize / 1024;
MBResources += entry.decodedBodySize / 1024 / 1024;
});
// Get the finish time in milliseconds
const finishTime = await page.evaluate(() => performance.timing.loadEventEnd);
// Convert the finish time to minutes
const finishTimeMinutes = finishTime / 1000 / 60;
console.log(`kB Transferred: ${kBTransferred}`);
console.log(`MB Resources: ${MBResources}`);
console.log(`Finish Time (minutes): ${finishTimeMinutes}`);
await browser.close();
})();
請記住,這些值僅適用於瀏覽器中當前加載的頁面。 如果要測量多個頁面使用的網絡流量和資源,則需要對每個頁面重復此過程。
要獲得傳輸的 kB(所有網絡流量)和 MB 資源,並在幾分鍾內完成時間,您需要結合使用系統實用程序和編程技術。 以下是您可以遵循的一些常規步驟:
首先,您需要收集有關您的網絡流量和資源使用情況的數據。 這通常可以使用系統實用程序(例如 .netstat、vmstat 或 ps)來完成。 這些實用程序可以提供有關通過網絡傳輸的數據量、memory 的數量和正在使用的 CPU 資源以及其他性能指標的信息。
接下來,您將需要處理此數據以提取您感興趣的特定指標。這通常可以使用編程技術來完成,例如解析文本 output、計算平均值或總和,或使用正則表達式提取特定數據點.
提取相關指標后,您可以通過將經過的總時間除以每小時的分鍾數 (60) 來計算以分鍾為單位的完成時間。 例如,如果經過的總時間為 120 秒,則以分鍾為單位的完成時間將為 2 分鍾(120 秒/每分鍾 60 秒)。
最后,您可以 output 以適合您需要的格式收集和計算的指標。 這可能涉及在屏幕上顯示數據、將其寫入文件或將其存儲在數據庫中以供進一步分析。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.