diff options
author | Julien HENRY <julien.henry@sonarsource.com> | 2014-10-28 11:20:38 +0100 |
---|---|---|
committer | Julien HENRY <julien.henry@sonarsource.com> | 2014-10-28 11:24:57 +0100 |
commit | bd080168083ccf35d75bbb0f21a29ede8a7ca549 (patch) | |
tree | a131adda8817d718664aff9478d79be9d3b8a045 /sonar-batch | |
parent | 32d7cae2a648fa9e1743c4e71fbd00cbe0c8be61 (diff) | |
download | sonarqube-bd080168083ccf35d75bbb0f21a29ede8a7ca549.tar.gz sonarqube-bd080168083ccf35d75bbb0f21a29ede8a7ca549.zip |
Dump profiling results in properties files in order to ease later comparison
Diffstat (limited to 'sonar-batch')
4 files changed, 50 insertions, 10 deletions
diff --git a/sonar-batch/src/main/java/org/sonar/batch/profiling/ModuleProfiling.java b/sonar-batch/src/main/java/org/sonar/batch/profiling/ModuleProfiling.java index af2d888c854..c1ac193cf28 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/profiling/ModuleProfiling.java +++ b/sonar-batch/src/main/java/org/sonar/batch/profiling/ModuleProfiling.java @@ -31,6 +31,7 @@ import java.util.HashMap; import java.util.LinkedHashMap; import java.util.Map; import java.util.Map.Entry; +import java.util.Properties; public class ModuleProfiling extends AbstractTimeProfiling { @@ -66,12 +67,16 @@ public class ModuleProfiling extends AbstractTimeProfiling { profilingPerBatchStep.put(stepName, new ItemProfiling(system(), stepName)); } - public void dump() { + public void dump(Properties props) { double percent = this.totalTime() / 100.0; Map<Object, AbstractTimeProfiling> categories = Maps.newLinkedHashMap(); categories.putAll(profilingPerPhase); categories.putAll(profilingPerBatchStep); + for (Map.Entry<Object, AbstractTimeProfiling> batchStep : categories.entrySet()) { + props.setProperty(batchStep.getKey().toString(), "" + batchStep.getValue().totalTime()); + } + for (Map.Entry<Object, AbstractTimeProfiling> batchStep : sortByDescendingTotalTime(categories).entrySet()) { println(" * " + batchStep.getKey() + " execution time: ", percent, batchStep.getValue()); } diff --git a/sonar-batch/src/main/java/org/sonar/batch/profiling/PhasesSumUpTimeProfiler.java b/sonar-batch/src/main/java/org/sonar/batch/profiling/PhasesSumUpTimeProfiler.java index fed134857cc..da7ec7818e6 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/profiling/PhasesSumUpTimeProfiler.java +++ b/sonar-batch/src/main/java/org/sonar/batch/profiling/PhasesSumUpTimeProfiler.java @@ -21,6 +21,7 @@ package org.sonar.batch.profiling; import com.google.common.annotations.VisibleForTesting; import com.google.common.collect.Lists; +import com.google.common.io.Closeables; import org.apache.commons.lang.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -37,6 +38,7 @@ 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.TempFolder; import org.sonar.api.utils.TimeUtils; import org.sonar.batch.events.BatchStepHandler; import org.sonar.batch.phases.Phases; @@ -45,10 +47,13 @@ import org.sonar.batch.phases.event.PersistersPhaseHandler; import javax.annotation.Nullable; +import java.io.File; +import java.io.FileOutputStream; import java.util.HashMap; import java.util.IdentityHashMap; import java.util.List; import java.util.Map; +import java.util.Properties; import static org.sonar.batch.profiling.AbstractTimeProfiling.sortByDescendingTotalTime; import static org.sonar.batch.profiling.AbstractTimeProfiling.truncate; @@ -71,6 +76,7 @@ public class PhasesSumUpTimeProfiler implements ProjectAnalysisHandler, SensorEx private DecoratorsProfiler decoratorsProfiler; private final System2 system; + private final File out; static void println(String msg) { LOG.info(msg); @@ -85,7 +91,8 @@ public class PhasesSumUpTimeProfiler implements ProjectAnalysisHandler, SensorEx println(sb.toString()); } - public PhasesSumUpTimeProfiler(System2 system) { + public PhasesSumUpTimeProfiler(System2 system, TempFolder tempFolder) { + this.out = tempFolder.newDir("profiling"); this.totalProfiling = new ModuleProfiling(null, system); this.system = system; } @@ -103,10 +110,13 @@ public class PhasesSumUpTimeProfiler implements ProjectAnalysisHandler, SensorEx println(""); println(" -------- Profiling of module " + module.getName() + ": " + TimeUtils.formatDuration(moduleTotalTime) + " --------"); println(""); - currentModuleProfiling.dump(); + Properties props = new Properties(); + currentModuleProfiling.dump(props); println(""); println(" -------- End of profiling of module " + module.getName() + " --------"); println(""); + String fileName = module.getKey() + "-profiler.xml"; + dumpToFile(props, fileName); totalProfiling.merge(currentModuleProfiling); if (module.isRoot() && !module.getModules().isEmpty()) { dumpTotalExecutionSummary(); @@ -126,10 +136,27 @@ public class PhasesSumUpTimeProfiler implements ProjectAnalysisHandler, SensorEx println(" o " + modulesProfiling.moduleName() + " execution time: ", percent, modulesProfiling); } println(""); - totalProfiling.dump(); + Properties props = new Properties(); + totalProfiling.dump(props); println(""); println(" ======== End of profiling of total execution ========"); println(""); + String fileName = "total-execution-profiler.xml"; + dumpToFile(props, fileName); + } + + private void dumpToFile(Properties props, String fileName) { + FileOutputStream fos = null; + try { + File file = new File(out, fileName); + fos = new FileOutputStream(file); + props.storeToXML(fos, "SonarQube"); + println("Results stored in " + file.getAbsolutePath()); + } catch (Exception e) { + throw new IllegalStateException("Unable to store profiler output", e); + } finally { + Closeables.closeQuietly(fos); + } } @Override diff --git a/sonar-batch/src/test/java/org/sonar/batch/profiling/PhasesSumUpTimeProfilerTest.java b/sonar-batch/src/test/java/org/sonar/batch/profiling/PhasesSumUpTimeProfilerTest.java index ad19f8fd1c9..67a25b2c439 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/profiling/PhasesSumUpTimeProfilerTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/profiling/PhasesSumUpTimeProfilerTest.java @@ -20,7 +20,9 @@ package org.sonar.batch.profiling; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.TemporaryFolder; import org.sonar.api.batch.Decorator; import org.sonar.api.batch.DecoratorContext; import org.sonar.api.batch.Initializer; @@ -43,29 +45,34 @@ import org.sonar.api.batch.events.SensorsPhaseHandler.SensorsPhaseEvent; import org.sonar.api.resources.Project; import org.sonar.api.resources.Resource; import org.sonar.api.utils.System2; +import org.sonar.api.utils.internal.DefaultTempFolder; import org.sonar.batch.events.BatchStepEvent; import org.sonar.batch.index.ScanPersister; import org.sonar.batch.phases.Phases.Phase; import org.sonar.batch.phases.event.PersisterExecutionHandler; import org.sonar.batch.phases.event.PersistersPhaseHandler; +import java.io.IOException; import java.util.Arrays; import java.util.Collections; import java.util.List; import static org.fest.assertions.Assertions.assertThat; -import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.spy; import static org.mockito.Mockito.when; public class PhasesSumUpTimeProfilerTest { + @Rule + public TemporaryFolder temp = new TemporaryFolder(); + private MockedSystem clock; private PhasesSumUpTimeProfiler profiler; @Before - public void prepare() { + public void prepare() throws IOException { clock = new MockedSystem(); - profiler = new PhasesSumUpTimeProfiler(clock); + profiler = new PhasesSumUpTimeProfiler(clock, new DefaultTempFolder(temp.newFolder())); } @Test @@ -146,7 +153,7 @@ public class PhasesSumUpTimeProfilerTest { } private Project mockProject(String name, boolean isRoot) { - final Project project = mock(Project.class); + final Project project = spy(new Project("myProject")); when(project.isRoot()).thenReturn(isRoot); when(project.getName()).thenReturn(name); return project; diff --git a/sonar-batch/src/test/java/org/sonar/batch/scan/ProjectScanContainerTest.java b/sonar-batch/src/test/java/org/sonar/batch/scan/ProjectScanContainerTest.java index a66f7f1378f..057998e6a11 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/scan/ProjectScanContainerTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/scan/ProjectScanContainerTest.java @@ -33,6 +33,7 @@ import org.sonar.api.config.Settings; import org.sonar.api.platform.ComponentContainer; import org.sonar.api.task.TaskExtension; import org.sonar.api.utils.System2; +import org.sonar.api.utils.TempFolder; import org.sonar.batch.bootstrap.AnalysisMode; import org.sonar.batch.bootstrap.BootstrapProperties; import org.sonar.batch.bootstrap.ExtensionInstaller; @@ -103,7 +104,7 @@ public class ProjectScanContainerTest { @Test public void should_activate_profiling() { - container.add(mock(ExtensionInstaller.class), projectBootstrapper); + container.add(mock(ExtensionInstaller.class), projectBootstrapper, mock(TempFolder.class)); container.doBeforeStart(); assertThat(container.getComponentsByType(PhasesSumUpTimeProfiler.class)).hasSize(0); @@ -111,7 +112,7 @@ public class ProjectScanContainerTest { settings.setProperty(CoreProperties.PROFILING_LOG_PROPERTY, "true"); container = new ProjectScanContainer(parentContainer); - container.add(mock(ExtensionInstaller.class), projectBootstrapper); + container.add(mock(ExtensionInstaller.class), projectBootstrapper, mock(TempFolder.class)); container.doBeforeStart(); assertThat(container.getComponentsByType(PhasesSumUpTimeProfiler.class)).hasSize(1); |