Mateusz Gruszczynski
Mateusz Gruszczynski

Reputation: 1510

Limit scalatest parallel execution thread number

I'm trying to execute some part of my tests in parallel so I've extended those tests classes with ParallelTestExecution trait, the only problem is that it runs too many tests at once. As I understand it runs up to 2 * number_of_cpu_cores so in my case 2*8 tests. Its way too much and I would like to limit it to 4 threads max. I've tried to use SBT concurentRestrictions in Test settings but it wont change anything (I thing it affects only concurrent test classes execution and do not affect number of concurrent tests in one class). Is there any way to force scalaTest to run max N tests in parallel? It would be best if I could set max number of threads per test class as some tests are less resources consuming and i could run more than 4 of them at once.

Upvotes: 5

Views: 2744

Answers (3)

Suma
Suma

Reputation: 34453

I've tried to use SBT concurentRestrictions in Test settings but it wont change anything

As SBT documentation says:

This is necessarily a global set of rules, so it must be scoped Global /.

You need to use it like this:

Global / concurrentRestrictions := Seq(
  Tags.limit(Tags.Test, 4)
)

I thing it affects only concurrent test classes execution and do not affect number of concurrent tests in one class

Unless you use ParallelTestExecution, individual tests in test classes run sequentially, only classes (suites) are run in parallel.

Upvotes: 0

Mateusz Gruszczynski
Mateusz Gruszczynski

Reputation: 1510

So after over a year I came back to that problem, as I was not able to resolve it previously. I stamped on a solution in this project: https://github.com/agido/pageobject. It was a bit more complicated then I needed so based on their code I've created simpler solution with just one trait that may be used as replacement for standard 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)
        )
      )
    ))
}

More how is it working and some examples can be found here: https://github.com/mateuszgruszczynski/scalatesttwolevelparallelism

Upvotes: 3

ka4eli
ka4eli

Reputation: 5424

Try to add this line to your sbt project settings:

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

From 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.

Upvotes: 2

Related Questions