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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
|
/*
* SonarQube
* Copyright (C) 2009-2017 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.scanner.profiling;
import com.google.common.annotations.VisibleForTesting;
import java.io.File;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import javax.annotation.Nullable;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.api.CoreProperties;
import org.sonar.api.batch.events.InitializerExecutionHandler;
import org.sonar.api.batch.events.InitializersPhaseHandler;
import org.sonar.api.batch.events.PostJobExecutionHandler;
import org.sonar.api.batch.events.PostJobsPhaseHandler;
import org.sonar.api.batch.events.ProjectAnalysisHandler;
import org.sonar.api.batch.events.SensorExecutionHandler;
import org.sonar.api.batch.events.SensorsPhaseHandler;
import org.sonar.api.resources.Project;
import org.sonar.api.utils.System2;
import org.sonar.api.utils.TimeUtils;
import org.sonar.scanner.bootstrap.GlobalProperties;
import org.sonar.scanner.events.BatchStepHandler;
import org.sonar.scanner.util.ScannerUtils;
import static org.sonar.scanner.profiling.AbstractTimeProfiling.sortByDescendingTotalTime;
import static org.sonar.scanner.profiling.AbstractTimeProfiling.truncate;
public class PhasesSumUpTimeProfiler implements ProjectAnalysisHandler, SensorExecutionHandler, PostJobExecutionHandler,
SensorsPhaseHandler, PostJobsPhaseHandler, InitializersPhaseHandler, InitializerExecutionHandler, BatchStepHandler {
static final Logger LOG = LoggerFactory.getLogger(PhasesSumUpTimeProfiler.class);
private static final int TEXT_RIGHT_PAD = 60;
private static final int TIME_LEFT_PAD = 10;
@VisibleForTesting
ModuleProfiling currentModuleProfiling;
@VisibleForTesting
ModuleProfiling totalProfiling;
private Map<Project, ModuleProfiling> modulesProfilings = new HashMap<>();
private final System2 system;
private final File out;
public PhasesSumUpTimeProfiler(System2 system, GlobalProperties bootstrapProps) {
String workingDirPath = StringUtils.defaultIfBlank(bootstrapProps.property(CoreProperties.WORKING_DIRECTORY), CoreProperties.WORKING_DIRECTORY_DEFAULT_VALUE);
File workingDir = new File(workingDirPath).getAbsoluteFile();
this.out = new File(workingDir, "profiling");
this.out.mkdirs();
this.totalProfiling = new ModuleProfiling(null, system);
this.system = system;
}
static void println(String msg) {
LOG.info(msg);
}
static void println(String text, @Nullable Double percent, AbstractTimeProfiling phaseProfiling) {
StringBuilder sb = new StringBuilder();
sb.append(StringUtils.rightPad(text, TEXT_RIGHT_PAD)).append(StringUtils.leftPad(phaseProfiling.totalTimeAsString(), TIME_LEFT_PAD));
if (percent != null) {
sb.append(" (").append((int) (phaseProfiling.totalTime() / percent)).append("%)");
}
println(sb.toString());
}
@Override
public void onProjectAnalysis(ProjectAnalysisEvent event) {
Project module = event.getProject();
if (event.isStart()) {
currentModuleProfiling = new ModuleProfiling(module, system);
} else {
currentModuleProfiling.stop();
modulesProfilings.put(module, currentModuleProfiling);
long moduleTotalTime = currentModuleProfiling.totalTime();
println("");
println(" -------- Profiling of module " + module.getName() + ": " + TimeUtils.formatDuration(moduleTotalTime) + " --------");
println("");
Properties props = new Properties();
currentModuleProfiling.dump(props);
println("");
println(" -------- End of profiling of module " + module.getName() + " --------");
println("");
String fileName = module.getKey() + "-profiler.properties";
dumpToFile(props, ScannerUtils.cleanKeyForFilename(fileName));
totalProfiling.merge(currentModuleProfiling);
if (module.getParent() == null && !module.getModules().isEmpty()) {
dumpTotalExecutionSummary();
}
}
}
private void dumpTotalExecutionSummary() {
totalProfiling.stop();
long totalTime = totalProfiling.totalTime();
println("");
println(" ======== Profiling of total execution: " + TimeUtils.formatDuration(totalTime) + " ========");
println("");
println(" * Module execution time breakdown: ");
double percent = totalTime / 100.0;
for (ModuleProfiling modulesProfiling : truncate(sortByDescendingTotalTime(modulesProfilings).values())) {
println(" o " + modulesProfiling.moduleName() + " execution time: ", percent, modulesProfiling);
}
println("");
Properties props = new Properties();
totalProfiling.dump(props);
println("");
println(" ======== End of profiling of total execution ========");
println("");
String fileName = "total-execution-profiler.properties";
dumpToFile(props, fileName);
}
private void dumpToFile(Properties props, String fileName) {
File file = new File(out, fileName);
try (FileOutputStream fos = new FileOutputStream(file)) {
props.store(fos, "SonarQube");
println("Profiling data stored in " + file.getAbsolutePath());
} catch (Exception e) {
throw new IllegalStateException("Unable to store profiler output: " + file, e);
}
}
@Override
public void onSensorsPhase(SensorsPhaseEvent event) {
if (event.isStart()) {
currentModuleProfiling.addPhaseProfiling(Phase.SENSOR);
} else {
currentModuleProfiling.getProfilingPerPhase(Phase.SENSOR).stop();
}
}
@Override
public void onSensorExecution(SensorExecutionEvent event) {
PhaseProfiling profiling = currentModuleProfiling.getProfilingPerPhase(Phase.SENSOR);
if (event.isStart()) {
profiling.newItemProfiling(event.getSensor());
} else {
profiling.getProfilingPerItem(event.getSensor()).stop();
}
}
@Override
public void onPostJobsPhase(PostJobsPhaseEvent event) {
if (event.isStart()) {
currentModuleProfiling.addPhaseProfiling(Phase.POSTJOB);
} else {
currentModuleProfiling.getProfilingPerPhase(Phase.POSTJOB).stop();
}
}
@Override
public void onPostJobExecution(PostJobExecutionEvent event) {
PhaseProfiling profiling = currentModuleProfiling.getProfilingPerPhase(Phase.POSTJOB);
if (event.isStart()) {
profiling.newItemProfiling(event.getPostJob());
} else {
profiling.getProfilingPerItem(event.getPostJob()).stop();
}
}
@Override
public void onInitializersPhase(InitializersPhaseEvent event) {
if (event.isStart()) {
currentModuleProfiling.addPhaseProfiling(Phase.INIT);
} else {
currentModuleProfiling.getProfilingPerPhase(Phase.INIT).stop();
}
}
@Override
public void onInitializerExecution(InitializerExecutionEvent event) {
PhaseProfiling profiling = currentModuleProfiling.getProfilingPerPhase(Phase.INIT);
if (event.isStart()) {
profiling.newItemProfiling(event.getInitializer());
} else {
profiling.getProfilingPerItem(event.getInitializer()).stop();
}
}
@Override
public void onBatchStep(BatchStepEvent event) {
if (event.isStart()) {
currentModuleProfiling.addBatchStepProfiling(event.stepName());
} else {
currentModuleProfiling.getProfilingPerBatchStep(event.stepName()).stop();
}
}
}
|