Gradle: Performance Is a Feature
How does Gradle perform as a build system? Very, very well! Read on for the details.
Join the DZone community and get the full member experience.Join For Free
at gradle inc., we take build performance seriously. while we bundle performance improvements into every gradle release, we’ve kicked off a concerted effort called a performance burst from gradle 2.13 in order to make building software faster and more enjoyable for all of our users. in this blog post, we will explore how we approach performance issues, as well as what improvements to expect in the 2.13 release and beyond.
the fastest thing to do is nothing
building software takes time, which is why the biggest performance improvement is cutting steps out of it entirely. that’s why, unlike traditional build tools such as maven or ant, gradle focuses on incremental builds. why would you ever run
when you don’t need to? for some developers, running
became a conditioned response to a broken build tool. gradle doesn’t have such an issue: aware of all inputs and outputs of a task, it is reliably capable of handling incremental builds. most builds will be incremental, and that’s why we focus so heavily on optimizing this case. one way that we accomplish this is through the gradle daemon.
the gradle daemon can dramatically improve your build performance by allowing build data to persist in memory between build invocations and avoiding jvm startup times on each build. the daemon is a hot jvm hosting the gradle runtime, making it possible to run subsequent builds much faster: instead of spawning a new jvm for each build, we can benefit from all the goodness of having a cached jvm – in particular, we realize a strong benefit from jit (just in time compilation). while turning on the daemon has a cost for the first build, the amount of time that you will gain for each subsequent build more than offsets the initial cost. in gradle 2.13, we focused our improvements when the daemon is activated, and we’re preparing to enable this by default in gradle 3.0. other performance improvements we’ve implemented will benefit all users, independently of whether they use the daemon or not (and if you don’t use the daemon yet, we strongly encourage you to try it out!).
as you can read in our release notes , we’ve emphasized several categories of performance improvements:
- reducing the build configuration time, that is to say, reducing the fixed cost of creating and configuring a gradle build
- reducing the test execution time; i.e., reducing the overhead of gradle compared to just executing tests in an ide
- improving the performance of importing a project in an ide
- reducing communication latency between the interactive gradle client and the daemon process
reducing configuration time
here’s an idea of the improvement you can expect:
so the example above yields a typical performance test metric: we’re comparing the average execution time of a build when we run
for a project that contains a lot of subprojects (10000 projects). you can see that when we started optimizing configuration time, the
branch was slower than gradle 2.7. now, gradle 2.13 is faster than ever! we have measured up to 25% reduction on our own builds! however, more than the improvement, it’s
we get to this that is important. improving performance is a process, and here is how it works.
performance test suite
the gradle sources contain a sub-project dedicated to performance tests . this test suite is very particular, and allows us:
to compare the performance of the
masterbranch with previous releases of gradle
- to compare various build scenarios against a single version of gradle
so typically, in the example above, we’re comparing the average execution time of a build, when we run
, in a specific scenario (an empty build with 10000 sub-projects), and compare it with previous gradle releases. it’s worth noting that this performance test suite is executed daily, allowing us to catch performance regressions very early in the development phase.
writing a performance test scenario
so how, in practice, do we write a performance test? it all starts with a scenario we want to test. for example, we want to make sure that we reduce the duration of test execution. the first step is then to write a build template that will let us test gradle against this scenario. and a template has various parameters: the number of sub-projects, the number of (test) classes in source, external dependencies, … this let us generate sample gradle builds that are used to measure performance. of course, those performance test builds are generated with gradle .
all the graphs you see below were generated using fully automated performance tests, and aimed at testing specific scenarios. should you find a performance issue with gradle, this is a great way to get started: create a new template, then send us a pull request to show the problem . of course, all our performance tests are regular test cases, which means that we can fail the build if we introduce a regression .
since gradle 2.13 is primarily a performance-enhancing release, let’s focus on some of the improvements.
gradle vs. maven
in this scenario, we are comparing the time it takes to execute
gradle clean test
mvn clean test
. as we mentioned earlier, cleaning is not necessary in gradle, but we do it here for the sake of comparison against maven, and to assess the “cold build” time. here are the results:
at the end of february, maven and gradle were comparable. since then, the new performance improvements in gradle 2.13 have resulted in a 10% speedup! you can notice that the graph contains some glitches: on april 2nd, you can see that the time considerably increased. however, it increased in both scenarios: maven and gradle. so what you need to keep in mind when reading such graphs is that results are relative between them for any same date. this is important because:
- we could change the templates between two executions of the performance build, resulting in an increase or decrease of the build time.
- we could change hardware between two executions, leading to the same side effects
profiling is better than guessing
so how did we manage to improve this? first of all, once a scenario is written and performance tests running, we need to profile the builds. for that purpose, we’re using different tools, from
yourkit java profiler
java mission control
, the jit logs or simply good old
statements. in the end, we try to identify what is causing slowdown and write a document summarizing our findings. those documents are all public, and you can find them in our
. once we’ve identified hotspots and written down the profiling results, we extract stories for improvement and actually go to the implementation phase. this “profiling to stories” phase is very important, because while a profiler will be very helpful in identifying hotspots, it will be no help when it comes to interpreting the results: often, rewriting an algorithm can be much more efficient than trying to optimize a sax parser…
optimizing the communication between the daemon and the client
as we explained, we’re primarily (but not only) focusing on improving performance when the daemon is activated. one issue with the daemon is that you have a forked jvm. when you run
, the client process, the one from the command-line, starts communicating with a long-living process, the daemon, which is effectively executing the build. and typically, to see the logs as the build is running, you need to forward events from the daemon to the client. before 2.13, this communication was synchronous. this means that the log messages were sent synchronously between the daemon and the client. this was inefficient, because we were blocking on network i/o where we could actually perform some build operations. in 2.13, not only is communication asynchronous, but we also optimized the protocol that is used to communicate between the client and the daemon and how the client responds to these events.
forked processes start up faster
another improvement that was made is visible in the following scenario:
this scenario is “unfair” to gradle, and meant to compare what happens when we just want to re-execute the tests. as you may know, when running
, maven will re-execute the tests even if nothing changed. gradle does nothing in that case, because everything is “up-to-date”. so to emulate the behavior of maven, we need to clean-up the test results so that we re-execute the tests and re-generate the reports. as you can see, in this scenario, gradle was significantly slower than maven. now, it is faster, while doing also more work: gradle not only runs the tests, but also generates 3 types of reports: a binary one, an xml one (for ci integration) and eventually an html report (for use by us, poor humans, but you can
disable this behavior
). gradle 2.12 is 15% slower in this scenario, and a large amount of improvement has been done by optimizing the classpath of the forked jvms used for tests. in 2.12, almost the whole gradle classpath was used on forked vms, when in reality we just need a subset of gradle classes (basically to communicate between the forked vm and the daemon). by optimizing this classpath, we can now reduce classpath scanning and significantly improve the time it takes to execute tests. if you ever noticed a “pause” when gradle was about to execute tests, it has now gone!
reports are generated in parallel
part of the improvement on test execution is also obtained thanks to a parallel generation of reports. as we explained, gradle generates more reports than maven by default. this is usually what you want, because when you’re developing an application and run tests locally, having to decipher xml test reports can be very frustrating. with gradle 2.13, now, the html and xml reports are generated in parallel, which significantly reduces the time required before starting the test suite of the next project. the more modules your project has, the more likely you will see a significant reduction in build duration.
improving build startup time
faster script compilation
when executing gradle builds for the first time, you can see, as part of the “configuration” phase, that gradle is actually compiling the build scripts. despite being scripts, gradle build files are written in groovy and are nevertheless compiled to bytecode. this is time-consuming, but has been optimized by the gradle team. in particular, gradle has to compile the scripts several times, with different classpaths, in order to compile scripts that contain references to remote resources such as plugins.
in gradle 2.13, we changed the way gradle scripts are compiled, and optimized two scenarios:
- running several builds concurrently from the same directory (this often happens on ci). before this, the “script cache” that gradle uses was locked during the execution of a build, so if a build script was changed during the execution of a build, all concurrent builds were locked until the first one finishes.
- re-use build scripts independently of their location. imagine that you have multiple projects using the same remote scripts. this is typically the case in corporate environments, where a script defines some credentials, conventions, or plugins to be used in all builds of the company. then, each project had to compile the script before being able to use it. gradle 2.13 changed that, and now compiles script based on their actual contents (and classpath) rather than their location. it means that if you have 2 projects which have the same build files but in different locations, the script will only be compiled once. however, to be able to report build errors on the correct build file, we’re also using a “relocation technique”, which takes a compiled script class and remaps it to an actual script file so that errors are reported correctly.
another work that has been done in 2.13 is improving the classpath of gradle, so that services are located faster. when you have a lot of jars on classpath, ordering is important, and the number of classes is important. even if you “only” gain 10ms, it can lead to significant differences when builds are often executed, in particular from the ide, which leads to the last area of improvement we worked on in 2.13.
sometimes, improving performance is a matter of serendipity. we recently discovered that some performance tests were executing significantly faster on our ci server than locally, but were unsure of the cause. after doing some profiling, we realized that the code to propagate properties from the various
files to the actual
was very inefficient: the more properties you had in your various
file, the longer it would take to start the build! we identified the problem and fixed this.
faster ide integration
the tooling api typically allows ide vendors to integrate gradle. this is what we do with buildship . it has very specific needs and in particular, it has to be both backwards and forward compatible, meaning a certain version of the tapi can execute gradle builds for both older and newer versions of gradle. of course, a developer would only benefit from the latest improvements by using both the latest version of the tooling api and gradle, but it leads to interesting architecture.
in this case, the tooling api heavily relies on reflection to invoke methods. in gradle 2.13, we significantly improved caching, which led to spectacular results:
this scenario illustrates how long it takes to import, typically, a 500 sub-projects build into eclipse. while it took 25s with the 2.12 version of the tooling api, it’s now only 10s. and you can even see more spectacular results in intellij idea, where they are using “custom models”. imports/synchronizing projects would then be orders of magnitude faster.
there’s more to come!
we cannot close this blog post without illustrating what we mean by “doing nothing is better”. in the maven vs gradle examples above, we’ve tried to “emulate” the behavior of maven with gradle. here is, typically, the graph that you would get when running proper
builds with gradle. that is to say that you open and edit several files from different sub-modules then re-execute the tests. remember, with gradle, you no longer have to
, but we were fair and didn’t clean with maven either:
yes, gradle is almost 6x as fast in this scenario. so now, imagine doing this 10, 100 times a day, multiplied by the number of developers in your company. and realize how much money it is.
thanks for reading this, and don’t worry: there’s more to come, stay in touch for more performance improvements in gradle 2.14!
Published at DZone with permission of Emilie Couturier. See the original article here.
Opinions expressed by DZone contributors are their own.