【问题标题】:Limit scalatest parallel execution thread number限制scalatest并行执行线程数
【发布时间】:2017-12-23 11:20:57
【问题描述】:

我正在尝试并行执行我的部分测试,所以我用ParallelTestExecution trait 扩展了这些测试类,唯一的问题是它一次运行了太多测试。据我了解,它运行到 2 * number_of_cpu_cores 所以在我的情况下是 2*8 测试。它太多了,我想将其限制为最多 4 个线程。我尝试使用 SBT concurentRestrictions in Test 设置,但它不会改变任何东西(我认为它只影响并发测试类的执行,不会影响一个类中的并发测试数量)。有什么方法可以强制 scalaTest 并行运行最大 N 个测试?如果我可以设置每个测试类的最大线程数,那将是最好的,因为某些测试消耗的资源较少,并且我可以一次运行超过 4 个。

【问题讨论】:

    标签: scala sbt scalatest


    【解决方案1】:

    所以一年多后我又回到了这个问题,因为我以前无法解决它。我在这个项目中标记了一个解决方案: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

    【讨论】:

      【解决方案2】:

      尝试将此行添加到您的 sbt 项目设置中:

      testOptions in Test += Tests.Argument(TestFrameworks.ScalaTest, "-P4")
      

      来自http://www.scalatest.org/user_guide/using_the_runner:

      -P 选项可以选择附加一个数字(例如“-P10”-- 没有中间空间)来指定要创建的线程数 在线程池中。如果未指定数字(或 0),则 线程将根据可用处理器的数量来决定。

      【讨论】:

      • 不幸的是,从 SBT 运行测试时不支持此参数,它建议使用 SBT 并行配置,但显然它执行的不是 -P。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-16
      相关资源
      最近更新 更多