From: Duarte Meneses Date: Fri, 1 Jul 2016 15:46:24 +0000 (+0200) Subject: SONAR-7354 Display total analysis time in scanners X-Git-Tag: 6.0-RC1~188 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=49cbc42b322e562807add4faaebd649445ee1bce;p=sonarqube.git SONAR-7354 Display total analysis time in scanners --- diff --git a/sonar-scanner-engine/src/main/java/org/sonar/batch/bootstrap/GlobalContainer.java b/sonar-scanner-engine/src/main/java/org/sonar/batch/bootstrap/GlobalContainer.java index 0578a3b144d..606b3d9a29d 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/batch/bootstrap/GlobalContainer.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/batch/bootstrap/GlobalContainer.java @@ -21,10 +21,13 @@ package org.sonar.batch.bootstrap; import java.util.List; import java.util.Map; + import org.sonar.api.Plugin; import org.sonar.api.internal.SonarQubeVersionFactory; import org.sonar.api.utils.System2; import org.sonar.api.utils.UriReader; +import org.sonar.api.utils.log.Logger; +import org.sonar.api.utils.log.Loggers; import org.sonar.batch.index.CachesManager; import org.sonar.batch.platform.DefaultServer; import org.sonar.batch.repository.DefaultGlobalRepositoriesLoader; @@ -40,7 +43,7 @@ import org.sonar.core.util.DefaultHttpDownloader; import org.sonar.core.util.UuidFactoryImpl; public class GlobalContainer extends ComponentContainer { - + private static final Logger LOG = Loggers.get(GlobalContainer.class); private final Map bootstrapProperties; private GlobalContainer(Map bootstrapProperties) { @@ -103,7 +106,26 @@ public class GlobalContainer extends ComponentContainer { } public void executeTask(Map taskProperties, Object... components) { + long startTime = System.currentTimeMillis(); new TaskContainer(this, taskProperties, components).execute(); + + LOG.info("Task total time: {}", formatTime(System.currentTimeMillis() - startTime)); + } + + static String formatTime(long time) { + long h = time / (60 * 60 * 1000); + long m = (time - h * 60 * 60 * 1000) / (60 * 1000); + long s = (time - h * 60 * 60 * 1000 - m * 60 * 1000) / 1000; + long ms = time % 1000; + final String format; + if (h > 0) { + format = "%1$d:%2$02d:%3$02d.%4$03ds"; + } else if (m > 0) { + format = "%2$d:%3$02d.%4$03ds"; + } else { + format = "%3$d.%4$03ds"; + } + return String.format(format, h, m, s, ms); } } diff --git a/sonar-scanner-engine/src/test/java/org/sonar/batch/bootstrap/GlobalContainerTest.java b/sonar-scanner-engine/src/test/java/org/sonar/batch/bootstrap/GlobalContainerTest.java index 0019d8adc18..168ebd6a844 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/batch/bootstrap/GlobalContainerTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/batch/bootstrap/GlobalContainerTest.java @@ -63,6 +63,14 @@ public class GlobalContainerTest { assertThat(container.getComponentByType(Bar.class)).isNotNull(); } + @Test + public void shouldFormatTime() { + assertThat(GlobalContainer.formatTime(1 * 60 * 60 * 1000 + 2 * 60 * 1000 + 3 * 1000 + 400)).isEqualTo("1:02:03.400s"); + assertThat(GlobalContainer.formatTime(2 * 60 * 1000 + 3 * 1000 + 400)).isEqualTo("2:03.400s"); + assertThat(GlobalContainer.formatTime(3 * 1000 + 400)).isEqualTo("3.400s"); + assertThat(GlobalContainer.formatTime(400)).isEqualTo("0.400s"); + } + @BatchSide public static class Foo {