Calculate the duration of a function call inside the Google Trace Event Format

Viewed 57

I am using pupeteer to profile the performance of a webpage initialization:

 import puppeteer from "puppeteer";
 const browser = await puppeteer.launch({ headless: false });
  const page = await browser.newPage();
  await page.tracing.start({ path: './profile.json', screenshots: true});
  await page.goto("https://stackoverflow.com/", {
    waitUntil: 'domcontentloaded',
    timeout: 120000
  });
  try {
    await page.waitForNetworkIdle({ idleTime: 2000, timeout: 120000 });
  } catch(e) {
    console.warn("Network didn't idle");
  }
  await page.tracing.stop();
  await browser.close();

Running that code generates a "profile.json" (in the Trace Event Format).

Importing this profile.json into Chrome shows a nice aggregate time calculation for each profiled function execution:

Preview of Chrome DevTools showing the aggregated time for a function call

Inside the JSON there is a ProfileChunk containing multiple CPU nodes which include the very same function call (the third entry inside args.data.cpuProfile.nodes):

"args": {
  "data": {
    "cpuProfile": {
      "nodes": [
        {
          "callFrame": {
            "codeType": "JS",
            "columnNumber": 29925,
            "functionName": "ready",
            "lineNumber": 1,
            "scriptId": 146,
            "url": "https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"
          },
          "id": 638,
          "parent": 637
        },
        {
          "callFrame": {
            "codeType": "JS",
            "columnNumber": 28148,
            "functionName": "fireWith",
            "lineNumber": 1,
            "scriptId": 146,
            "url": "https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"
          },
          "id": 639,
          "parent": 638
        },
        {
          "callFrame": {
            "codeType": "JS",
            "columnNumber": 27374,
            "functionName": "i",
            "lineNumber": 1,
            "scriptId": 146,
            "url": "https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"
          },
          "id": 640,
          "parent": 639
        },
        {
          "callFrame": {
            "codeType": "JS",
            "columnNumber": 31004,
            "functionName": "",
            "lineNumber": 1,
            "scriptId": 146,
            "url": "https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"
          },
          "id": 641,
          "parent": 640
        }
      ],
      "samples": [
        637,
        639,
        640,
        641,
        641,
        641,
        641,
        641,
        641,
        641,
        641,
        641,
        641,
        641,
        641,
        641,
        641,
        641,
        641,
        641,
        641,
        641,
        641,
        641,
        641,
        641,
        641,
        641,
        641,
        641,
        641,
        641,
        641,
        641,
        641,
        641,
        641,
        641,
        641,
        641,
        641,
        641,
        641,
        641,
        641,
        641,
        641,
        641,
        641,
        641,
        641,
        641,
        641,
        641,
        641,
        641,
        641,
        641,
        641,
        641,
        641,
        641,
        641,
        641,
        641,
        641,
        641,
        641,
        641,
        641,
        641,
        641,
        641,
        641,
        641,
        641,
        641,
        641,
        641,
        641,
        641,
        641,
        641,
        641,
        641,
        641,
        641,
        641,
        641,
        641,
        641,
        641,
        641,
        641,
        641,
        641,
        641,
        641,
        641,
        641
      ]
    },
    "lines": [
      2,
      2,
      2,
      2,
      2,
      2,
      2,
      2,
      2,
      2,
      2,
      2,
      2,
      2,
      2,
      2,
      2,
      2,
      2,
      2,
      2,
      2,
      2,
      2,
      2,
      2,
      2,
      2,
      2,
      2,
      2,
      2,
      2,
      2,
      2,
      2,
      2,
      2,
      2,
      2,
      2,
      2,
      2,
      2,
      2,
      2,
      2,
      2,
      2,
      2,
      2,
      2,
      2,
      2,
      2,
      2,
      2,
      2,
      2,
      2,
      2,
      2,
      2,
      2,
      2,
      2,
      2,
      2,
      2,
      2,
      2,
      2,
      2,
      2,
      2,
      2,
      2,
      2,
      2,
      2,
      2,
      2,
      2,
      2,
      2,
      2,
      2,
      2,
      2,
      2,
      2,
      2,
      2,
      2,
      2,
      2,
      2,
      2,
      2,
      2
    ],
    "timeDeltas": [
      116,
      135,
      128,
      264,
      222,
      1450,
      136,
      388,
      380,
      144,
      147,
      129,
      143,
      131,
      166,
      140,
      214,
      166,
      179,
      182,
      174,
      128,
      139,
      121,
      131,
      163,
      138,
      141,
      117,
      144,
      135,
      316,
      126,
      163,
      163,
      123,
      128,
      133,
      166,
      147,
      182,
      138,
      1084,
      202,
      147,
      126,
      1624,
      105,
      150,
      145,
      103,
      140,
      130,
      131,
      169,
      165,
      108,
      204,
      72,
      109,
      128,
      142,
      113,
      140,
      114,
      138,
      129,
      145,
      137,
      163,
      141,
      146,
      154,
      151,
      131,
      133,
      130,
      132,
      131,
      131,
      130,
      131,
      131,
      131,
      130,
      133,
      133,
      155,
      137,
      160,
      116,
      119,
      164,
      127,
      281,
      133,
      120,
      152,
      128,
      129
    ]
  }
},
"cat": "disabled-by-default-v8.cpu_profiler",
"id": "0x1",
"name": "ProfileChunk",
"ph": "P",
"pid": 90837,
"tid": 71195,
"ts": 795527225633,
"tts": 96468

Is there any way to calculate the duration programmatically from the profile.json?

0 Answers
Related