跳转到主内容

contentTracing

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

Process: Main

此模块不包括 Web 界面。 若要查看记录的轨迹,请使用跟踪查看器,在Chrome上可以访问 chrome://tracking

[!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('追踪数据记录到: ' + 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>.

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

在所有进程上开始记录

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

如果一个记录已经运行了,promise将立即resolve,因为一次只能进行一个跟踪操作。

contentTracing.stopRecording([resultFilePath])

History
Version(s)Changes
None

This method now returns a Promise instead of using a callback function.

None

The resultFilePath parameter is now optional.

  • resultFilePath string (可选)

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

停止所有进程记录。

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

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

contentTracing.getTraceBufferUsage()

History

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

  • value number
  • percentage number

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

contentTracing.enableHeapProfiling([options]) 实验功能

History
Version(s)Changes
None
API ADDED

Returns Promise<void> - Resolves once heap profiling has been enabled.

Enable heap profiling for MemoryInfra traces. Equivalent to the --memlog switch in Chrome.

Only takes effect if the disabled-by-default-memory-infra category is included.

Needs to be called before contentTracing.startRecording().

用法:

const { contentTracing } = require('electron')

async function recordTrace () {
await contentTracing.enableHeapProfiling()
await contentTracing.startRecording({
included_categories: ['disabled-by-default-memory-infra'],
excluded_categories: ['*'],
memory_dump_config: {
triggers: [
{ mode: 'detailed', periodic_interval_ms: 1000 }
]
}
})

await new Promise(resolve => setTimeout(resolve, 5000))

const filePath = await contentTracing.stopRecording()
}

To view the recorded heap dumps:

  1. Download the breakpad symbols for your Electron version from the Electron GitHub releases

  2. Clone the Electron source code

  3. In your Chromium checkout for Electron, run this command to symbolicate the heap dump:

    python3 third_party/catapult/tracing/bin/symbolize_trace --use-breakpad-symbols --breakpad-symbols-directory /path/to/breakpad_symbols /path/to/trace.json
  4. Open the symbolicated trace in chrome://tracing (the Perfetto UI does not support memory dumps yet)

  5. Click on one of the M symbols

  6. Click on a triple bar icon (e.g., in the malloc column)

Screenshot showing how to view a heapdump in Chromium's tracing view