-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathfunction.gradle
89 lines (73 loc) · 2.55 KB
/
function.gradle
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
class CollectTaskTimeListener implements TaskExecutionListener,BuildListener{
private long mStartTime;
private def timings = new HashMap<String, Long>() //存储所有task和其所发时间的对应关系
private def final MIN_COST = 5
//@Override
void buildStarted(Gradle gradle) {
}
@Override
void settingsEvaluated(Settings settings) {
}
@Override
void projectsLoaded(Gradle gradle) {
}
@Override
void projectsEvaluated(Gradle gradle) {
}
@Override
void buildFinished(BuildResult result) {
//输出排序后的统计数据
outputHeader("Task timings(sorted): ")
outputProfile(sortProfileData(timings).iterator())
println("\n")
outputTotalTime(timings.iterator())
}
@Override
void beforeExecute(Task task) {
mStartTime = System.currentTimeMillis();
}
@Override
void afterExecute(Task task, TaskState state) {
long ms = System.currentTimeMillis()-mStartTime;
timings.put(task.path, ms)
task.project.logger.warn "${task.path} took ${ms}ms"
}
void outputHeader(String headerMessage) {
println("\n======================================================")
println(headerMessage)
}
//输出收集的数据
void outputProfile(Iterator<Map.Entry<String, Long>> it) {
for (entry in it) {
if (entry.value >= MIN_COST) {
printf("%-50s %-15s\n", entry.key, entry.value + "ms")
}
}
}
//对task所花费的时间进行排序
List<Map<String, Long>> sortProfileData(Map<String, Long> profileData) {
List<Map.Entry<String, Long>> data = new ArrayList<>()
for (timing in profileData) data.add(timing)
Collections.sort(data, new Comparator<Map.Entry<String, Long>>() {
@Override
int compare(Map.Entry<String, Long> o1, Map.Entry<String, Long> o2) {
if (o1.value > o2.value) return -1
else if (o1.value < o2.value) return 1
return 0
}
})
return data
}
void outputTotalTime(Iterator<Map.Entry<String, Long>> it){
long totalTime
for (entry in it) {
totalTime += entry.value;
}
long minte = totalTime/1000/60;
long second = totalTime/1000-minte*60;
long milion = totalTime-second*1000-minte*60*1000;
println("Total Tasks took ${minte}min ${second}s ${milion}ms")
}
}
//添加自定义的监听
gradle.addListener(new CollectTaskTimeListener())