跳至主要內容

contentTracing

從 Chromium 收集追蹤資料,以找出效能瓶頸和緩慢的操作。

進程: 主進程

此模組不包含網頁介面。若要檢視記錄的追蹤,請使用 追蹤檢視器,該檢視器可在 Chrome 的 chrome://tracing 中找到。

注意:您不應該使用此模組,直到發出 app 模組的 ready 事件。

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

傳回 Promise<string[]> - 一旦所有子進程確認 getCategories 請求,則以類別群組陣列解析。

取得一組類別群組。當到達新的程式碼路徑時,類別群組可能會變更。另請參閱內建追蹤類別的清單

注意:Electron 新增一個名為 "electron" 的非預設追蹤類別。此類別可用於擷取 Electron 特定的追蹤事件。

contentTracing.startRecording(options)

傳回 Promise<void> - 一旦所有子進程確認 startRecording 請求,則解析。

開始在所有進程上進行記錄。

當子進程收到 EnableRecording 請求時,會立即在本地非同步開始記錄。

如果記錄已經在執行,則承諾會立即解析,因為一次只能進行一個追蹤操作。

contentTracing.stopRecording([resultFilePath])

  • resultFilePath 字串 (可選)

傳回 Promise<string> - 一旦所有子進程確認 stopRecording 請求,則以包含追蹤資料的檔案路徑解析。

停止在所有進程上進行記錄。

子進程通常會快取追蹤資料,並且很少將追蹤資料刷新並傳送回主進程。這有助於盡量減少追蹤的執行時間額外負荷,因為透過 IPC 傳送追蹤資料可能是一項昂貴的操作。因此,若要結束追蹤,Chromium 會非同步要求所有子進程刷新任何擱置的追蹤資料。

追蹤資料將會寫入 resultFilePath。如果 resultFilePath 為空或未提供,則追蹤資料將會寫入暫存檔,並且路徑將會在承諾中傳回。

contentTracing.getTraceBufferUsage()

傳回 Promise<Object> - 以包含追蹤緩衝區最大使用量的 valuepercentage 的物件解析

  • value 數字
  • percentage 數字

取得追蹤緩衝區在各進程中的最大使用量,以完整狀態的百分比表示。