跳转到主内容

contentTracing

从Chromium收集追踪数据以找到性能瓶颈和慢操作。

ProcessMain

此模块不包括 Web 界面。 Traces are recorded in the Perfetto protobuf format; to view them, open them in the Perfetto UI.

[!NOTE] You should not use this module until the ready event of the app module is emitted.

const { app, contentTracing } = require('electron')

app.whenReady().then(async () => {
await contentTracing.startRecording({
included_categories: ['*']
})
console.log('Tracing started')
await new Promise((resolve) => setTimeout(resolve, 5000))
const path = await contentTracing.stopRecording()
console.log('Tracing data recorded to ' + path)
})

方法​

contentTracing模块包含以下方法:

contentTracing.getCategories()​

History

返回 Promise<string[]> - 一旦所有子进程都确认了 getCategories 请求,会 resolve 一个类别组的数组

获取一组类别组。当到达新的代码路径时,类别组可能会更改。 See also the list of built-in tracing categories.

NOTE: Electron adds a non-default tracing category called "electron". 此类别可以用于捕捉Electron特定的追踪事件。

contentTracing.startRecording(options)​

History
Version(s)Changes
None

The options parameter now accepts TraceConfig in addition to TraceCategoriesAndOptions.

None

This function now returns a callbackPromise<void>.

None

Added the nativeHeapProfiling option to enable native heap profiling.

返回 Promise<void> - 当所有子进程都确认了 startRecording 请求后 resolve.

在所有进程上开始记录

一旦收到EnableRecording请求,记录立即在本地开始进行,并在子进程上异步执行。

如果一个记录已经运行了,promise将立即resolve,因为一次只能进行一个跟踪操作。 If options.nativeHeapProfiling is set while a recording is already running, the promise is rejected instead, since the already-running recording's options can't be changed to turn on heap profiling.

contentTracing.stopRecording([resultFilePath])​

History
  • resultFilePath string (可选)

返回 Promise<string> - 一旦所有子进程都确认了 stopRecording 请求,会 resolve 一个包含了追踪数据的文件路径

停止所有进程记录。

子进程通常缓存跟踪数据,并且很少清空和发送跟踪数据回到主进程。这有助于最小化运行时间开销,因为通过IPC发送跟踪数据可能是一个开销巨大的操作。因此,为了结束跟踪,Chromium异步地要求所有子进程刷新所有挂起的跟踪数据。

追踪数据将被写入 resultFilePath。如果 resultFilePath 是空的或未提供,追踪数据将被写入临时文件,并且路径将在promise中返回。

Trace data is written in the Perfetto protobuf format.

Heap profiling samples record instruction addresses and module build IDs rather than function names. To symbolize them, use the symbols published for the matching Electron release with Chromium's symbolize_trace tool.

contentTracing.getTraceBufferUsage()​

History

返回 Promise<Object> - resolve一个包含了追踪缓冲器的 value 和 percentage 最大使用量的对象

  • value number
  • percentage number

获取追踪缓冲区在进程间的最大使用量(占全部状态的百分比)。