Optimizing Maven Surefire Parallel Execution for Large-Scale Unit Tests
The Taobao User Operations Platform team reduced CI unit‑test time from fifty minutes to ten by configuring Maven Surefire to reuse forks, use a fork count of twice the CPU cores, and run methods in parallel with an appropriate thread count, achieving faster builds while keeping failure rates low.
This article shares the experience of the Taobao User Operations Platform team in improving unit‑test efficiency on their CI pipeline (aone). It describes the problem of test failures caused by concurrency and the long execution time when test cases scale up.
The testing stack consists of three core tools: JUnit for writing tests, JaCoCo for code‑coverage measurement, and the Maven Surefire Plugin for executing tests in the pipeline.
Phase 1 – Test‑case accumulation : As the number of test cases grew, random failures appeared in the pipeline due to race conditions when tests ran concurrently. The team first limited concurrency by disabling fork reuse and setting forkCount to 1.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.16</version>
<configuration>
<reuseForks>false</reuseForks>
<forkCount>1</forkCount>
</configuration>
</plugin>Phase 2 – Large test suite : With many test cases the total runtime reached ~50 minutes, severely impacting developer productivity. To speed up execution while keeping failure rates low, the team explored parallel execution options.
The Surefire plugin offers several parameters: parallel – execution mode (methods, classes, both, suites) threadCount – number of threads useUnlimitedThreads – no thread‑limit perCoreThreadCount – threads per CPU core parallelTestsTimeoutInSeconds – timeout
When parallel is enabled, either useUnlimitedThreads or threadCount must be set, otherwise Surefire throws an error.
After testing several combinations, the optimal configuration was found to be reuseForks=true and forkCount=2C (twice the number of CPU cores). This reduced the average run time to about 10 minutes with a low error probability.
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M7</version>
<configuration>
<parallel>methods</parallel>
<threadCount>10</threadCount>
</configuration>
</plugin>
</plugins>Additional tips:
Enable Maven parallel builds (e.g., mvn -T 1C clean test) only when the test suite is distributed across modules.
Adjust concurrency per project: some projects benefit from high parallelism, others prioritize stability.
In summary, understanding Surefire’s concurrency model and tailoring fork and parallel settings to the specific test workload can dramatically improve CI efficiency without sacrificing reliability.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
