Repository navigation
XCL-171 キャッシュのない初回の jest が遅く、手元でメモリ不足で止まることがある - #266
Merged
Merged
Conversation
キャッシュがないと、ワーカーごとに同じモジュールを変換し直すため、ワーカーが多いほど遅くなり、メモリも増える。
Contributor
Code reviewNo issues found. Checked for bugs and AGENTS.md / CLAUDE.md compliance. 範囲: 全体 |
Contributor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
概要
キャッシュのない初回の jest が遅く、手元でメモリ不足 (exit 137) で止まることがあった。測ると、原因はワーカーの数だった。キャッシュがないと、どのワーカーも同じモジュールを変換し直すので、ワーカーを増やすほど遅くなり、メモリも増える。jest のワーカーを 3 に絞り、ワーカーのメモリに上限を付ける。
変更内容
package.jsonのjestにmaxWorkers: 3を足す (CLI の--maxWorkersで上書きできる)workerIdleMemoryLimit: "1GB"を足す。テストのファイルを実行するたびにワーカーのヒープが増えていく (後のファイルほど 500〜630 MB) ので、1 GB を超えたワーカーは次のファイルの前に作り直す。今の件数では発動しない、保険の設定測った結果
8 コア・メモリ 9 GB (WSL2) の手元で測った。キャッシュなしは
pnpm test --clearCacheの後の初回。最大メモリは、jest の親プロセスとワーカーの RSS の合計を 0.5 秒ごとに見た最大値。ヒープは--logHeapUsageの値。workerIdleMemoryLimitだけprofile-screens.test.tsx)、後 21.5 秒時間がかかっている所
babel の変換に計測用の処理を挟んで、キャッシュなしの初回を測った (計測用のコードはコミットしていない)。
react-native-reanimated101 秒、expo-router39 秒、react-native32 秒、expo8 秒。@shopify/flash-listは 1.8 秒で、主な原因ではなかったreact-native-reanimatedは、expo-router/testing-libraryが、モックを当てる前に本物をrequireして有るかを確かめるため、TypeScript のソース (272 ファイル) がまるごと変換される。空のモックに差し替えて試したが、39.8 秒が 38.3 秒になる程度だったので、入れていない受け入れ条件
pnpm test --clearCacheの後) で、どこに時間とメモリがかかっているかを測り、数字を PR に書く (--logHeapUsageなど)maxWorkersを絞る、変換の対象を最小にする、workerIdleMemoryLimit) を入れ、キャッシュのない初回が、メモリ不足で落ちずに通る動作確認
pnpm test --clearCacheの後にpnpm test: 47 ファイル・358 件が通る (2 回)pnpm test: 358 件が通るpnpm lint、pnpm typecheck、pnpm format:checkリスクの確認
.env*、キー、トークン) を含めていない人が確認すること
pnpm test --clearCache && pnpm testが通るか補足
/tmp/jest_rsで、worktree の間で共有されるが、キャッシュの鍵に worktree の絶対パス (rootDir や babel の設定) が入るので、新しい worktree では毎回キャッシュなしになる