parallelism
它能做什么
这项技能展示如何在多个进程中同时运行 Ginkgo 测试以节省时间。适合 Go 测试套件耗时很长的开发者。
适用场景
- ✓当你的 Ginkgo 测试套件运行太慢的时候。
- ✓当你需要测试同时占用多个 CPU 核心的时候。
- ✓当并行测试共享端口、文件夹或数据库,需要分别设置时。
技能链接
https://github.com/onsi/ginkgo/blob/master/plugins/ginkgo/skills/parallelism/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“我的 Go 测试套件运行太慢了,你能用 Ginkgo 让它并行运行吗?”
每周 5 个新技能,1 分钟读完。
工作原理
它会把测试程序作为独立进程运行多份,每份各自承担一部分测试,然后把结果合并成一份报告。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
Running Ginkgo in parallel
Parallelism is where Ginkgo's "every spec is independent" assumption (ginkgo:overview) earns its keep. If your specs are independent, going parallel is one flag. If they aren't, parallelism is how you find out. Docs: https://onsi.github.io/ginkgo/#spec-parallelization, https://onsi.github.io/ginkgo/#patterns-for-parallel-integration-specs.
The flag
ginkgo -p # auto-detect optimal process count from CPU cores
ginkgo --procs=4 # pin the count explicitly
ginkgo watch -p # re-run in parallel on every file change
-p/--procs require the ginkgo CLI — go test cannot parallelize a Ginkgo suite (it has no server to coordinate processes). Most other features work under go test; parallelism is the headline exception. → ginkgo:running.
The mental model: separate processes, not goroutines
This is the one thing to internalize. Ginkgo compiles the suite once (go test -c), then launches N copies of the test binary as separate OS processes. Each process runs the full tree-construction phase independently (so all N build the identical spec list), then pulls specs to run from the CLI, which acts as a server and merges everything into one coherent output stream.
- Separate processes means separate memory. Each process has its own copy of every package-level var and closure variable. There is no shared memory, so shared-closure specs like
var bookdon't race across processes — they each get their ownbook. (Within a process specs still run one at a time.) - The flip side: a
BeforeSuiteruns on every process, so anything it creates is created N times. - You usually don't care which process runs a spec — except for the integration patterns below, which deliberately shard shared external resources by process index.
What the N processes cost
Going parallel is cheap, but not free — and the cost is rarely where people look for it.
- Compilation is once per suite, not once per process. Ginkgo builds the test binary a single time and runs N copies of it; under
-rthe CLI also overlaps compiling the next suite with running the current one. It is usually not the bottleneck it's assumed to be. - Teardown is once per process — but they overlap, so a suite pays roughly one process teardown, not N.
- Under
--racethat one teardown is ~1s. ThreadSanitizer sleeps for a second at process exit (atexit_sleep_ms, its default) so races reported by goroutines still running at exit aren't lost. Every process pays it however trivial the suite: a 2-spec package pays exactly what a 2000-spec package pays.
That flatness is the tell. The sleep lands in the gap between suites under -r, where it looks exactly like compilation — but a build cost can't be flat across package size, and a per-process cost must be. If you're auditing a slow -p --race run, precompile with ginkgo build -r so compilation is excluded by construction rather than by inference. To remove the sleep:
GORACE=atexit_sleep_ms=0 ginkgo -r -p --race
This slightly weakens detection at the very end of a suite — a reasonable trade when your races are found by your specs rather than at teardown, but a deliberate one; write the reason down where you set it. → ginkgo:ci for the whole CI flag set.
Suite setup when N copies is wrong: Synchronized*Suite
BeforeSuite runs on every process → N independent resources. Sometimes that's ideal (max isolation). When a resource is expensive or must be shared, use SynchronizedBeforeSuite: set it up once on process #1, then distribute connection info to all processes.
var dbClient *db.Client
var _ = SynchronizedBeforeSuite(func() []byte {
// runs ONLY on process #1; all others wait
dbRunner := db.NewRunner()
Expect(dbRunner.Start()).To(Succeed())
DeferCleanup(dbRunner.Stop) // runs like SynchronizedAfterSuite: after ALL procs finish
return []byte(dbRunner.Address()) // []byte passed to every process
}, func(address []byte) {
// runs on ALL processes with the data from process #1
dbClient = db.NewClient()
Expect(dbClient.Connect(string(address))).To(Succeed())
dbClient.SetNamespace(fmt.Sprintf("namespace-%d", GinkgoParallelProcess()))
DeferOnFailure(dbClient.DumpNamespace) // suite-wide failure diagnostics belong here (every process), not in the proc-#1 func
})
BeforeSuite |
SynchronizedBeforeSuite |
|
|---|---|---|
| process #1 |
常见问题
并行运行总能让测试更快吗?
通常可以,但启动每个副本需要时间,对于较小的测试套件,这部分时间可能会抵消收益。
为什么不能用 go test 进行并行运行?
标准的 go test 命令无法协调各个独立进程,因此需要使用 ginkgo 命令。
并行运行测试有什么风险?
共享端口或数据库的测试可能发生冲突,因此每个进程都需要自己的设置。
详情
作者的原始描述
Run Ginkgo suites in parallel — ginkgo -p / --procs, the separate-process (not goroutine) model, SynchronizedBeforeSuite/SynchronizedAfterSuite vs BeforeSuite, GinkgoParallelProcess() for sharding ports/tmpdirs/databases, building a binary once via gexec, piping child-process output to GinkgoWriter, and what N processes actually cost (compilation vs teardown, and the fixed ~1s --race adds per suite — GORACE=atexit_sleep_ms=0). Use when parallelizing a suite, speeding up integration tests, auditing why a parallel run is slow, fixing parallel-only flakes/races, sharding external resources, or ch
repo: onsi/ginkgo
热度
- GitHub 星标
- 9.1k
- 排名
- #12304
- 许可证
- MIT
- 更新时间
- 2026/10/9
评论
- …
同一仓库的更多技能
ci
9.1k帮你的智能体为持续集成设置 Ginkgo 测试,使用推荐的参数,能捕捉遗留的 Focus 或 Pending 标记以及不稳定的测试。
debugging-failures
9.1k帮你的智能体通过阅读摘要行,并从 JSON 报告中提取确切的失败测试和消息,找出 Ginkgo 测试失败的原因。
decorators
9.1k帮你的智能体正确使用 Ginkgo 测试装饰器,例如 Serial、Ordered、Label、Focus 和超时设置,并解释每一个的作用。
filtering
9.1k帮你的智能体只运行部分 Ginkgo 测试,按正确的优先顺序使用 pending、skip、focus、标签、版本约束和文件过滤器。