diff options
author | Jean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com> | 2013-12-03 17:19:30 +0100 |
---|---|---|
committer | Jean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com> | 2013-12-03 17:20:57 +0100 |
commit | 4dbc99f65e51724e5f31f166f4ead50459a0fd64 (patch) | |
tree | 203a946dd6b4efbaf7ea19e88b6dfcb53114e229 | |
parent | a6424d7238c9f9894fe18bf38ba81cbce7354b48 (diff) | |
download | sonarqube-4dbc99f65e51724e5f31f166f4ead50459a0fd64.tar.gz sonarqube-4dbc99f65e51724e5f31f166f4ead50459a0fd64.zip |
SONAR-4756 Use platform profiling in preview DB export
3 files changed, 36 insertions, 30 deletions
diff --git a/sonar-core/src/main/java/org/sonar/core/persistence/DbTemplate.java b/sonar-core/src/main/java/org/sonar/core/persistence/DbTemplate.java index 98a74196af4..9d101018ca2 100644 --- a/sonar-core/src/main/java/org/sonar/core/persistence/DbTemplate.java +++ b/sonar-core/src/main/java/org/sonar/core/persistence/DbTemplate.java @@ -27,21 +27,24 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.sonar.api.ServerComponent; import org.sonar.api.utils.SonarException; +import org.sonar.core.profiling.Profiling; +import org.sonar.core.profiling.Profiling.Level; +import org.sonar.core.profiling.StopWatch; import javax.annotation.Nullable; import javax.sql.DataSource; -import java.sql.Connection; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.sql.Statement; -import java.sql.Timestamp; -import java.sql.Types; +import java.sql.*; public class DbTemplate implements ServerComponent { private static final Logger LOG = LoggerFactory.getLogger(DbTemplate.class); + private Profiling profiling; + + public DbTemplate(Profiling profiling) { + this.profiling = profiling; + } + public DbTemplate copyTable(DataSource source, DataSource dest, String table) { return copyTableColumns(source, dest, table, null); } @@ -59,7 +62,7 @@ public class DbTemplate implements ServerComponent { public DbTemplate copyTableColumns(DataSource source, DataSource dest, String table, String selectQuery, @Nullable String[] columnNames) { LOG.debug("Copy table {}", table); - long startup = System.currentTimeMillis(); + StopWatch watch = profiling.start("previewdb", Level.BASIC); truncate(dest, table); @@ -103,13 +106,11 @@ public class DbTemplate implements ServerComponent { destStatement.executeBatch(); destConnection.commit(); } - if (LOG.isDebugEnabled()) { - LOG.debug(" " + count + " rows of " + table + " copied in " + (System.currentTimeMillis() - startup) + " ms"); - } } catch (SQLException e) { LOG.error("Fail to copy table " + table, e); throw new IllegalStateException("Fail to copy table " + table, e); } finally { + watch.stop(" " + count + " rows of " + table + " copied"); DbUtils.closeQuietly(destStatement); DbUtils.closeQuietly(destResultSet); DbUtils.closeQuietly(destConnection); diff --git a/sonar-core/src/main/java/org/sonar/core/persistence/PreviewDatabaseFactory.java b/sonar-core/src/main/java/org/sonar/core/persistence/PreviewDatabaseFactory.java index e362da12e9e..5fcd506d018 100644 --- a/sonar-core/src/main/java/org/sonar/core/persistence/PreviewDatabaseFactory.java +++ b/sonar-core/src/main/java/org/sonar/core/persistence/PreviewDatabaseFactory.java @@ -20,11 +20,12 @@ package org.sonar.core.persistence; import org.apache.commons.dbcp.BasicDataSource; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import org.sonar.api.ServerComponent; import org.sonar.api.issue.Issue; import org.sonar.api.utils.SonarException; +import org.sonar.core.profiling.Profiling; +import org.sonar.core.profiling.Profiling.Level; +import org.sonar.core.profiling.StopWatch; import org.sonar.core.source.SnapshotDataTypes; import javax.annotation.Nullable; @@ -34,7 +35,6 @@ import java.io.File; import java.sql.SQLException; public class PreviewDatabaseFactory implements ServerComponent { - private static final Logger LOG = LoggerFactory.getLogger(PreviewDatabaseFactory.class); private static final String DIALECT = "h2"; private static final String DRIVER = "org.h2.Driver"; private static final String URL = "jdbc:h2:"; @@ -44,13 +44,15 @@ public class PreviewDatabaseFactory implements ServerComponent { private static final String PASSWORD = SONAR; private final Database database; + private final Profiling profiling; - public PreviewDatabaseFactory(Database database) { + public PreviewDatabaseFactory(Database database, Profiling profiling) { this.database = database; + this.profiling = profiling; } public File createNewDatabaseForDryRun(Long projectId, File destFolder, String dbFileName) { - long startup = System.currentTimeMillis(); + StopWatch watch = profiling.start("previewdb", Level.BASIC); String h2Name = destFolder.getAbsolutePath() + File.separator + dbFileName; @@ -62,15 +64,16 @@ public class PreviewDatabaseFactory implements ServerComponent { close(destination); File dbFile = new File(h2Name + H2_FILE_SUFFIX); - if (LOG.isDebugEnabled()) { - long size = dbFile.length(); - long duration = System.currentTimeMillis() - startup; - if (projectId == null) { - LOG.debug("Preview Database created in " + duration + " ms, size is " + size + " bytes"); - } else { - LOG.debug("Preview Database for project " + projectId + " created in " + duration + " ms, size is " + size + " bytes"); - } + + long size = dbFile.length(); + String message = ""; + if (projectId == null) { + message = "Preview Database created, size is " + size + " bytes"; + } else { + message = "Preview Database for project " + projectId + " created, size is " + size + " bytes"; } + watch.stop(message); + return dbFile; } catch (SQLException e) { @@ -80,7 +83,7 @@ public class PreviewDatabaseFactory implements ServerComponent { } private void copy(DataSource source, DataSource dest, @Nullable Long projectId) { - DbTemplate template = new DbTemplate(); + DbTemplate template = new DbTemplate(profiling); template .copyTable(source, dest, "active_rules") .copyTable(source, dest, "active_rule_parameters") @@ -156,8 +159,8 @@ public class PreviewDatabaseFactory implements ServerComponent { } private BasicDataSource create(String dialect, String driver, String user, String password, String url) { - BasicDataSource dataSource = new DbTemplate().dataSource(driver, user, password, url); - new DbTemplate().createSchema(dataSource, dialect); + BasicDataSource dataSource = new DbTemplate(profiling).dataSource(driver, user, password, url); + new DbTemplate(profiling).createSchema(dataSource, dialect); return dataSource; } diff --git a/sonar-core/src/test/java/org/sonar/core/persistence/PreviewDatabaseFactoryTest.java b/sonar-core/src/test/java/org/sonar/core/persistence/PreviewDatabaseFactoryTest.java index 88daa9d24d5..a3e07801ac7 100644 --- a/sonar-core/src/test/java/org/sonar/core/persistence/PreviewDatabaseFactoryTest.java +++ b/sonar-core/src/test/java/org/sonar/core/persistence/PreviewDatabaseFactoryTest.java @@ -28,6 +28,8 @@ import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; +import org.sonar.api.config.Settings; +import org.sonar.core.profiling.Profiling; import javax.sql.DataSource; @@ -49,7 +51,7 @@ public class PreviewDatabaseFactoryTest extends AbstractDaoTestCase { @Before public void setUp() throws Exception { - localDatabaseFactory = new PreviewDatabaseFactory(getDatabase()); + localDatabaseFactory = new PreviewDatabaseFactory(getDatabase(), new Profiling(new Settings())); } @After @@ -154,11 +156,11 @@ public class PreviewDatabaseFactoryTest extends AbstractDaoTestCase { private BasicDataSource createDatabase(byte[] db) throws IOException { File file = temporaryFolder.newFile("db.h2.db"); Files.write(db, file); - return new DbTemplate().dataSource("org.h2.Driver", "sonar", "sonar", "jdbc:h2:" + file.getAbsolutePath().replaceAll(".h2.db", "")); + return new DbTemplate(new Profiling(new Settings())).dataSource("org.h2.Driver", "sonar", "sonar", "jdbc:h2:" + file.getAbsolutePath().replaceAll(".h2.db", "")); } private int rowCount(String table) { - return new DbTemplate().getRowCount(dataSource, table); + return new DbTemplate(new Profiling(new Settings())).getRowCount(dataSource, table); } public String getUserPassword(DataSource dataSource, int userId) { |