Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Gradle tasks cacheable #295

Merged
merged 9 commits into from
Nov 15, 2023
Merged

Conversation

marcospereira
Copy link
Contributor

What?

Gradle has a build cache that can be used to speed up the build process. From Gradle docs:

The Gradle build cache is a cache mechanism that aims to save time by reusing outputs produced by other builds. The build cache works by storing (locally or remotely) build outputs and allowing builds to fetch these outputs from the cache when it is determined that inputs have not changed, avoiding the expensive work of regenerating them.

I noticed that the build cache for the compileKotlin task was not working when using jte's generate or precompile, because the inputs generated by those tasks were not cached. Therefore, the generated code would be considered new input for compileKotlin, meaning the task cannot use the build cache. I suspect the same applies to compileJava, but I didn't check.

Local validation

Comparison when running the compileKotlin task with and without -Dorg.gradle.caching=true.

Before

Running with --build-cache multiple times has no effect since the cache is always stale:

time -h -p gradle --warning-mode=none --build-cache clean compileKotlin
> Task :cleanGenerateJte
> Task :cleanPrecompileJte UP-TO-DATE
> Task :clean
> Task :checkKotlinGradlePluginConfigurationErrors

> Task :generateJte
Extension gg.jte.models.generator.ModelExtension generated 3 files.

> Task :kspKotlin
> Task :compileKotlin

BUILD SUCCESSFUL in 6s
7 actionable tasks: 6 executed, 1 up-to-date
real 6.96
user 1.10
sys 0.16

After

With no cache (--warning-mode=none for brevity):

time -h -p gradle --warning-mode=none clean compileKotlin
> Task :cleanGenerateJte
> Task :cleanPrecompileJte UP-TO-DATE
> Task :clean
> Task :checkKotlinGradlePluginConfigurationErrors

> Task :generateJte
Extension gg.jte.models.generator.ModelExtension generated 3 files.

> Task :kspKotlin
> Task :compileKotlin

BUILD SUCCESSFUL in 8s
7 actionable tasks: 6 executed, 1 up-to-date
real 9.30
user 1.17
sys 0.15

With a populated build-cache (notice the FROM-CACHE):

time -h -p gradle --warning-mode=none --build-cache clean compileKotlin
> Task :cleanGenerateJte
> Task :cleanPrecompileJte UP-TO-DATE
> Task :clean
> Task :checkKotlinGradlePluginConfigurationErrors
> Task :generateJte FROM-CACHE
> Task :kspKotlin FROM-CACHE
> Task :compileKotlin FROM-CACHE

BUILD SUCCESSFUL in 1s
7 actionable tasks: 3 executed, 3 from cache, 1 up-to-date
real 1.49
user 1.13
sys 0.16

Let's see the effect on testClasses after running the command above:

time -h -p gradle --warning-mode=none --build-cache clean testClasses
> Task :cleanGenerateJte
> Task :cleanPrecompileJte UP-TO-DATE
> Task :clean
> Task :checkKotlinGradlePluginConfigurationErrors
> Task :generateJte FROM-CACHE
> Task :kspKotlin FROM-CACHE
> Task :compileKotlin FROM-CACHE
> Task :compileJava NO-SOURCE
> Task :processResources
> Task :classes
> Task :kspTestKotlin
> Task :processTestResources
> Task :compileTestKotlin
> Task :compileTestJava NO-SOURCE
> Task :testClasses

BUILD SUCCESSFUL in 8s
11 actionable tasks: 7 executed, 3 from cache, 1 up-to-date
real 8.94
user 1.21
sys 0.18

And then, since the build cache was populated:

time -h -p gradle --warning-mode=none --build-cache clean testClasses
> Task :cleanGenerateJte
> Task :cleanPrecompileJte UP-TO-DATE
> Task :clean
> Task :checkKotlinGradlePluginConfigurationErrors
> Task :generateJte FROM-CACHE
> Task :kspKotlin FROM-CACHE
> Task :compileKotlin FROM-CACHE
> Task :compileJava NO-SOURCE
> Task :processResources
> Task :classes
> Task :kspTestKotlin FROM-CACHE
> Task :compileTestKotlin FROM-CACHE
> Task :compileTestJava NO-SOURCE
> Task :processTestResources
> Task :testClasses

BUILD SUCCESSFUL in 1s
11 actionable tasks: 5 executed, 5 from cache, 1 up-to-date
real 2.40
user 1.26
sys 0.16

