Skip to content

Refactored buildSrc Gradle-based tests around GradleFixture and reduce resource use. #11370

Open
AlexeyKuznetsov-DD wants to merge 6 commits into
masterfrom
alexeyk/refactor-gradle-fixture
Open

Refactored buildSrc Gradle-based tests around GradleFixture and reduce resource use. #11370
AlexeyKuznetsov-DD wants to merge 6 commits into
masterfrom
alexeyk/refactor-gradle-fixture

Conversation

@AlexeyKuznetsov-DD
Copy link
Copy Markdown
Contributor

@AlexeyKuznetsov-DD AlexeyKuznetsov-DD commented May 14, 2026

What Does This Do

Shares the TestKit directory used by GradleFixture so the Gradle daemons spawned by buildSrc plugin tests can be reused across test methods. Several of these tests run real Gradle builds, including Kotlin Gradle DSL script compilation, and paying for a fresh TestKit daemon/cache on every test was expensive in check_build_src.

The fixture keeps the shared TestKit directory outside each JUnit @TempDir so temp-dir cleanup does not race daemon file locks, and it stops the TestKit daemons from a JVM shutdown hook. Because shared daemons can also share plugin classloader state, MuzzleMavenRepoUtils no longer caches the MAVEN_REPOSITORY_PROXY-derived repository list in a daemon-static lazy value; it now builds the default muzzle repository list on demand.

The duplicated GradleRunner setup in individual tests has also been consolidated into GradleFixture. Test classes now extend the fixture directly and use focused helpers such as writeFile, writeRootProject, writeSettings, writeGradleProperties, writeJavaSource, addSubproject, dir, file, and buildFile that read more like a DSL than boilerplate.

Motivation

  1. Reduce check_build_src overhead. Reusing the TestKit daemon/cache amortizes Gradle and Kotlin DSL setup across the buildSrc plugin tests.

  2. Keep daemon reuse correct. Shared daemons make daemon-static state visible across fixture builds, so environment-derived muzzle repository configuration must be recomputed instead of cached.

  3. One way to write a fixture test. Centralizing GradleRunner setup and project-file helpers makes the tests shorter and keeps build fixture behavior uniform.

Additional Notes

Stats from GitLab

Job time reduced from ~10mins to ~5mins!

Memory reduced:

**Before:**                                       | **After:**
--------------------------------------------------------------------------------------------------------
RAM memory                          : 194720488   | RAM memory                          : 389949356  !!
cgroup v2 memory.peak               : 19437047808 | cgroup v2 memory.peak               : 7072915456 !!
cgroup v2 memory.max                : 21474836480 | cgroup v2 memory.max                : 21474836480
cgroup v2 memory.high               : max         | cgroup v2 memory.high               : max
cgroup v2 memory.current            : 2583728128  | cgroup v2 memory.current            : 1718136832 !!
cgroup v2 memory.pressure           :             | cgroup v2 memory.pressure           :
 some avg10=0.00 avg60=0.00 avg300=0.00 total=733 |  some avg10=0.00 avg60=0.00 avg300=0.00 total=0  !!
 full avg10=0.00 avg60=0.00 avg300=0.00 total=732 |  full avg10=0.00 avg60=0.00 avg300=0.00 total=0  !!

@AlexeyKuznetsov-DD AlexeyKuznetsov-DD self-assigned this May 14, 2026
@AlexeyKuznetsov-DD AlexeyKuznetsov-DD added tag: no release notes Changes to exclude from release notes type: refactoring comp: tooling Build & Tooling labels May 14, 2026
@AlexeyKuznetsov-DD AlexeyKuznetsov-DD marked this pull request as ready for review May 14, 2026 02:12
@AlexeyKuznetsov-DD AlexeyKuznetsov-DD requested review from a team as code owners May 14, 2026 02:12
Copy link
Copy Markdown
Contributor

@bric3 bric3 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change is nice, however I'd rather not switch back to groovy dsl.

Since it looks ai generated for some parts, I'd rather convert the remaining fixture to kotlin dsl.

Comment thread buildSrc/src/test/kotlin/datadog/gradle/plugin/GradleFixture.kt Outdated
Comment thread buildSrc/src/test/kotlin/datadog/gradle/plugin/GradleFixture.kt Outdated
Comment thread buildSrc/src/test/kotlin/datadog/gradle/plugin/GradleFixture.kt Outdated
Comment on lines -137 to +133
file(projectDir, "build.gradle.kts").writeText(
writeRootProject(
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This switched to groovy dsl.

Copy link
Copy Markdown
Contributor Author

@AlexeyKuznetsov-DD AlexeyKuznetsov-DD May 15, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bric3 thanks for pointing to this!

I switched all scripts to Kotlin DSL. And ./gradlew -PrunBuildSrcTests=true :buildSrc:test started to take ~20min. I investigated and after couple of rounds of refactoring I was able to do more-or less elegant way to reuse Gradle daemons! Now this test suite passed in ~1.5 mins locally and do not consume insane amount of RAM. But the change is quite huge and non trivial, please do one more round of review. With daemons reuse results are very impressive.

Copy link
Copy Markdown
Contributor

@bric3 bric3 May 15, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool thanks for this ! Indeed that's the way to go with Gradle : "daemon reuse". But it may be possible some tests need to offer a way for non reusable daemons for some tests 🤔 , maybe not yet though.

I'll take a second review

AlexeyKuznetsov-DD and others added 3 commits May 14, 2026 10:33
The Groovy→Kotlin DSL conversion of test scripts pushed :buildSrc:test
from 6 min to ~11 min because every test method killed its TestKit
daemon, forcing kotlinc to recompile every .gradle.kts in the next test.

Move testKitDir into a JVM-wide companion-object lazy val, remove the
per-test stopDaemons() call, and reap daemons once from the shutdown
hook. Shared daemons cut :buildSrc:test from 10 min 40 s down to
1 min 36 s on Kotlin DSL (and ~3.8× faster than the original Groovy DSL
runtime).

Fix MuzzleMavenRepoUtils.MUZZLE_REPOS, which cached MAVEN_REPOSITORY_PROXY
in a daemon-static `by lazy` val. With per-test daemons the cache was
harmless; with daemon reuse it leaked a stale repo URL into the next
test's resolution and caused version-range queries to fail. Replace it
with defaultMuzzleRepos(), a function that reads the env on each call.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@dd-octo-sts dd-octo-sts Bot added the tag: ai generated Largely based on code generated by an AI or LLM label May 14, 2026
@AlexeyKuznetsov-DD AlexeyKuznetsov-DD requested a review from bric3 May 15, 2026 00:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp: tooling Build & Tooling tag: ai generated Largely based on code generated by an AI or LLM tag: no release notes Changes to exclude from release notes type: refactoring

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants