设置性能分析

学习如何在你的应用中启用性能分析(如果尚未设置)。

通过 性能分析,Sentry 通过在各种环境中采样程序的调用栈来跟踪软件的性能。此功能收集代码的函数级信息,使您可以优化程序的性能。Sentry 的性能分析器捕获函数调用及其确切位置,对其进行聚合,并显示程序中最常见的代码路径。这突出了您可以优化的区域,以帮助提高代码性能和用户满意度。

在浏览器环境中,性能分析可以帮助您确定导致 UI 卡顿的原因,揭示为什么像交互到下次绘制 (INP) 这样的值表现不佳,或者为什么一个长时间任务阻止了浏览器重新绘制屏幕并导致帧丢失。所有这些信息都可以帮助您修复实际的性能问题,并为用户提供更流畅的用户体验。

性能分析依赖于 Sentry 的 Tracing 功能已预先启用。要在 SDK 中启用跟踪:

Copied
Sentry.init({
  dsn: "https://examplePublicKey@o0.ingest.sentry.io/0",
  // Set tracesSampleRate to 1.0 to capture 100% of transactions for tracing.
  // We recommend adjusting this value in production.
  // Learn more at
  // https://docs.sentry.io/platforms/javascript/configuration/options/#traces-sample-rate
  tracesSampleRate: 1.0,
});

查看 跟踪设置文档 以获取有关如何配置采样的更详细信息。将采样率设置为 1.0 意味着所有事务都将被捕获。

React Native 的性能分析在 SDK 版本 5.32.0 及以上可用。

要启用性能分析,设置 profilesSampleRate

Copied
Sentry.init({
  dsn: "https://examplePublicKey@o0.ingest.sentry.io/0",
  // Set tracesSampleRate to 1.0 to capture 100% of transactions for tracing.
  // We recommend adjusting this value in production.
  // Learn more at
  // https://docs.sentry.io/platforms/javascript/configuration/options/#traces-sample-rate
  tracesSampleRate: 1.0,
  // profilesSampleRate is relative to tracesSampleRate.
  // Here, we'll capture profiles for 100% of transactions.
  profilesSampleRate: 1.0,
});

要更改 React Native 性能分析选项,请在 Sentry.init 的集成中添加 hermesProfilingIntegration

Copied
Sentry.init({
  integrations: [
    Sentry.hermesProfilingIntegration({
      platformProfilers: false,
    }),
  ],
});

默认值为 true,平台性能分析器已启用。默认情况下,React Native JS 代码(在 Hermes 引擎中执行)和平台特定代码(Swift、Objective-C、Kotlin、Java)分别由各自的性能分析器进行分析。将 platformProfilers 设置为 false 将禁用平台特定代码的性能分析,仅分析在 Hermes 中执行的 JS 代码。此选项自 SDK 版本 5.33.0 起可用。