Skip to main content

contentTracing

从 Chromium 收集跟踪数据以查找性能瓶颈和缓慢的操作。

¥Collect tracing data from Chromium to find performance bottlenecks and slow operations.

进程:主进程

¥Process: Main

该模块不包括网络界面。要查看记录的跟踪,请使用 跟踪查看器(在 Chrome 中可通过 chrome://tracing 获取)。

¥This module does not include a web interface. To view recorded traces, use trace viewer, available at chrome://tracing in Chrome.

注意:在发出应用模块的 ready 事件之前,不应使用此模块。

¥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)
})()
})

方法

¥Methods

contentTracing 模块有以下方法:

¥The contentTracing module has the following methods:

contentTracing.getCategories()

返回 Promise<string[]> - 一旦所有子进程都确认了 getCategories 请求,就会解析一系列类别组

¥Returns Promise<string[]> - resolves with an array of category groups once all child processes have acknowledged the getCategories request

获取一组类别组。当到达新的代码路径时,类别组可能会发生变化。另请参见 内置跟踪类别列表

¥Get a set of category groups. The category groups can change as new code paths are reached. See also the list of built-in tracing categories.

注意:Electron 添加了一个名为 "electron" 的非默认跟踪类别。该类别可用于捕获特定于 Electron 的跟踪事件。

¥NOTE: Electron adds a non-default tracing category called "electron". This category can be used to capture Electron-specific tracing events.

contentTracing.startRecording(options)

返回 Promise<void> - 一旦所有子进程都确认了 startRecording 请求,就解决了。

¥Returns Promise<void> - resolved once all child processes have acknowledged the startRecording request.

开始记录所有进程。

¥Start recording on all processes.

一旦子进程收到 EnableRecording 请求,就会立即在本地异步开始录制。

¥Recording begins immediately locally and asynchronously on child processes as soon as they receive the EnableRecording request.

如果记录已经在运行,则 promise 将立即得到解决,因为一次只能进行一个跟踪操作。

¥If a recording is already running, the promise will be immediately resolved, as only one trace operation can be in progress at a time.

contentTracing.stopRecording([resultFilePath])

  • resultFilePath 字符串(可选)

    ¥resultFilePath string (optional)

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

¥Returns Promise<string> - resolves with a path to a file that contains the traced data once all child processes have acknowledged the stopRecording request

停止所有进程的记录。

¥Stop recording on all processes.

子进程通常会缓存跟踪数据,并且很少刷新并将跟踪数据发送回主进程。这有助于最大限度地减少跟踪的运行时开销,因为通过 IPC 发送跟踪数据可能是一项昂贵的操作。因此,为了结束跟踪,Chromium 异步要求所有子进程刷新任何挂起的跟踪数据。

¥Child processes typically cache trace data and only rarely flush and send trace data back to the main process. This helps to minimize the runtime overhead of tracing since sending trace data over IPC can be an expensive operation. So, to end tracing, Chromium asynchronously asks all child processes to flush any pending trace data.

跟踪数据将写入 resultFilePath。如果 resultFilePath 为空或未提供,则跟踪数据将写入临时文件,并在 Promise 中返回路径。

¥Trace data will be written into resultFilePath. If resultFilePath is empty or not provided, trace data will be written to a temporary file, and the path will be returned in the promise.

contentTracing.getTraceBufferUsage()

返回 Promise<Object> - 使用包含跟踪缓冲区最大使用量 valuepercentage 的对象进行解析

¥Returns Promise<Object> - Resolves with an object containing the value and percentage of trace buffer maximum usage

  • value 数字

    ¥value number

  • percentage 数字

    ¥percentage number

获取跟踪缓冲区跨进程的最大使用量占完整状态的百分比。

¥Get the maximum usage across processes of trace buffer as a percentage of the full state.