Skip to content
Vitest 2
Main Navigation 指南API配置瀏覽器模式高級
3.2.0
2.1.9
1.6.1
0.34.6

繁體中文

English
简体中文
Español
Français
Русский
Português – Brasil
Deutsch
日本語
한국어
Italiano
Polski
Türkçe
čeština
magyar

繁體中文

English
简体中文
Español
Français
Русский
Português – Brasil
Deutsch
日本語
한국어
Italiano
Polski
Türkçe
čeština
magyar

外觀

Sidebar Navigation

為什麼使用 Vitest

開始使用

功能特性

工作區

命令列界面

測試過濾器

報告器

覆蓋率

快照

模擬(Mocking)

測試類型

Vitest UI

原始碼測試

測試上下文

測試環境

擴展匹配器

IDE 整合支援

偵錯

與其他測試執行器的比較

遷移指南

常見錯誤

Profiling Test Performance

提升效能

本頁導覽

測試效能分析 ​

當您執行 Vitest 時,它會報告測試的多個時間指標:

bash
RUN  v2.1.1 /x/vitest/examples/profiling

✓ test/prime-number.test.ts (1) 4517ms
  ✓ generate prime number 4517ms

Test Files  1 passed (1)
     Tests  1 passed (1)
  Start at  09:32:53
  Duration  4.80s (transform 44ms, setup 0ms, collect 35ms, tests 4.52s, environment 0ms, prepare 81ms)
  # Time metrics ^^
  • Transform (轉換):檔案轉換所花費的時間。請參閱檔案轉換。
  • Setup (設定):執行 setupFiles 所花費的時間。
  • Collect (收集):收集測試檔案中所有測試案例所花費的時間。這包括匯入所有檔案依賴項所需的時間。
  • Tests (測試):實際執行測試案例所花費的時間。
  • Environment (環境):設定測試環境 environment 所花費的時間,例如 JSDOM。
  • Prepare (準備):Vitest 準備測試執行器所需的時間。

測試執行器 ​

如果您的測試執行時間過長,您可以產生測試執行器的效能分析報告。請參閱 NodeJS 文件以了解下列選項:

  • --cpu-prof
  • --heap-prof
  • --prof

WARNING

由於 node:worker_threads 的限制,--prof 選項不適用於 pool: 'threads'。

若要將這些選項傳遞給 Vitest 的測試執行器,請在您的 Vitest 設定中定義 poolOptions.<pool>.execArgv:

ts
import { defineConfig } from 'vitest/config';

export default defineConfig({
  test: {
    pool: 'forks',
    poolOptions: {
      forks: {
        execArgv: [
          '--cpu-prof',
          '--cpu-prof-dir=test-runner-profile',
          '--heap-prof',
          '--heap-prof-dir=test-runner-profile',
        ],

        // 若要產生單一效能分析檔
        singleFork: true,
      },
    },
  },
});
ts
import { defineConfig } from 'vitest/config';

export default defineConfig({
  test: {
    pool: 'threads',
    poolOptions: {
      threads: {
        execArgv: [
          '--cpu-prof',
          '--cpu-prof-dir=test-runner-profile',
          '--heap-prof',
          '--heap-prof-dir=test-runner-profile',
        ],

        // 若要產生單一效能分析檔
        singleThread: true,
      },
    },
  },
});

測試執行完成後,應該會產生 test-runner-profile/*.cpuprofile 和 test-runner-profile/*.heapprofile 檔案。請參閱檢查效能分析記錄以了解如何分析這些檔案的說明。

請參閱效能分析 | 範例。

主執行緒 ​

分析主執行緒對於偵錯 Vitest 的 Vite 使用方式和 globalSetup 檔案很有用。 您的 Vite 外掛程式也會在此處執行。

TIP

請參閱效能 | Vite 以了解更多 Vite 專屬的效能分析提示。

我們建議使用 vite-plugin-inspect 來分析 Vite 外掛程式的效能。

為此,您需要將參數傳遞給執行 Vitest 的 Node.js 程序。

bash
$ node --cpu-prof --cpu-prof-dir=main-profile ./node_modules/vitest/vitest.mjs --run
#      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^                                  ^^^^^
#               NodeJS 參數                                           Vitest 參數

測試執行後,應該會產生 main-profile/*.cpuprofile 檔案。請參閱檢查效能分析記錄以了解如何分析這些檔案的說明。

檔案轉換 ​

如果您的測試轉換和收集時間過長,您可以使用 DEBUG=vite-node:* 環境變數來查看哪些檔案正在被 vite-node 轉換和執行。

bash
$ DEBUG=vite-node:* vitest --run

 RUN  v2.1.1 /x/vitest/examples/profiling

  vite-node:server:request /x/vitest/examples/profiling/global-setup.ts +0ms
  vite-node:client:execute /x/vitest/examples/profiling/global-setup.ts +0ms
  vite-node:server:request /x/vitest/examples/profiling/test/prime-number.test.ts +45ms
  vite-node:client:execute /x/vitest/examples/profiling/test/prime-number.test.ts +26ms
  vite-node:server:request /src/prime-number.ts +9ms
  vite-node:client:execute /x/vitest/examples/profiling/src/prime-number.ts +9ms
  vite-node:server:request /src/unnecessary-file.ts +6ms
  vite-node:client:execute /x/vitest/examples/profiling/src/unnecessary-file.ts +4ms
...

這種效能分析策略是識別由 barrel files (桶狀檔案) 引起的不必要轉換的有效方法。 如果這些日誌包含在測試執行時不應載入的檔案,則您的桶狀檔案可能不必要地匯入了其他檔案。

您也可以使用 Vitest UI 來排查由桶狀檔案引起的效能緩慢問題。 以下範例顯示了如何在不使用桶狀檔案的情況下匯入檔案,從而將轉換的檔案數量減少約 85%。

├── src
│   └── utils
│       ├── currency.ts
│       ├── formatters.ts  <-- 待測試檔案
│       ├── index.ts
│       ├── location.ts
│       ├── math.ts
│       ├── time.ts
│       └── users.ts
├── test
│   └── formatters.test.ts
└── vitest.config.ts
ts
import { expect, test } from 'vitest';
import { formatter } from '../src/utils'; 
import { formatter } from '../src/utils/formatters'; 

test('formatter works', () => {
  expect(formatter).not.toThrow();
});
Vitest UI 演示桶狀檔案問題

若要查看檔案如何轉換,您可以使用 VITE_NODE_DEBUG_DUMP 環境變數將轉換後的檔案寫入檔案系統中:

bash
$ VITE_NODE_DEBUG_DUMP=true vitest --run

[vite-node] [debug] dump modules to /x/examples/profiling/.vite-node/dump

 RUN  v2.1.1 /x/vitest/examples/profiling
...

$ ls .vite-node/dump/
_x_examples_profiling_global-setup_ts-1292904907.js
_x_examples_profiling_test_prime-number_test_ts-1413378098.js
_src_prime-number_ts-525172412.js

檢查效能分析記錄 ​

您可以使用各種工具檢查 *.cpuprofile 和 *.heapprofile 的內容。請參閱以下清單以了解範例。

  • Speedscope
  • 在 Visual Studio Code 中分析 JavaScript 效能
  • 使用效能面板分析 Node.js 效能 | developer.chrome.com
  • 記憶體面板概覽 | developer.chrome.com
Pager
上一頁常見錯誤
下一頁提升效能

以 MIT 授權條款 發布。

版權所有 (c) 2021-Present Vitest Team

https://v2.vitest.dev/guide/profiling-test-performance

以 MIT 授權條款 發布。

版權所有 (c) 2021-Present Vitest Team