将 t.Parallel() 放在测试的顶部有什么实际好处?

huangapple go评论98阅读模式
英文:

What is the practical benefit to putting t.Parallel() at the top of my tests?

问题

go testing 包定义了 Parallel() 函数

> Parallel 表示此测试将与其他并行测试一起(仅与其他并行测试一起)运行。

然而,当我搜索标准库中编写的测试时,我发现只有很少使用这个函数。

我的测试非常快,并且通常不依赖于改变共享状态,所以我一直在添加这个函数,以期望能提高速度。但是标准库中没有使用它让我有些犹豫。在测试中添加 t.Parallel() 的实际好处是什么?

英文:

The go testing package defines a Parallel() function:

> Parallel signals that this test is to be run in parallel with (and only with) other parallel tests.

However when I searched the tests written for the standard library, I found only a few uses of this function.

My tests are pretty fast, and generally don't rely on mutating shared state, so I've been adding this, figuring it would lead to a speedup. But the fact it's not used in the standard library gives me pause. What is the practical benefit to adding t.Parallel() to your tests?

答案1

得分: 6

这个讨论串(其中提出和讨论了t.Parallel)表明**t.Parallel()只适用于慢速测试;普通测试速度很快,因此并行执行的收益微乎其微。**

以下是一些引用(只有Russ的观点,但没有太多反对意见):

Russ Cox [链接]:
> 对于默认值有一些争议。
我们大部分的测试运行得非常快,因此并行化它们是不必要的。我认为这可能是正确的模型,这意味着并行化是例外而不是规则。
作为一个例外,可以通过调用t.Parallel()方法来声明一个测试可以与其他测试并行运行。

Russ Cox再次发表意见[链接]:
> 非并行测试应该很快。它们是无关紧要的。
如果两个测试很慢,那么如果你介意的话,你可以将它们并行化。
如果一个测试很慢,那么一个测试就很慢。

英文:

This thread (in which t.Parallel is conceived and discussed) indicates that t.Parallel() is intended to be used for slow tests only; average tests are so fast that any gain from parallel execution would be negligible.

Here are some quotes (only from Russ, but there wasn't much opposition):

Russ Cox [link]:
> There is some question about what the right default is.
Most of our tests run so fast that parallelizing them is
unnecessary. I think that's probably the right model,
which would suggest parallelizing is the exception,
not the rule.
As an exception, this can be accommodated having a
t.Parallel()
method that a test can call to declare that it is okay
to run in parallel with other tests.

Russ Cox again [link]:
> the non-parallel tests should be fast. they're inconsequential.
if two tests are slow, then you can t.Parallel them if it bothers you.
if one test is slow, well, one test is slow.

答案2

得分: 3

这似乎是首次在golang-dev组上提出的。

最初的请求如下:

>"我想为gotest添加一个并行运行测试的选项。
我的动机来自于运行selenium测试,其中每个测试基本上都是相互独立的,但它们需要时间。"

该线程包含了对实际好处的讨论。

这只是为了让您能够同时运行不相关的长时间运行的测试。它在标准库中并没有真正被使用,因为几乎所有的功能都需要尽可能快(一些加密例外等)。

这里还有进一步的讨论在这里,提交的代码在这里

英文:

This seems to have been brought up first on the golang-dev group.

The initial request states:

>"I'd like to add an option to run tests in parallel with gotest.
My motivation comes from running selenium tests where each test is pretty much independent from each other but they take time."

The thread contains the discussion of the practical benefits.

It's really just for allowing you to run unrelated, long-running tests at the same time. It's not really used in the standard library as almost all of the functionality needs to be as fast as possible (some crypto exceptions etc.)

There was further discussion here and the commit is here

huangapple
  • 本文由 发表于 2015年1月21日 10:31:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/28058640.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定