Profiler

OpenReplay 的 Profiler 插件。

Profiler

profiler 插件让你能够测量 JS 函数的性能,并捕获每次调用的参数和返回结果。

npm i @openreplay/tracker-profiler

初始化 tracker 并将插件加载到其中。然后用生成的函数装饰代码中的任意函数。

如果你的网站是单页应用(SPA)

Section titled 如果你的网站是单页应用(SPA)
import OpenReplay from '@openreplay/tracker';
import trackerProfiler from '@openreplay/tracker-profiler';

const tracker = new OpenReplay({
  projectKey: PROJECT_KEY
});
const profiler = tracker.use(trackerProfiler());

tracker.start();

// ...
const fn = profiler('MyFunction')(() => {
  // Inspecting function body
}, thisArg); // thisArg is optional

如果你的 Web 应用是服务端渲染(SSR)

Section titled 如果你的 Web 应用是服务端渲染(SSR)

如果你的应用是 SSR,请参照下面的示例。确保 tracker.start() 在应用启动后调用(在 useEffectcomponentDidMount 中)。

import OpenReplay from '@openreplay/tracker/cjs';
import trackerProfiler from '@openreplay/tracker-profiler/cjs';
//...
const tracker = new OpenReplay({
  projectKey: PROJECT_KEY
});
const profiler = tracker.use(trackerProfiler());

//...
function MyApp() {
  useEffect(() => { // use componentDidMount in case of React Class Component
    tracker.start();
  }, [])
}
//...
const fn = profiler('MyFunction')(() => {
  // Inspecting function body
}, thisArg); // thisArg is optional

在设置此插件时遇到问题?请加入我们的 Slack 或访问我们的论坛,从我们的社区获取帮助。