Skip to content

Commit

Permalink
perf($Gradle): execute unit tests in parallel mode; supports JaCoCo
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnymillergh committed Aug 12, 2022
1 parent 7656fce commit a34b7ee
Showing 1 changed file with 48 additions and 7 deletions.
55 changes: 48 additions & 7 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import enforcer.rules.RequireGradleVersion
import enforcer.rules.RequireJavaVersion
import enforcer.rules.RequireJavaVendor
import enforcer.rules.RequireJavaVersion
import io.spring.gradle.dependencymanagement.dsl.DependencyManagementExtension
import org.gradle.api.JavaVersion.VERSION_17
import org.gradle.api.tasks.testing.logging.TestExceptionFormat.FULL
import org.gradle.api.tasks.testing.logging.TestLogEvent.*
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
import org.springframework.boot.gradle.tasks.bundling.BootJar

plugins {
java
idea
`java-library`
jacoco
`maven-publish`
kotlin("jvm")
kotlin("kapt")
Expand Down Expand Up @@ -66,6 +69,7 @@ subprojects {
apply {
plugin("java")
plugin("java-library")
plugin("jacoco")
plugin("kotlin")
plugin("kotlin-kapt")
plugin("org.jetbrains.kotlin.plugin.spring")
Expand All @@ -90,18 +94,56 @@ subprojects {
options.isFork = true
}

tasks.withType<Test> {
useJUnitPlatform()
}

// https://docs.gradle.org/current/userguide/performance.html#parallel_test_execution
tasks.withType<Test>().configureEach {
// The normal approach is to use some number less than or equal to the number of CPU cores you have, such as this algorithm:
// The normal approach is to use some number less than or equal to the number of CPU cores you have,
// such as this algorithm:
maxParallelForks = (Runtime.getRuntime().availableProcessors() / 2).takeIf { it > 0 } ?: 1
// To fork a new test VM after a certain number of tests have run
setForkEvery(100)
}

// https://docs.gradle.org/current/dsl/org.gradle.api.tasks.testing.Test.html
tasks.withType<Test> {
// Configuration parameters to execute top-level classes in parallel but methods in the same thread
// https://www.jvt.me/posts/2021/03/11/gradle-speed-parallel/
systemProperties["junit.jupiter.execution.parallel.enabled"] = "true"
systemProperties["junit.jupiter.execution.parallel.mode.default"] = "concurrent"
systemProperties["junit.jupiter.execution.parallel.mode.classes.default"] = "concurrent"
// Discover and execute JUnit Platform-based tests
useJUnitPlatform()
failFast = true
// https://technology.lastminute.com/junit5-kotlin-and-gradle-dsl/
testLogging {
// set options for log level LIFECYCLE
events = mutableSetOf(FAILED, PASSED, SKIPPED, STANDARD_OUT)
exceptionFormat = FULL
showStandardStreams = true
showExceptions = true
showCauses = true
showStackTraces = true
// set options for log level DEBUG and INFO
debug {
events = mutableSetOf(FAILED, PASSED, SKIPPED, STANDARD_OUT)
exceptionFormat = FULL
}
info {
events = debug.events
exceptionFormat = debug.exceptionFormat
}
}
// report is always generated after tests run
finalizedBy(tasks.jacocoTestReport)
}

tasks.jacocoTestReport {
// tests are required to run before generating the report
dependsOn(tasks.test)
reports {
csv.required.set(true)
}
}

// Disable for take of building Spring Boot executable jar for most of the subprojects,
// Only bootstrap subproject needs it to be `true`.
tasks.withType<BootJar> {
Expand Down Expand Up @@ -148,7 +190,6 @@ subprojects {
// Testing
testImplementation("org.springframework.boot:spring-boot-starter-test")
testImplementation("org.mockito.kotlin:mockito-kotlin:$mockitoKotlinVersion")
testImplementation("org.jacoco:org.jacoco.agent:0.8.8")
}

configurations {
Expand Down

0 comments on commit a34b7ee

Please sign in to comment.