使用可擴充的 API 自訂效能資料

Andrés Olivares
Andrés Olivares
Sofia Emelianova
Sofia Emelianova

總覽

「Performance」面板支援成效擴充功能 API,可讓您在成效時間軸中新增自訂資料。

這個 API 可運用現有的 User Timings API,讓您將測量資料和事件直接插入成效時間軸,做為自訂追蹤或 Timings 追蹤。這項功能可能對使用自訂檢測功能的架構、程式庫和複雜應用程式的開發人員有所助益,可讓他們更全面地瞭解效能。

主要功能與特色

  • 自訂曲目:建立專屬曲目,以視覺化方式呈現自己的時間資料。
  • 項目:使用代表事件或評估項目的項目填入軌跡。
  • 工具提示和詳細資料:使用可自訂的工具提示和詳細檢視畫面,為項目提供豐富的背景資訊。
  • 標記:使用視覺標記,在時間軸中標示特定片段。

使用 User Timings API 插入資料

如要插入自訂資料,請在 performance.markperformance.measure 方法的 detail 屬性中加入 devtools 物件。這個 devtools 物件的結構會決定資料在「成效」面板中的顯示方式。

  • 使用 performance.mark 在時間軸上記錄即時事件或時間戳記。您可以標示特定作業的開始或結束時間,或是任何沒有時間長度的重要事件。當您在 detail 屬性中加入 devtools 物件後,「Performance」(效能) 面板會在時間軸中顯示自訂標記。

  • 使用 performance.measure 測量工作或程序的時間長度。在 detail 屬性中加入 devtools 物件後,「Performance」面板會在時間軸中顯示自訂評估項目。如果您使用 performance.mark 做為參照點來建立 performance.measure,則不需要在 performance.mark 呼叫中加入 devtools 物件。

devtools 個物件

這些類型會針對不同的擴充功能功能定義 devtools 物件的結構:

type DevToolsColor =
  "primary" | "primary-light" | "primary-dark" |
  "secondary" | "secondary-light" | "secondary-dark" |
  "tertiary" | "tertiary-light" | "tertiary-dark" |
  "error";

interface ExtensionTrackEntryPayload {
  dataType?: "track-entry"; // Defaults to "track-entry"
  color?: DevToolsColor;    // Defaults to "primary"
  track: string;            // Required: Name of the custom track
  trackGroup?: string;      // Optional: Group for organizing tracks
  properties?: [string, string][]; // Key-value pairs for detailed view
  tooltipText?: string;     // Short description for tooltip
}

interface ExtensionMarkerPayload {
  dataType: "marker";       // Required: Identifies as a marker
  color?: DevToolsColor;    // Defaults to "primary"
  properties?: [string, string][]; // Key-value pairs for detailed view
  tooltipText?: string;     // Short description for tooltip
}

在時間軸上查看資料

如要在時間軸中查看自訂資料,請在「成效」面板中依序開啟「設定」「擷取設定」>「擴充資料」核取方塊

效能面板「擷取設定」中的「擴充資料」核取方塊。

歡迎到這個示範頁面試用看看。開啟「擴充資料」,開始錄製效能,按一下示範頁面上的「新增 Corgi」,然後停止錄製。您會在追蹤記錄中看到自訂軌跡,其中包含事件,並在「摘要」分頁中顯示自訂工具提示和詳細資料。

程式碼範例

請參考後續各節中的程式碼範例,瞭解如何在成效時間軸中加入以下內容:

自訂測試群組與項目

建立自訂追蹤,並填入項目,以便在自訂追蹤中以圖表呈現成效資料。例如:

// Mark used to represent the start of an image processing task
performance.mark("Image Processing Start");

// ... later in your code

// Track entry representing the completion of image processing
// with additional details and a tooltip
performance.measure("Image Processing Complete", "Image Processing Start", {
  detail: {
    devtools: {
      dataType: "track-entry",
      track: "Image Processing Tasks",
      trackGroup: "My Tracks", // Group related tracks together
      color: "tertiary-dark",
      properties: [
        ["Filter Type", "Gaussian Blur"],
        ["Resize Dimensions", "500x300"]
      ],
      tooltipText: "Image processed successfully"
    }
  }
});

這會在成效時間軸中產生下列自訂追蹤項目,以及相關工具提示文字和屬性:

成效時間軸中的自訂軌跡。

標記

使用跨越所有音軌的自訂標記,在時間軸上視覺化突顯特定搜尋點。例如:

// Marker indicating when the processed image was uploaded
performance.mark("Image Upload", {
  detail: {
    devtools: {
      dataType: "marker",
      color: "secondary",
      properties: [
        ["Image Size", "2.5MB"],
        ["Upload Destination", "Cloud Storage"]
      ],
      tooltipText: "Processed image uploaded"
    }
  }
});

這會在「Timings」軌道中產生下列標記,以及相關的工具提示文字和屬性:

時間軸中的自訂標記。