How to analyse all your Android Gradle build times from your computer
Tags: android, gradle
Published: 2022-02-28 21:09:14

Did you know every time you build your Android project with Gradle that data gets logged somewhere?

It can be useful to understand how much time you're spending waiting around for things to build. It can help give the impact to get better equipment, or spend time working on improving it. I believe things that get tracked get improved! It can also help your performance review to evidence how much time you've saved everyone!

So where do we start?

These instructions are for OS X: If you open Finder or Terminal, and navigate to the `//Users/{your name}/.gradle/daemon/` folder you will see a folder for every version of gradle you have used on your computer.

![Finder window showing where the out.log files are located inside the gradle daemon directory](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fd52zabp6jowyinvaq8j.png)

Inside those folders are a bunch of log files that contain every build you've ever run! You can open these folders with a text editor and search for the phrase "BUILD SUCCESSFUL" or "BUILD FAILED".

![Screenshot of a gradle log file demonstrating a successful build](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2wmek3gl7fjrj7vda8n3.png)


We can spend some time manually looking around these files, however as programmers we can probably write something to do this for us!

I've used JavaScript and NodeJs to write my tool - which your welcome to use: https://gist.github.com/daniellevass/c5a7655c83bac1293d6fa3f797a0dd20

Please update the directory variable on L4 to match where your files are. Also note on L6 & L7 this is where the output file name.

I'll explain how this tool works:

It first opens the directory you provide on L4. Finding the list of versions you've got records. Then parsing each file that ends in `.out.log`.

The difficult part is processing each log file. First we have to find all the lines we care about - in my example this is `BUILD SUCCESSFUL` and `BUILD FAILED`. Once we've found a line that we care about, we have to work out the duration in a sensible format. We get lines that look like:

```
BUILD SUCCESSFUL in 1m 32s
```

I want to turn that into 92, to track the seconds uniformly. So we've had to write some processing to turn `21s` into 21, `1m 32` into 92, and `120ms` into 1 (milliseconds are too much precision for me, so we're just rounding up to 1s).

Once we've parsed all of those we're starting to get somewhere! The next problem is it would be useful to have the date and timestamp to indicate when these logs happened. 

This is a bit more challenging as each `BUILD SUCCESSFUL` or `BUILD FAILED` doesn't actually have a date or timestamp on that row! Fortunately almost all the other rows actually start with a timestamp!

So what we're doing is checking if a row starts with `"202"`, I've not had my laptop longer than 2020 so this matches all the timestamp rows. I then keep a local variable that holds the last timestamp read. This is good enough for my use case 🤷‍♀️. 

Finally once we've processed all the data we can save it to a csv file!

You can use something like Microsoft Excel to open csv files, or Google Sheets. You'll need to add some headings to indicate what the data is. After you've done that, you can create a pivot table with the data.

I configured the rows to be the date - in ascending order. Set up 3 values: one for AVERAGE of seconds, one for MAX of seconds, and one for COUNT of seconds. Then I set filter to status. This lets you remove out failed builds if you're not interested in that data. 

![Google Sheets demonstrating the pivot table of my build data](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ombjzy3yj0vc82ohb5oi.png)