1#!/bin/bash
2set -e
3
4# This is a helper script to be called by androidx.sh
5# This script locates, parses, and merges build profiling information from various report files
6# A history of the results of running this script can be visualized at go/androidx-build-times
7
8cd "$(dirname $0)"
9
10if [ "$OUT_DIR" == "" ]; then
11  OUT_DIR=../../../../out
12fi
13if [ "$DIST_DIR" == "" ]; then
14  DIST_DIR="$OUT_DIR/dist"
15fi
16
17METRICS_DIR="$DIST_DIR/librarymetrics/build"
18INTERMEDIATES_DIR=$OUT_DIR
19
20# Find the metrics files that exist
21METRICS_FILES="$(echo $OUT_DIR/androidx/*/build/build-metrics.json | grep -v '*' || true)"
22
23# Look for a profile file and attempt to make a metrics json out of it
24PROFILE_FILES="$OUT_DIR/androidx/build/reports/profile/*.html"
25if ls $PROFILE_FILES >/dev/null 2>&1 ; then
26  # parse the profile file and generate a .json file summarizing it
27  PROFILE_JSON=$INTERMEDIATES_DIR/build_androidx.json
28  # Because we run Gradle twice (see TaskUpToDateValidator.kt), we want the second-to-last profile
29  ./parse_profile_html.py --input-profile "$(ls $PROFILE_FILES | sort | tail -n 2 | head -n 1)" --output-summary $PROFILE_JSON
30  METRICS_FILES="$METRICS_FILES $PROFILE_JSON"
31fi
32
33# Identify which log file we want to parse
34# Because we run Gradle twice (see TaskUpToDateValidator.kt), we want the second-to-last log
35LOG_FILE="$DIST_DIR/logs/gradle.1.log"
36
37# extract cache status from log file
38if [ -e "$LOG_FILE" ]; then
39  summaryLine="$(grep ' actionable task' "$LOG_FILE")"
40  if [ "$summaryLine" != "" ]; then
41    # sample line to parse: 621 actionable tasks: 149 executed, 472 from cache
42    # we want to extract:   ^^^                   ^^^
43    numTasksExecuted="$(echo "$summaryLine" | sed 's/ executed.*//' | sed 's/.*, //' | sed 's/.*: //')"
44    if [ "$numTasksExecuted" == "" ]; then
45      numTasksExecuted="0"
46    fi
47    numTasksFromCache="$(echo "$summaryLine" | sed 's/ from cache//' | sed 's/.*, //' | sed 's/.*: //')"
48    if [ "$numTasksFromCache" == "" ]; then
49      numTasksFromCache="0"
50    fi
51    numTasksActionable="$(echo "$summaryLine" | sed 's/ actionable task.*//' | sed 's/.*, //' | sed 's/.*: //')"
52    if [ "$numTasksActionable" == "" ]; then
53      numTasksActionable="0"
54    fi
55
56    CACHE_STATS_FILE="$OUT_DIR/androidx/build/cache-stats.json"
57    echo -n "{ \"num_tasks_executed\": $numTasksExecuted, \"num_tasks_from_cache\": $numTasksFromCache , \"num_tasks_actionable\": $numTasksActionable }" > "$CACHE_STATS_FILE"
58    METRICS_FILES="$METRICS_FILES $CACHE_STATS_FILE"
59  fi
60fi
61
62if [ "$METRICS_FILES" != "" ]; then
63  # merge all profiles
64  mkdir -p "$METRICS_DIR"
65  # concatenate files, and replace "}{" with ", ", ignoring whitespace
66  cat $METRICS_FILES | sed 's/ *} *{ */, /g' > $METRICS_DIR/build_androidx.json
67  # remove metrics files so that next time if Gradle skips emitting them then we don't get old results
68  rm -f $METRICS_FILES
69fi
70