During development you'll be running Gradle builds many times so it is important that they run as quickly as possible. Learn how to profile your Gradle builds so you can see how long they take to run and identify opportunities for improving performance.
- [Instructor] In this video we'll talk about profiling a Gradle build. We'll first spend a little bit of time talking about what a Gradle profile is. Then we'll run a profile. Then we'll find where the profile output is in the directory structure. And then, we'll spend a little bit of time trying to understand the profile output. So what is a Gradle profile? Well, essentially it's just a report on the build that highlights the performance of the build. It can be created any time that you run a task. And it can also be saved permanently to record a history of the build performance so you can analyze how the build performance has improved, or degraded over time.
So let's look at some code. I'm in IntelliJ and I'm looking at the Java project that I've been working on in the prior videos. If you're starting at this point you can load the starter project from the exercise files. So let's run a Gradle profile. It's not necessary to add anything to our build.gradle file to enable profiling. That's automatically enabled in Gradle. I'll open up my terminal. And now I'll run my build. Gradle build. And to enable profiling, I simply add the dash dash profile argument to the end of the command.
I'll now run my build. And I can see that the output looks the same as it always does. So nothing's different there except for the very bottom of the terminal output. And you'll notice that there's a line that says, See the profiling report. And this provides our location of the profiling report. Let me close my terminal window. And I'll use the information that I saw in the terminal output to find the report. It was under the build directory. The report sub-directory. The profile subdirectory underneath that.
Before I look at the report let's run it again. I'll open up the terminal. And I'll run the build again. Gradle build with the profile option. Let me close the terminal window. And you'll notice now that we have two reports there with slightly different timestamps. And every time you run the profile it'll add an additional report there. So that you can see the history of profiling over time. Let me open up the first report. I'll right click and I will open the HTML page in the default browser.
We can see that we've got the profile report. The first line tells us it is a profile report. The second line tells us that it's a result of the build task. We can see the date and time that we started it. And then we've got four different sections. The summary section shows us our overall times for these high level phases. The configuration section shows us the projects that we have. The dependency resolution section shows us how long it took to resolve the dependencies. And to acquire them from the Internet if that was a necessary step.
And then the final section shows us each individual task underneath the initial task that we executed and how long that task took. Also, the final column, called result shows us whether the task had to be run again or it's the output from it did not change so it wasn't necessary to run it. You'll notice that many of our tasks were up to date so were going to get a much faster execution time than if we were building from scratch, for instance. Let me go back to IntelliJ.
One final point that I'll make is the profile reports are saved in the build directory. If you do clean task remember, that removes the build directory so the profile and reports will disappear, as well. So if you do want to save them permanently you may want to copy them to some other directory.
- The purpose of Gradle
- Building a Java project with Gradle
- Adding a dependency to a project
- Creating a library module
- The Gradle project structure
- Creating new Gradle tasks
- Profiling and using the Gradle cloud