Profiler
profiler 插件让您能够衡量 JS 函数的性能,并捕获每次调用的参数和结果。
npm i @openreplay/tracker-profiler
初始化 tracker 并将插件加载到其中。然后使用生成的函数来装饰代码中的任意函数。
如果您的网站是单页应用程序(SPA)
Section titled 如果您的网站是单页应用程序(SPA)import trackerProfiler from '@openreplay/tracker-profiler';
import { tracker } from '@openreplay/tracker';
tracker.configure({
projectKey: 'YOUR_PROJECT_KEY',
ingestPoint: "https://openreplay.mydomain.com/ingest", // when dealing with the self-hosted version of OpenReplay
});
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() 在应用启动后被调用(在 useEffect 或 componentDidMount 中)。
import trackerProfiler from '@openreplay/tracker-profiler/cjs';
import { tracker } from '@openreplay/tracker';
tracker.configure({
projectKey: 'YOUR_PROJECT_KEY',
ingestPoint: "https://openreplay.mydomain.com/ingest", // when dealing with the self-hosted version of OpenReplay
});
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