限制最大规模的并行执行线程数
Limit scalatest parallel execution thread number
我正在尝试并行执行测试的某些部分,因此我扩展了这些测试 classes 具有 ParallelTestExecution
特征,唯一的问题是它 运行一次测试太多了。据我了解,它 运行 最多 2 * number_of_cpu_cores
所以在我的例子中是 2*8 次测试。它太多了,我想将它限制为最多 4 个线程。我试过使用 SBT concurentRestrictions in Test
设置,但它不会改变任何东西(我认为它只影响并发测试 classes 执行并且不影响一个 class 中的并发测试数量)。有没有办法强制 scalaTest 并行进行 运行 max N 测试?最好是我可以设置每个测试的最大线程数 class,因为有些测试消耗的资源较少,我可以一次 运行 超过 4 个。
尝试将这一行添加到您的 sbt 项目设置中:
testOptions in Test += Tests.Argument(TestFrameworks.ScalaTest, "-P4")
来自http://www.scalatest.org/user_guide/using_the_runner:
The -P option may optionally be appended with a number (e.g. "-P10" --
no intervening space) to specify the number of threads to be created
in the thread pool. If no number (or 0) is specified, the number of
threads will be decided based on the number of processors available.
所以一年多后我又回到了那个问题,因为我以前无法解决它。我在这个项目中戳了一个解决方案:https://github.com/agido/pageobject。它比我需要的要复杂一些,所以根据他们的代码,我创建了一个更简单的解决方案,只有一个特征可以用来替代标准 ParallelTestExecution
.:
package org.scalatest
import java.util.concurrent.Executors
import org.scalatest.tools.ConcurrentDistributor
trait FixedThreadPoolParallelExecution extends SuiteMixin with ParallelTestExecution{ this: Suite =>
val threadPoolSize: Int
abstract override def run(testName: Option[String], args: Args): Status =
super.run(testName, args.copy(
distributor = Some(
new ConcurrentDistributor(
args,
java.util.concurrent.Executors.newFixedThreadPool(threadPoolSize, Executors.defaultThreadFactory)
)
)
))
}
有关其工作原理的更多信息和一些示例可在此处找到:https://github.com/mateuszgruszczynski/scalatesttwolevelparallelism
我正在尝试并行执行测试的某些部分,因此我扩展了这些测试 classes 具有 ParallelTestExecution
特征,唯一的问题是它 运行一次测试太多了。据我了解,它 运行 最多 2 * number_of_cpu_cores
所以在我的例子中是 2*8 次测试。它太多了,我想将它限制为最多 4 个线程。我试过使用 SBT concurentRestrictions in Test
设置,但它不会改变任何东西(我认为它只影响并发测试 classes 执行并且不影响一个 class 中的并发测试数量)。有没有办法强制 scalaTest 并行进行 运行 max N 测试?最好是我可以设置每个测试的最大线程数 class,因为有些测试消耗的资源较少,我可以一次 运行 超过 4 个。
尝试将这一行添加到您的 sbt 项目设置中:
testOptions in Test += Tests.Argument(TestFrameworks.ScalaTest, "-P4")
来自http://www.scalatest.org/user_guide/using_the_runner:
The -P option may optionally be appended with a number (e.g. "-P10" -- no intervening space) to specify the number of threads to be created in the thread pool. If no number (or 0) is specified, the number of threads will be decided based on the number of processors available.
所以一年多后我又回到了那个问题,因为我以前无法解决它。我在这个项目中戳了一个解决方案:https://github.com/agido/pageobject。它比我需要的要复杂一些,所以根据他们的代码,我创建了一个更简单的解决方案,只有一个特征可以用来替代标准 ParallelTestExecution
.:
package org.scalatest
import java.util.concurrent.Executors
import org.scalatest.tools.ConcurrentDistributor
trait FixedThreadPoolParallelExecution extends SuiteMixin with ParallelTestExecution{ this: Suite =>
val threadPoolSize: Int
abstract override def run(testName: Option[String], args: Args): Status =
super.run(testName, args.copy(
distributor = Some(
new ConcurrentDistributor(
args,
java.util.concurrent.Executors.newFixedThreadPool(threadPoolSize, Executors.defaultThreadFactory)
)
)
))
}
有关其工作原理的更多信息和一些示例可在此处找到:https://github.com/mateuszgruszczynski/scalatesttwolevelparallelism