如何使用node.js perf_hooks以适当的顺序记录性能?
我正在使用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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论