Arindam
Arindam

Reputation: 21

Running junits test in parallel to maximize the performance through jenkins/ intellij

I have a project which contains around 50 junit tests. Each test takes about 5-6 minutes to run since it tries to assert on some data which takes about that time to be available in redshift(the test look up the data in redshift). Now I am trying to run all them in parallel and expecting all test to be run in about 15-20 minutes max. I tried to use test.runWith(){maxParallelForks} option but the tests takes more than an hour. These tests are all independent. Is there an efficient way to parallelize them?

N.B This is a neighbor test not just junits. So I don't have the option to mock the results as results to be derived from the actual interactions between our neighbor systems.

Thanks

Here is what I am using:

tasks.withType(Test) {
    maxParallelForks = 50
}

Our cut off is to have them running in 20-25 minutes max but it's taking more then an hour.

Upvotes: 2

Views: 2141

Answers (1)

Ph03n1x
Ph03n1x

Reputation: 844

  1. Create a file named junit-platform.properties in your test/resources folder with the following data:
junit.jupiter.execution.parallel.enabled=false
junit.jupiter.execution.parallel.config.strategy=fixed
junit.jupiter.execution.parallel.config.fixed.parallelism=4

I have a 4 cores (without hyper threading). You can also try the dynamic option.

  1. Annotate your class with @Execution(ExecutionMode.CONCURRENT)

Here is a great article on the topic. Also one here.

I have also experimented with parallel test execution and hat the following conclusions:

  • If the tests are short (not as in your case though) it's not worth it. When dealing with short tests the effort to setup the whole multi-threaded testing environment and the context switches later on didn't result in any performance gain. (I have had ca 150+ Unit tests)
  • When dealing with Spring the tests need to be configured correctly. As you start containers parallel, they must not try to use the same port etc. Also you have to be aware of context-caching. (Even if test B didn't require the component XYZ, I asked spring to initialize it, and then it realized that test A already had the same configuration, hence the context was reused with resulted in a way better performance gain than having two different containers starting on multiple threads.)
  • If your tests contain code (as in your case), where you "await" an event in order to complete the test, using multiple threads is the way to go.
  • Finally: "Sounds good, doesn't work." - On my local machine the parallel execution resulted in some degree of performance gain (IntelliJ, maven), but in our CI server we have a single virtual core machine, and there asking for fixed 4 threads resulted in a massive performance drop.

Upvotes: 4

Related Questions