パフォーマンスの向上
テストの分離
デフォルトでは、Vitest は pool に基づく分離された環境で各テストファイルを実行します。
threads
プールは、各テストファイルを個別のWorker
で実行します。forks
プールは、各テストファイルを個別の forked child process で実行します。vmThreads
プールは、各テストファイルを個別の VM context で実行しますが、並列処理にはワーカーを使用します。
この分離はテスト時間を大幅に増加させる可能性がありますが、副作用に依存せず、状態を適切にクリアするプロジェクト(通常、node
環境のプロジェクトに当てはまる)では、この挙動が望ましくない場合があります。この場合、分離を無効化するとテスト速度が向上します。そのためには、CLI に --no-isolate
フラグを指定するか、設定で test.isolate
プロパティを false
に設定します。
vitest --no-isolate
import { defineConfig } from 'vitest/config';
export default defineConfig({
test: {
isolate: false,
// 特定のプールのみ分離を無効化することも可能です
poolOptions: {
forks: {
isolate: false,
},
},
},
});
TIP
vmThreads
プールを使用している場合、分離を無効にすることはできません。テストパフォーマンスを向上させるには、代わりに threads
プールを使用してください。
一部のプロジェクトでは、起動時間を短縮するために並列処理を無効にすることが望ましいこともあります。そのためには、CLI に --no-file-parallelism
フラグを指定するか、設定で test.fileParallelism
プロパティを false
に設定します。
vitest --no-file-parallelism
import { defineConfig } from 'vitest/config';
export default defineConfig({
test: {
fileParallelism: false,
},
});
プール
デフォルトでは、Vitest は pool: 'forks'
を使用してテストを実行します。'forks'
プールは互換性の問題(ハングするプロセスやセグメンテーション違反)に対応していますが、大規模なプロジェクトでは pool: 'threads'
よりもわずかに遅くなる可能性があります。
設定で pool
オプションを切り替えることで、テスト実行時間を短縮できる可能性があります。
vitest --pool=threads
import { defineConfig } from 'vitest/config';
export default defineConfig({
test: {
pool: 'threads',
},
});
シャーディング
テストシャーディングとは、テストスイートを複数のグループ(シャード)に分割するプロセスです。これは、大規模なテストスイートを所有しており、そのサブセットを同時に実行できる複数のマシンがある場合に役立ちます。
Vitest のテストを複数の異なる実行に分割するには、--shard
オプションを --reporter=blob
オプションと組み合わせて使用します。
vitest run --reporter=blob --shard=1/3 # 1st machine
vitest run --reporter=blob --shard=2/3 # 2nd machine
vitest run --reporter=blob --shard=3/3 # 3rd machine
Vitest はテストケースではなく、_テストファイル_単位でシャード分割を行います。1000個のテストファイルがある場合、
--shard=1/4
オプションは、各ファイルに含まれるテストケースの数に関わらず、250個のテストファイルを実行します。
各マシンから .vitest-reports
ディレクトリに保存された結果を収集し、--merge-reports
オプションで結合します。
vitest run --merge-reports
Github action example
この設定は、https://github.com/vitest-tests/test-sharding でも使用されています。
# https://playwright.dev/docs/test-sharding から着想を得ました
name: Tests
on:
push:
branches:
- main
jobs:
tests:
runs-on: ubuntu-latest
strategy:
matrix:
shardIndex: [1, 2, 3, 4]
shardTotal: [4]
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
- name: Install pnpm
uses: pnpm/action-setup@a7487c7e89a18df4991f7f222e4898a00d66ddda # v4.1.0
- name: Install dependencies
run: pnpm i
- name: Run tests
run: pnpm run test --reporter=blob --shard=${{ matrix.shardIndex }}/${{ matrix.shardTotal }}
- name: Upload blob report to GitHub Actions Artifacts
if: ${{ !cancelled() }}
uses: actions/upload-artifact@v4
with:
name: blob-report-${{ matrix.shardIndex }}
path: .vitest-reports/*
include-hidden-files: true
retention-days: 1
merge-reports:
if: ${{ !cancelled() }}
needs: [tests]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
- name: Install pnpm
uses: pnpm/action-setup@a7487c7e89a18df4991f7f222e4898a00d66ddda # v4.1.0
- name: Install dependencies
run: pnpm i
- name: Download blob reports from GitHub Actions Artifacts
uses: actions/download-artifact@v4
with:
path: .vitest-reports
pattern: blob-report-*
merge-multiple: true
- name: Merge reports
run: npx vitest --merge-reports
TIP
テストシャーディングは、CPUコア数が多いマシンでも有効です。
Vitest は、メインスレッドでは単一の Vite サーバーのみが実行されます。残りのスレッドはテストファイルの実行に使用されます。 CPU コア数の多いマシンでは、メインスレッドがボトルネックとなる場合があります。これは、テストスレッドからのすべてのリクエストを処理しきれないためです。たとえば、32 CPU マシンでは、メインスレッドは 31 のテストスレッドからの負荷を処理する役割を担います。
メインスレッドの Vite サーバーからの負荷を軽減するために、テストシャーディングを使用できます。負荷は複数の Vite サーバーに分散させることができます。
# 32 CPU でテストを 4 つのシャードに分割する例。
# 各プロセスに 1 つのメインスレッドが必要なため、テストランナーには 7 つのスレッドがあります (1+7)*4 = 32
# プールに応じて VITEST_MAX_THREADS または VITEST_MAX_FORKS を使用します。
VITEST_MAX_THREADS=7 vitest run --reporter=blob --shard=1/4 & \
VITEST_MAX_THREADS=7 vitest run --reporter=blob --shard=2/4 & \
VITEST_MAX_THREADS=7 vitest run --reporter=blob --shard=3/4 & \
VITEST_MAX_THREADS=7 vitest run --reporter=blob --shard=4/4 & \
wait # https://man7.org/linux/man-pages/man2/waitpid.2.html
vitest run --merge-reports