简体   繁体   中英

How can I get the network traffic metrics using puppeteer

How can I get the kB transferred (all.network traffic) and MB resources, and Finish time in minutes?

在此处输入图像描述

To get the kB transferred and MB resources used by Chromium when using Puppeteer, you can use the performance.getEntries() method. This method returns a list of performance entries, which includes information about the resources that were loaded and how long they took to load. You can then iterate over this list and calculate the total kB transferred and MB resources used.

To get the finish time in minutes, you can use the performance.timing.loadEventEnd property, which indicates the time at which the load event of the current page finished, in milliseconds. You can then divide this value by 1000 to convert it to seconds, and then divide it by 60 to convert it to minutes.

Here's an example of how you can use these methods and properties to get the kB transferred, MB resources used, and finish time in minutes:

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();
 })();

Keep in mind that these values will only be accurate for the page that is currently loaded in the browser. If you want to measure the.network traffic and resources used for multiple pages, you will need to repeat this process for each page.

To get the kB transferred (all.network traffic) and MB resources, and finish time in minutes, you will need to use a combination of system utilities and programming techniques. Here are some general steps that you can follow:

First, you will need to gather data about your.network traffic and resource usage. This can typically be done using system utilities such as.netstat, vmstat, or ps. These utilities can provide information about the amount of data transferred over the.network, the amount of memory and CPU resources being used, and other performance metrics.

Next, you will need to process this data in order to extract the specific metrics that you are interested in. This can typically be done using programming techniques such as parsing text output, calculating averages or sums, or using regular expressions to extract specific data points.

Once you have extracted the relevant metrics, you can calculate the finish time in minutes by dividing the total time elapsed by the number of minutes per hour (60). For example, if the total time elapsed is 120 seconds, the finish time in minutes would be 2 minutes (120 seconds / 60 seconds per minute).

Finally, you can output the metrics that you have gathered and calculated in a format that is suitable for your needs. This could involve displaying the data on the screen, writing it to a file, or storing it in a database for further analysis.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM