【问题标题】:How to enable and disable perf_hook based on Flag如何根据 Flag 启用和禁用 perf_hook
【发布时间】:2022-02-03 04:15:15
【问题描述】:

我们有大量的代码,其中有很多函数被串联调用。为了提高性能,我们希望测量到特定点之间的时间。

我们要逐步改进。我正在考虑让perf_hook 的用户 我们最好的用例将使用如下,根据 lib

const { PerformanceObserver, performance } = require('perf_hooks');

const obs = new PerformanceObserver((items) => {
  console.log(items.getEntries()[0].duration);
  performance.clearMarks();
});

obs.observe({ type: 'measure' });
performance.measure('Start to Now');

performance.mark('A');
doSomeLongRunningProcess(() => {
  performance.measure('A to Now', 'A');

  performance.mark('B');
  performance.measure('A to B', 'A', 'B');
});

我在 lib 和 web 中查看和搜索,没有找到任何 env 变量来启用或禁用 perf_hook,因此在 PROD 阶段启用和禁用代码。

【问题讨论】:

    标签: node.js npm node-modules


    【解决方案1】:

    玩弄代码。

    根据条件进行声明

    let obs = null;
    //ACTIVATE or DEACTIVATE
    if( true || false) {
       obs = new PerformanceObserver((items) => {
                  console.log(items.getEntries()[0].duration);
                  performance.clearMarks();
        });
        obs.observe({ type: 'measure' });
    
    } 
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-08-24
    • 2014-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多