@marcospereira
Copy link
Contributor Author

@casid, I'm trying to figure out how to add a test for this, though.

Copy link

codecov bot commented Nov 3, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (241efe8) 91.21% compared to head (5170ff2) 91.15%.

Additional details and impacted files
@@             Coverage Diff              @@
##               main     #295      +/-   ##
============================================
- Coverage     91.21%   91.15%   -0.07%     
+ Complexity     1197     1196       -1     
============================================
  Files            76       76              
  Lines          3132     3132              
  Branches        484      484              
============================================
- Hits           2857     2855       -2     
- Misses          167      168       +1     
- Partials        108      109       +1     

see 1 file with indirect coverage changes

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@edward3h
Copy link
Contributor

edward3h commented Nov 3, 2023

I don't have time to have a detailed look, but I skimmed through and it seems good.

@marcospereira

This comment was marked as resolved.

@marcospereira

This comment was marked as outdated.

@marcospereira marcospereira marked this pull request as draft November 3, 2023 18:58
@casid
Copy link
Owner

casid commented Nov 5, 2023

Unfortunately, I'm not really a gradle expert, so I can't help much with this. :-(

@marcospereira marcospereira marked this pull request as ready for review November 8, 2023 01:34
@marcospereira

This comment was marked as duplicate.

.github/workflows/maven.yml Outdated Show resolved Hide resolved
.github/workflows/maven.yml Show resolved Hide resolved
jte-gradle-plugin/build.gradle Show resolved Hide resolved
test/gradle-test-wrapper/build.gradle Outdated Show resolved Hide resolved
@marcospereira
Copy link
Contributor Author

@casid @edward3h build is green, and this is good to be reviewed. :)

Copy link
Owner

@casid casid left a comment

Choose a reason for hiding this comment

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

Thank you for your contribution @marcospereira!

I left a few notes that need to be changed, so that we don't miss platform specific bugs in the future.

@edward3h could you have a quick look at the changes to the Gradle plugin, if possible? You have a lot more expertise here as I do :-)

.github/workflows/maven.yml Outdated Show resolved Hide resolved
.github/workflows/maven.yml Show resolved Hide resolved
test/gradle-test-wrapper/build.gradle Outdated Show resolved Hide resolved

public static Stream<Arguments> checkBuildCache() {
return Stream
.of(Paths.get("../jte-runtime-cp-test-models-gradle"), Paths.get("../jte-runtime-test-gradle-convention"))
Copy link
Owner

Choose a reason for hiding this comment

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

Hmm, what about the other test projects?

gg.jte.GradleMatrixTest#runGradleBuild()

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We are only interested in testing that the tasks are cacheable and not checking every project (done in the other above).

Copy link
Contributor Author

@marcospereira marcospereira left a comment

Choose a reason for hiding this comment

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

Hey @casid, fixed most of the comments and added a reply to the test selection in GradleMatrixTest. See if this looks better for you now.


public static Stream<Arguments> checkBuildCache() {
return Stream
.of(Paths.get("../jte-runtime-cp-test-models-gradle"), Paths.get("../jte-runtime-test-gradle-convention"))
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We are only interested in testing that the tasks are cacheable and not checking every project (done in the other above).

@casid
Copy link
Owner

casid commented Nov 10, 2023

LGTM, would wait a few more days with the merge, so that @edward3h might have a chance to look at it.

@edward3h
Copy link
Contributor

I'm busy this weekend, but I can take a look on Monday.

@edward3h
Copy link
Contributor

I am happy with this change. Thanks @marcospereira !

@marcospereira
Copy link
Contributor Author

I am happy with this change. Thanks @marcospereira !

Awesome! Thanks for taking the time to review, @edward3h.

@casid, I guess this is good to merge/release then. 🍾

@casid casid merged commit 827e4e7 into casid:main Nov 15, 2023
7 checks passed
@casid
Copy link
Owner

casid commented Nov 15, 2023

Thanks everyone!

@marcospereira marcospereira deleted the gradle/cacheable-tasks branch November 15, 2023 19:06
@marcospereira
Copy link
Contributor Author

Hey @casid, just a quick check about when we can have a release with these changes. 🚀

@casid
Copy link
Owner

casid commented Nov 21, 2023

@marcospereira I just released version 3.1.5 :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants