跳至內容

效能分析

在發布模式下編譯 oxlint 並啟用除錯資訊

為了進行效能分析,您需要以發布模式編譯 oxlint 二進制檔案,並啟用除錯資訊。您可以使用 cargo build 並傳遞 --profile release-with-debug 來完成。

bash
cargo build --profile release-with-debug --bin oxlint

建置完成後,二進制檔案位於 ./target/release-with-debug/oxlint。這就是應該用於效能分析的二進制檔案。

CPU - Samply

Samply 是一個命令列 CPU 分析器,它使用 Firefox 分析器作為其 UI。適用於 macOS 和 Linux。

要將 Samply 與 oxlint 一起使用,請執行 samply record,然後是 oxlint 命令和引數。

bash
samply record ./target/release-with-debug/oxlint .

為了改善效能分析體驗,您可以考慮以下一些選項

  • oxlint--silent 將抑制診斷輸出並使效能分析更集中。
  • oxlint--threads 1 將以單執行緒執行程式碼檢查器,這會比較慢,但更容易分析單執行緒效能的效能分析。
  • samply record--rate <number> 將以較高的速率取樣效能分析。預設值為 1000Hz (1ms),但增加此值將提供更詳細的資訊,但代價是更大的效能分析檔案。

例如,以 0.1 毫秒的取樣率執行單執行緒的 oxlint

bash
samply record --rate 10000 ./target/release-with-debug/oxlint --silent --threads 1 .

CPU - Mac Xcode Instruments

cargo instruments 是橋接 Mac Xcode instruments 的首選工具。

以下指令複製 cargo instruments 的程序。

首先,安裝 Xcode Instruments 命令列工具

bash
xcode-select --install

然後,如果您還沒有這麼做,請確保已編譯 oxlint 二進制檔案

在底層,cargo instruments 會呼叫 xcrun xctrace 命令,這相當於

bash
xcrun xctrace record --template 'Time Profile' --output . --launch -- /path/to/oxc/target/release-with-debug/oxlint

執行上述命令會產生以下輸出

Starting recording with the Time Profiler template. Launching process: oxlint.
Ctrl-C to stop the recording
Target app exited, ending recording...
Recording completed. Saving output file...
Output file saved as: Launch_oxlint_2023-09-03_4.41.45 PM_EB179B85.trace

開啟追蹤檔案 open Launch_oxlint_2023-09-03_4.41.45\ PM_EB179B85.trace

若要查看由上而下的追蹤

  1. 在頂部面板上,按一下 CPUs
  2. 在左側輸入方塊中,按一下 x 然後選取 Time Profiler
  3. 在底部面板中,按一下「呼叫樹狀結構」,開啟「反轉呼叫樹狀結構」並關閉依執行緒分隔。

對於記憶體和磁碟操作,請使用 --template 'Allocations'--template 'File Activity'

若要進行更詳細的 CPU 效能分析,例如 L1/L2 快取未命中、週期和指令計數以及分支預測資訊,您需要使用自訂的「CPU 計數器」範本

  1. 開啟 Instruments 並選取「CPU 計數器」範本。
  2. 在「CPU 計數器」設定中
    1. 開啟「高頻取樣」選項。
    2. 在「高頻取樣」選項下方,按一下加號圖示並選取事件類型。一些建議的事件類型
      • 週期 - 用於粗略了解每個函式中花費了多少 CPU 週期。
      • 指令 - 用於粗略了解每個函式中執行了多少 CPU 指令以及需要多少週期
      • L1D_CACHE_MISS_LD - 從記憶體載入資料時 L1 快取未命中的計數
  3. 啟用您感興趣的事件後,將範本儲存在「檔案 > 另存為範本...」中,並為其命名。
  4. 現在,您可以透過將範本名稱傳遞給 --template 選項,將其與 xctrace 一起使用:xcrun xctrace record --template 'My Custom CPU Counters' --output . --launch -- /path/to/oxc/target/release-with-debug/oxlint

堆積配置

試試 dhat

在 MIT 授權下發布。