|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +import java.util.concurrent.ConcurrentHashMap |
| 18 | +import java.util.stream.Collectors |
| 19 | + |
| 20 | +// Measure aggregate wall time of each task execution time (summed up across |
| 21 | +// all projects). |
| 22 | + |
| 23 | +if (propertyOrDefault("task.times", null) != null) { |
| 24 | + gradle.taskGraph.whenReady { TaskExecutionGraph graph -> |
| 25 | + def taskTimes = new ConcurrentHashMap<String, Long>() |
| 26 | + def measured = graph.allTasks.findAll { task -> true } |
| 27 | + |
| 28 | + graph.addTaskExecutionListener(new TaskExecutionListener() { |
| 29 | + @Override |
| 30 | + void beforeExecute(Task task) { |
| 31 | + def start = System.currentTimeMillis() |
| 32 | + if (measured.contains(task)) { |
| 33 | + taskTimes.put(task.path, start) |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + @Override |
| 38 | + void afterExecute(Task task, TaskState taskState) { |
| 39 | + def stop = System.currentTimeMillis() |
| 40 | + if (measured.contains(task)) { |
| 41 | + if (taskState.didWork) { |
| 42 | + taskTimes.compute(task.path, { k, v -> stop - v }) |
| 43 | + } else { |
| 44 | + taskTimes.remove(task.path) |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | + }) |
| 49 | + |
| 50 | + // After the build is finished, check the test count. |
| 51 | + gradle.buildFinished { |
| 52 | + // Compute aggregate times of all task execution times. |
| 53 | + def aggregates = taskTimes.entrySet().stream() |
| 54 | + .collect(Collectors.groupingBy({e -> e.key.replaceAll(".*:", "")})) |
| 55 | + .entrySet().stream() |
| 56 | + .collect(Collectors.toMap( |
| 57 | + {e -> e.key }, |
| 58 | + {e -> e.value.stream().mapToLong({ entry -> entry.value }).sum() } |
| 59 | + )) |
| 60 | + |
| 61 | + logger.lifecycle("Aggregate task times (possibly running in parallel!):") |
| 62 | + aggregates.entrySet().stream() |
| 63 | + .sorted({a, b -> Long.compare(b.value, a.value) }) |
| 64 | + .forEach {v -> |
| 65 | + logger.lifecycle(String.format(Locale.ROOT, " %6.2f sec. %s", v.value / 1000.0, v.key)) |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | +} |
0 commit comments