package org.sonar.ce.task.projectanalysis.purge;
import java.util.Set;
+import org.sonar.api.CoreProperties;
import org.sonar.api.ce.ComputeEngineSide;
import org.sonar.api.config.Configuration;
import org.sonar.api.server.ServerSide;
+import org.sonar.api.utils.TimeUtils;
+import org.sonar.api.utils.log.Logger;
+import org.sonar.api.utils.log.Loggers;
import org.sonar.db.DbSession;
import org.sonar.db.purge.PurgeConfiguration;
import org.sonar.db.purge.PurgeDao;
@ServerSide
@ComputeEngineSide
public class ProjectCleaner {
+ private static final Logger LOG = Loggers.get(ProjectCleaner.class);
+
private final PurgeProfiler profiler;
private final PurgeListener purgeListener;
private final PurgeDao purgeDao;
}
public ProjectCleaner purge(DbSession session, String rootUuid, String projectUuid, Configuration projectConfig, Set<String> disabledComponentUuids) {
+ long start = System.currentTimeMillis();
profiler.reset();
periodCleaner.clean(session, rootUuid, projectConfig);
purgeDao.purge(session, configuration, purgeListener, profiler);
session.commit();
+ logProfiling(start, projectConfig);
return this;
}
+
+ private void logProfiling(long start, Configuration config) {
+ if (config.getBoolean(CoreProperties.PROFILING_LOG_PROPERTY).orElse(false)) {
+ long duration = System.currentTimeMillis() - start;
+ LOG.info("");
+ LOG.info(" -------- Profiling for purge: " + TimeUtils.formatDuration(duration) + " --------");
+ LOG.info("");
+ profiler.dump(duration, LOG);
+ LOG.info("");
+ LOG.info(" -------- End of profiling for purge --------");
+ LOG.info("");
+ }
+ }
}
import org.junit.Before;
import org.junit.Test;
+import org.sonar.api.CoreProperties;
import org.sonar.api.config.PropertyDefinitions;
import org.sonar.api.config.internal.MapSettings;
import org.sonar.api.utils.System2;
import static java.util.Collections.emptySet;
import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
public class ProjectCleanerTest {
verify(periodCleaner).clean(any(), any(), any());
verify(dao).purge(any(), any(), any(), any());
}
+
+ @Test
+ public void no_profiling_when_property_is_false() {
+ settings.setProperty(CoreProperties.PROFILING_LOG_PROPERTY, false);
+
+ underTest.purge(mock(DbSession.class), "root", "project", settings.asConfig(), emptySet());
+
+ verify(profiler, never()).dump(anyLong(), any());
+ }
+
+ @Test
+ public void profiling_when_property_is_true() {
+ settings.setProperty(CoreProperties.PROFILING_LOG_PROPERTY, true);
+
+ underTest.purge(mock(DbSession.class), "root", "project", settings.asConfig(), emptySet());
+
+ verify(profiler).dump(anyLong(), any());
+ }
}