跳到正文
Skillpedia
测试9.1k · #12304

parallelism

它能做什么

这项技能展示如何在多个进程中同时运行 Ginkgo 测试以节省时间。适合 Go 测试套件耗时很长的开发者。

适用场景

  • ✓当你的 Ginkgo 测试套件运行太慢的时候。
  • ✓当你需要测试同时占用多个 CPU 核心的时候。
  • ✓当并行测试共享端口、文件夹或数据库,需要分别设置时。

技能链接

https://github.com/onsi/ginkgo/blob/master/plugins/ginkgo/skills/parallelism/SKILL.md

复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。

使用方法

最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。

可直接粘贴的消息

请用这个技能帮我:https://github.com/onsi/ginkgo/blob/master/plugins/ginkgo/skills/parallelism/SKILL.md 阅读该链接中的 SKILL.md 文件,并在我的任务中按照它的说明来做。
想让你的 AI 自己找到这样的技能吗?安装免费的 Skillpedia MCP:只需一行,每次任务前它都会在 15,000+ 个技能中搜索。1 分钟完成安装 →

可以这样问 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 book don't race across processes — they each get their own book. (Within a process specs still run one at a time.)
  • The flip side: a BeforeSuite runs 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 -r the 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 --race that 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

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能