【问题标题】:how to measure function execute time in nodejs? [duplicate]如何测量nodejs中的函数执行时间? [复制]
【发布时间】:2019-03-30 03:26:47
【问题描述】:

我有很多功能的 nodejs 应用程序。

如何记录执行每个函数所需的时间?

例如,我的应用程序应该执行很多功能:

execfn1(); -> should output in some log, takes: 1ms..
execfn2(); -> should output in some log, takes: 2ms..
execfn3(); -> should output in some log, takes: 3ms..
execfn4(); -> should output in some log, takes: 5ms..
execfnN(); -> should output in some log, takes: 7ms..

我不能将每个函数都包装在 begin 和 end 中,例如:var endtimer = starttimer(execfnN);

【问题讨论】:

    标签: javascript node.js performance-testing v8


    【解决方案1】:

    使用process.hrtime,它很快并以纳秒为单位返回时间。

    这是在节点中测量短期持续时间的常用解决方案。

    大多数情况下,您仍需要测量重复操作以获得有意义的数字。您可以通过循环或使用累加器 (exemple of accumulator for node micro benchmarking) 来做到这一点。

    【讨论】:

    • 但是,我问如何在不包装每个函数的情况下做到这一点..?
    • @jackbauer 然后寻找 profiler。这会不太精确,但它很容易设置,并且可以让您发现意外缓慢的功能
    猜你喜欢
    • 2020-07-16
    • 2014-10-04
    • 1970-01-01
    • 2011-06-14
    • 2015-06-28
    • 2014-11-26
    • 2020-07-08
    • 2010-09-23
    相关资源
    最近更新 更多