Skip to content

Commit f266fca

Browse files
authored
Merge pull request #1428 from gradle/gk/transformCachingExperiment
Added the Remote cache experiments for transforms script
2 parents 848adaf + 362bbd3 commit f266fca

File tree

2 files changed

+128
-0
lines changed

2 files changed

+128
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Artifact transform remote build cache performance experiment
2+
3+
## Overview
4+
5+
This script is used to test the performance of remote caching for transforms.
6+
7+
It will run 3 builds:
8+
9+
1. Init/seed build, that writes to remote build cache. The scan for this build will have a tag of `remote-cache-experiment-init`.
10+
2. Transform caching enabled - pulls from remote cache. The scan for this build will have a tag of `baseline-transforms`.
11+
3. Transform caching disabled - executes the transforms. The scan for this build will have a tag of `disabled-cache-transforms`.
12+
13+
You can then use the published build scans to evaluate how caching artifact transforms affects your overall build time.
14+
15+
If results show that (remote) caching of transforms is not beneficial for your project, you can disable caching for (specific) transforms using the flags in this script.
16+
17+
## Requirements
18+
19+
- A Develocity instance to publish scans to
20+
- Access key with remote build cache write permission
21+
- Gradle version 8.9 or higher
22+
- The [Common Custom User Data Gradle plugin](https://github.com/gradle/common-custom-user-data-gradle-plugin) is expected to be applied to the project
23+
24+
## Usage
25+
26+
1. Ensure you have an access key with remote build cache write permission.
27+
2. Copy the script into your project directory.
28+
3. (Optional) If you're using a HTTP cache connector, uncomment line 31 and set the remote cache URL in the script to use a cache shard instead of the default cache.
29+
4. Having the key set in the environment is expected - note that **remote build cache write is required**. If the key stored in your Gradle user home is missing the remote build cache write permission you can set the key in the environment by running `export DEVELOCITY_ACCESS_KEY=<develocity-url>=<your-access-key>`, either in the script, or in the terminal before running the script.
30+
5. Run the script with `./transforms-caching-experiment.sh`. It will run the Gradle `help` task by default, but you can specify a different task by passing it as an argument to the script.
31+
6. Inspect and compare build times
32+
33+
### Invocation
34+
35+
To run the script with the `help` task (default), use the following command:
36+
```bash
37+
./transforms-caching-experiment.sh
38+
```
39+
40+
To run the script with a specific Gradle task(s), use the following command:
41+
```bash
42+
./transforms-caching-experiment.sh <gradle-tasks-and-args>
43+
```
44+
45+
## Advanced usage - disabling specific transforms caching
46+
47+
If you are sure you are only suffering negative avoidance savings because of a select list of transforms retreiving outputs from cache you can disable only caching for those. To disable specific transforms caching, you can uncomment the line 48 in the script to enable running also experiments with specific transforms caching disabled. You will also need to modify the `disabledTransforms` variable in the script to specify the transforms you want to disable - there is an example supplied in the script. This will run the last two builds again, but with the specified transforms caching disabled - the tags added in scans for those build will be `baseline-transforms-selected` and `disabled-cache-transforms-selected`. You can use https://github.com/cdsap/ArtifactTransformReport to get statistics on Artifact transforms for a single build or aggregating multiple builds.
48+
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#!/bin/bash
2+
3+
# REQUIRED: Having the key set in the environment is expected - note that remote build cache write is requiered. It can be set here or exported in the terminal before running the script.
4+
#export DEVELOCITY_ACCESS_KEY=$develocityUrl=<access-key>
5+
6+
gradleVersion=$(./gradlew --version | grep "Gradle" | awk '{print $2}')
7+
8+
major=$(echo "$gradleVersion" | cut -d. -f1)
9+
minor=$(echo "$gradleVersion" | cut -d. -f2)
10+
11+
# Check if the version is lower than 8.9
12+
if [ "$major" -lt 8 ] || { [ "$major" -eq 8 ] && [ "$minor" -lt 9 ]; }; then
13+
echo "Gradle version $gradleVersion is lower than 8.9"
14+
exit 1
15+
else
16+
echo "Gradle version detected: $gradleVersion"
17+
fi
18+
19+
homeDir=build/HOME
20+
21+
# Set 'task' to the first argument or 'help' if no arguments are provided
22+
tasks=${*:-help}
23+
24+
# Initialize empty Gradle User Home with settings to run build
25+
echo "Initializing Gradle User Home directory at $homeDir"
26+
rm -rf $homeDir
27+
mkdir -p $homeDir
28+
mkdir -p $homeDir/caches/"$gradleVersion"/
29+
cp ~/.gradle/gradle.properties $homeDir
30+
cp -r ~/.gradle/caches/"$gradleVersion"/generated-gradle-jars $homeDir/caches/"$gradleVersion"/
31+
cp -r ~/.gradle/develocity/ $homeDir/develocity/
32+
cp -r ~/.gradle/enterprise/ $homeDir/enterprise/
33+
34+
# Note: This is expecting that CCUD Gradle plugin is applied
35+
export GRADLE_CACHE_REMOTE_PUSH=true
36+
export GRADLE_CACHE_REMOTE_PATH="cache/$USER-exp-non-task"
37+
#export GRADLE_CACHE_REMOTE_URL="<develocityUrl>/cache/$USER-exp-non-task" # Needed if the HTTP cache connector is used
38+
39+
echo "------------------------------------------------------------"
40+
echo "Priming build with task '$tasks' and HOME=$homeDir"
41+
echo "------------------------------------------------------------"
42+
set -x
43+
# shellcheck disable=SC2086
44+
./gradlew $tasks -g $homeDir -Dscan.tag.remote-cache-experiment-init --no-configuration-cache -Ddevelocity.deprecation.muteWarnings=true -Dscan.uploadInBackground=false -Dgradle.cache.local.enabled=false --no-daemon
45+
set +x
46+
47+
runs='transforms'
48+
# runs='transforms transforms-selected' # Uncomment to test with selected transforms disabled
49+
50+
for run in $runs
51+
do
52+
# Set args based on cache
53+
if [ "$run" == 'transforms' ]
54+
then
55+
disabledCacheArgs='-Dorg.gradle.internal.transform-caching-disabled'
56+
elif [ "$run" == 'transforms-selected' ]
57+
then
58+
# Specify the transforms to disable. Example below:
59+
disabledTransforms='org.jetbrains.kotlin.gradle.internal.transforms.BuildToolsApiClasspathEntrySnapshotTransform,org.jetbrains.kotlin.gradle.internal.transforms.ClasspathEntrySnapshotTransform'
60+
disabledCacheArgs="-Dorg.gradle.internal.transform-caching-disabled=$disabledTransforms"
61+
fi
62+
63+
for args in "-Dscan.tag.baseline-$run" "-Dscan.tag.disabled-cache-$run $disabledCacheArgs"
64+
do
65+
echo "------------------------------------------------------------"
66+
echo "Test caches/*/transforms removal with $args"
67+
echo "------------------------------------------------------------"
68+
set -x
69+
70+
echo "Removing transforms from $homeDir/caches"
71+
rm -rf $homeDir/caches/*/transforms
72+
rm -rf $homeDir/caches/transforms-* # Also remove the transforms for Gradle 8.7
73+
74+
# shellcheck disable=SC2086
75+
./gradlew $tasks -g $homeDir --no-configuration-cache -Ddevelocity.deprecation.muteWarnings=true -Dscan.uploadInBackground=false -Dgradle.cache.local.enabled=false --no-daemon $args
76+
77+
set +x
78+
echo ""
79+
done
80+
done

0 commit comments

Comments
 (0)