Repository navigation
ci(test): 单元测试拆成 4 个分片,并按记录的各文件耗时均衡分配 - #2452
Merged
Merged
Conversation
Co-Authored-By: Claude Opus 5.5 <[email protected]>
deepcoldy
requested review from
47seek,
LucasIcarus,
xiaoxueSunn and
xiongz-c
as code owners
October 27, 2026 20:53
vitest 自带的 --shard 按路径哈希后等文件数切片,几个数分钟级的集成测试 文件落在哪一片就决定那片的墙钟:同一次 run 四片分别 670s/999s/395s/629s, 最慢一片的 job 仍有 19 分多钟,贴着 20 分钟时限。 改为自定义 sequencer:按 test/shard-timings.json 记录的各文件耗时(加上 每文件的启动开销)由重到轻依次放进当前最轻的一片;没记录的文件按中位数 估。计划只由 vitest 实际收集到的文件决定,耗时表过期只会让分片不够均匀, 不会漏跑或重跑文件。耗时表用 scripts/update-shard-timings.mjs 从一次 CI run 的日志刷新。 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Owner
Author
|
已合入 ea956fa(squash,标题带 #2452、正文为空)。合前复核:审过的 head 4aa059f 未变;master 期间只进了 #2217(只改 两轮评审提的 6 条非阻断项已放进 follow-up #2463:
|
Owner
Author
|
已合并,感谢这个 PR——最慢片从 20.2 分钟被取消降到 14.6 分钟,四片墙钟差收敛到 1.9 分钟。评审中记录了几条可选打磨,供后续小 PR 取舍(都不阻断):
另:本 PR 修复了 open issue #2443。 |
This was referenced Oct 28, 2026
|
🚀 Released in v3.55.0 |
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.
Closes #2443(CI 单元测试分片余量过小)。
背景
master 上
af64e8712的 CI 里,test (2/3)跑到 20.2 分钟,撞上timeout-minutes: 20被取消,汇总检查test因此变红。这个分片平时就贴着时限跑。本 PR 第一版只把 3 片改成 4 片,结果最慢一片仍有 19.3 分钟:vitest 自带的
--shard先按路径哈希、再按文件数等分,几个数分钟级的集成测试文件落在哪一片,就决定那片的墙钟。同一次 run(40581973180)四片的 vitest 墙钟是 670s / 999s / 395s / 629s,累计测试耗时 1650s / 2516s / 919s / 1534s,最重一片是最轻一片的 2.7 倍。维护者定:保持 4 片、
timeout-minutes: 20不放大(免得掩盖真的卡住的分片),改成按记录的各文件耗时做确定的贪心分片。改了什么
scripts/duration-shards.mjs(纯函数,Node/Bun 都能跑):perFileOverhead);vitest.config.ts:根级sequence.sequencer换成继承BaseSequencer的DurationShardSequencer,只覆写shard(),片内执行顺序仍是 vitest 自己的sort()。只有带--shard时才走这里,本地bun run test、e2e、bun-test不受影响。test/shard-timings.json:{ source, perFileOverhead, files: { "test/x.test.ts": 秒 } },初版取自 run 40581973180 四个分片的日志(2073 个文件,每文件启动开销 0.56s)。scripts/update-shard-timings.mjs(见下节)。.github/workflows/ci.yml:matrix[1, 2, 3, 4],job 名test (i/4)、步骤名、run-unit-shard.mjs i/4分母同步改成 4;timeout-minutes: 20、fail-fast: false不变;汇总 job 仍叫test、if: always()、只有needs.test.result == success才通过;过时注释按现状改正。scripts/run-unit-shard.mjs:只改了一句过时注释(原写「按 sha1 切连续区间」)。耗时表怎么更新
脚本读该 run 里每个
test (i/N)job 的日志,解析 vitest 的逐文件行✓ unit test/x.test.ts (N tests) 1234ms和汇总行里的 transform/setup/import,然后合并:run-unit-shard重试打印两次时取较慢的一次);键按路径排序,diff 可读。什么时候刷新:某片墙钟明显偏离其它几片、或加了新的重型集成测试之后,单独提一个 PR 更新即可,不需要每次都更新。
表过期的后果:只会稍不均,不会漏跑
计划是从 vitest 实际收集到的文件列表 出发算的,表只提供权重:
所以表过期最坏是各片墙钟不够均匀,不会让任何文件漏跑或被两片重复跑。新加的
test/duration-shards.test.ts专门钉住这一点(见验证)。必需检查不受影响
gh api repos/deepcoldy/botmux/rulesets/21274855(Require CI green on master)的必需检查是build和test,二者的 job 名都没变;分片名test (i/4)不在必需检查里。耗时(job 全程,含 bwrap 准备、
bun install、build)改前(3 分片,master 最近跑完的 run)
4 片、vitest 哈希分片(本 PR 第一版 30f7e6b,run 40581973180)
4 片、按耗时贪心(本 PR 当前 head)
head 4aa059f,run 40609706820,四片首跑全部通过:
最慢一片 14.6 分钟,离 20 分钟时限还有 5.4 分钟;最快与最慢只差 1.9 分钟(哈希分片时差 11.1 分钟)。从四片日志逐文件行核对:共 2078 个文件,去重后仍是 2078,与仓库里的单元测试文件列表逐一相同,没有漏跑或重跑。这次没有遇到 session-picker-responsive 那条偶发。
验证
createVitest+ 配置里的 sequencer)对真实配置、真实收集结果求--shard=i/4:四片 519 / 519 / 519 / 520 个文件,并集正好 2077、无重复,sequencer 类名是DurationShardSequencer(当时还没加本 PR 的新测试文件;CI 上加上它是 2078,见上表)。test/duration-shards.test.ts(12 例,vitest 与bun test下均绿):--shard=i/N的那个,且结果与分片器一致;ms/s、重试取较慢、启动开销;合并时新值覆盖、旧值保留、删掉的文件去掉。test/ci-unit-shards.test.ts(3 例)解析真实ci.yml:matrix 正好 1..N、job 名/步骤名/--shard分母都等于 N、不 fail-fast、唯一名叫test的汇总 job 依赖分片、if: always()、只在success时通过。run-unit-shard、测试卫生护栏、scripts 类型检查覆盖):5 个文件 44 例全绿;bun run build通过。isolate-run内逐个改、跑测试、还原):ms当s、开销漏掉 import、保留已删文件、丢掉未测到的旧值、旧值覆盖新值、配置不接 sequencer。index % count),这只是片号的一一置换,所有 job 用同一映射,覆盖关系不变。ci.yml8 处全部变红:matrix 少一片 / 重复一片、--shard分母仍为 3、job 名分母不一致、汇总去掉if: always()、汇总放行 skipped/cancelled、汇总不依赖分片、打开 fail-fast。🤖 Generated with Claude Code