如何使用node.js perf_hooks以适当的顺序记录性能?

发布于 2025-02-13 19:15:02 字数 2595 浏览 1 评论 0原文

我正在使用node.js 性能测量apis crope crope crope crope to profem coperip 功能为浏览器性能API 。我的代码看起来像这样:

performance.mark('start');

// Use setTimeout to get stable result
setTimeout(() => {
  performance.mark('step1');
  performance.measure('Duration of step 1', 'start', 'step1');

  performance.mark('step2');
  performance.measure('Duration of step 2', 'step1', 'step2');

  performance.mark('step3');
  performance.measure('Duration of step 3', 'step2', 'step3');
  
  // Option 1
  performance.measure('Duration of total', 'start', 'step3');
  
  // Option 2
  performance.measure('Duration of total', 'start');

  // Option 3
  performance.mark('end');
  performance.measure('Duration of total', 'start', 'end');
  
  // "Duration of total" won't appear at the end
  performance.getEntriesByType("measure").forEach((entry) => {
    console.log(`${entry.name}: ${entry.duration} ms`)
  })
}, 100)

结果将是这样的:

步骤1:111.59999999962747 MS
总计:111.699999999925494 MS
总计:111.699999999925494 MS
总计:111.699999999925494 MS
步骤2的持续时间:0.099999999962747097 MS
步骤3:0 ms

的持续时间

i尝试了三个实际上等效的选项,以测量从'start'到末尾的持续时间。但是,在记录量度时,量级的持续时间无法在结束时出现。我还检查了性能API的文档,发现getentriesbytype()的返回值是:

返回按时间顺序返回perfortryentry对象的列表。

因此,一旦我将最终度量的startmark设置为'start',量级 total 的持续时间将位于措施之后步骤1的持续时间,因为它们具有相同的开始时间。但是,我需要这样的记录,这更直观:

步骤1:111.59999999962747 MS
步骤2的持续时间:0.099999999962747097 MS
步骤3:0 ms的持续时间
总持续时间:111.699999999925494 MS


除了手动计算总时间或切换返回阵列中的措施位置外,是否有任何方法可以实现此目标

参考:
performance.mark
performance.measure


performance.getentriesbytype

I'm using Node.js Performance measurement APIs to profile my code, which has the same main functions as Browser Performance API. My code looks like this:

performance.mark('start');

// Use setTimeout to get stable result
setTimeout(() => {
  performance.mark('step1');
  performance.measure('Duration of step 1', 'start', 'step1');

  performance.mark('step2');
  performance.measure('Duration of step 2', 'step1', 'step2');

  performance.mark('step3');
  performance.measure('Duration of step 3', 'step2', 'step3');
  
  // Option 1
  performance.measure('Duration of total', 'start', 'step3');
  
  // Option 2
  performance.measure('Duration of total', 'start');

  // Option 3
  performance.mark('end');
  performance.measure('Duration of total', 'start', 'end');
  
  // "Duration of total" won't appear at the end
  performance.getEntriesByType("measure").forEach((entry) => {
    console.log(`${entry.name}: ${entry.duration} ms`)
  })
}, 100)

The result will be something like this:

Duration of step 1: 111.59999999962747 ms
Duration of total: 111.69999999925494 ms
Duration of total: 111.69999999925494 ms
Duration of total: 111.69999999925494 ms
Duration of step 2: 0.09999999962747097 ms
Duration of step 3: 0 ms

I have tried three options, which are actually equivalent, to measure the duration from 'start' to the end. But the measure Duration of total just can't appear at the end when logging measures. I also checked the document of Performance API and found that the return value of getEntriesByType() is:

Returns a list of PerformanceEntry objects in chronological order with respect to performanceEntry.startTime whose performanceEntry.entryType is equal to type.

So once I set the startMark of final measure to 'start', the measure Duration of total will be located right after the measure Duration of step 1 since they have same startTime. However, I need it to log like this, which is more intuitive:

Duration of step 1: 111.59999999962747 ms
Duration of step 2: 0.09999999962747097 ms
Duration of step 3: 0 ms
Duration of total: 111.69999999925494 ms

Is there any way to achieve this goal other than manually calculating the total time or switching the positions of measures in the return array?

Thanks for any solutions or thoughts!

Refs:
performance.mark
performance.measure
performance.getEntriesByType

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文