Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SONAR-10430 add timing logs + support for dump into temp csv file
dump is enabled by setting property "sonar.filemove.dumpCsv" to "true"
- Loading branch information
Showing
6 changed files
with
238 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
...in/java/org/sonar/server/computation/task/projectanalysis/filemove/ScoreMatrixDumper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/* | ||
* SonarQube | ||
* Copyright (C) 2009-2018 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.server.computation.task.projectanalysis.filemove; | ||
|
||
public interface ScoreMatrixDumper { | ||
void dumpAsCsv(ScoreMatrix scoreMatrix); | ||
} |
58 changes: 58 additions & 0 deletions
58
...ava/org/sonar/server/computation/task/projectanalysis/filemove/ScoreMatrixDumperImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* SonarQube | ||
* Copyright (C) 2009-2018 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.server.computation.task.projectanalysis.filemove; | ||
|
||
import java.io.BufferedWriter; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import org.sonar.api.config.Configuration; | ||
import org.sonar.api.utils.log.Logger; | ||
import org.sonar.api.utils.log.Loggers; | ||
import org.sonar.ce.queue.CeTask; | ||
|
||
import static java.nio.charset.StandardCharsets.UTF_8; | ||
|
||
public class ScoreMatrixDumperImpl implements ScoreMatrixDumper { | ||
private static final Logger LOG = Loggers.get(ScoreMatrixDumperImpl.class); | ||
|
||
private final Configuration configuration; | ||
private final CeTask ceTask; | ||
|
||
public ScoreMatrixDumperImpl(Configuration configuration, CeTask ceTask) { | ||
this.configuration = configuration; | ||
this.ceTask = ceTask; | ||
} | ||
|
||
@Override | ||
public void dumpAsCsv(ScoreMatrix scoreMatrix) { | ||
if (configuration.getBoolean("sonar.filemove.dumpCsv").orElse(false)) { | ||
try { | ||
Path tempFile = Files.createTempFile(String.format("score-matrix-%s", ceTask.getUuid()), ".csv"); | ||
try (BufferedWriter writer = Files.newBufferedWriter(tempFile, UTF_8)) { | ||
writer.write(scoreMatrix.toCsv(';')); | ||
} | ||
LOG.info("File move similarity score matrix dumped as CSV: {}", tempFile); | ||
} catch (IOException e) { | ||
LOG.error("Failed to dump ScoreMatrix as CSV", e); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
133 changes: 133 additions & 0 deletions
133
...org/sonar/server/computation/task/projectanalysis/filemove/ScoreMatrixDumperImplTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
/* | ||
* SonarQube | ||
* Copyright (C) 2009-2018 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.server.computation.task.projectanalysis.filemove; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
import com.google.common.collect.ImmutableMap; | ||
import com.google.common.collect.ImmutableSet; | ||
import com.tngtech.java.junit.dataprovider.DataProvider; | ||
import com.tngtech.java.junit.dataprovider.DataProviderRunner; | ||
import com.tngtech.java.junit.dataprovider.UseDataProvider; | ||
import java.io.File; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.Collection; | ||
import org.apache.commons.io.FileUtils; | ||
import org.apache.commons.io.filefilter.AbstractFileFilter; | ||
import org.junit.Before; | ||
import org.junit.BeforeClass; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.sonar.api.config.Configuration; | ||
import org.sonar.api.config.internal.MapSettings; | ||
import org.sonar.ce.queue.CeTask; | ||
|
||
import static org.apache.commons.lang.RandomStringUtils.randomAlphabetic; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
@RunWith(DataProviderRunner.class) | ||
public class ScoreMatrixDumperImplTest { | ||
private static final ScoreMatrix A_SCORE_MATRIX = new ScoreMatrix( | ||
ImmutableSet.of("A", "B"), | ||
ImmutableMap.of("1", new FileSimilarity.File("path_1", ImmutableList.of("foo", "bar"))), | ||
new int[][] {{10}, {2}}, | ||
10); | ||
private MapSettings settings = new MapSettings(); | ||
private Configuration configuration = settings.asConfig(); | ||
private CeTask ceTask = mock(CeTask.class); | ||
private ScoreMatrixDumper underTest = new ScoreMatrixDumperImpl(configuration, ceTask); | ||
private static Path tempDir; | ||
|
||
@BeforeClass | ||
public static void lookupTempDir() throws IOException { | ||
Path tempFile = Files.createTempFile("a", "b"); | ||
Files.delete(tempFile); | ||
tempDir = tempFile.getParent(); | ||
} | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
FileUtils.listFiles(tempDir.toFile(), new AbstractFileFilter() { | ||
@Override | ||
public boolean accept(File file) { | ||
if (file.getName().contains("score-matrix-")) { | ||
file.delete(); | ||
} | ||
return false; | ||
} | ||
}, null); | ||
} | ||
|
||
@Test | ||
public void dumpAsCsv_creates_csv_dump_of_score_matrix_if_property_is_true() throws IOException { | ||
String taskUuid = "acme"; | ||
when(ceTask.getUuid()).thenReturn(taskUuid); | ||
settings.setProperty("sonar.filemove.dumpCsv", "true"); | ||
|
||
underTest.dumpAsCsv(A_SCORE_MATRIX); | ||
|
||
Collection<File> files = listDumpFilesForTaskUuid(taskUuid); | ||
assertThat(files).hasSize(1); | ||
assertThat(files.iterator().next()).hasContent(A_SCORE_MATRIX.toCsv(';')); | ||
} | ||
|
||
@Test | ||
public void dumpAsCsv_has_no_effect_if_configuration_is_empty() throws IOException { | ||
String taskUuid = randomAlphabetic(6); | ||
when(ceTask.getUuid()).thenReturn(taskUuid); | ||
|
||
underTest.dumpAsCsv(A_SCORE_MATRIX); | ||
|
||
assertThat(listDumpFilesForTaskUuid(taskUuid)).isEmpty(); | ||
} | ||
|
||
@Test | ||
@UseDataProvider("notTruePropertyValues") | ||
public void dumpAsCsv_has_no_effect_if_property_is_not_true(String value) throws IOException { | ||
String taskUuid = randomAlphabetic(6); | ||
when(ceTask.getUuid()).thenReturn(taskUuid); | ||
settings.setProperty("sonar.filemove.dumpCsv", value); | ||
|
||
underTest.dumpAsCsv(A_SCORE_MATRIX); | ||
|
||
assertThat(listDumpFilesForTaskUuid(taskUuid)).isEmpty(); | ||
} | ||
|
||
@DataProvider | ||
public static Object[][] notTruePropertyValues() { | ||
return new Object[][] { | ||
{randomAlphabetic(6)}, | ||
{"false"}, | ||
}; | ||
} | ||
|
||
private static Collection<File> listDumpFilesForTaskUuid(String taskUuid) throws IOException { | ||
return FileUtils.listFiles(tempDir.toFile(), new AbstractFileFilter() { | ||
@Override | ||
public boolean accept(File file) { | ||
String name = file.getName(); | ||
return name.startsWith("score-matrix-" + taskUuid) && name.endsWith(".csv"); | ||
} | ||
}, null); | ||
} | ||
} |