You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SourceDbBenchmarkTest.java 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * SonarQube, open source software quality management tool.
  3. * Copyright (C) 2008-2014 SonarSource
  4. * mailto:contact AT sonarsource DOT com
  5. *
  6. * SonarQube is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * SonarQube is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.server.benchmark;
  21. import org.apache.commons.dbutils.DbUtils;
  22. import org.apache.commons.io.IOUtils;
  23. import org.junit.Rule;
  24. import org.junit.Test;
  25. import org.slf4j.Logger;
  26. import org.slf4j.LoggerFactory;
  27. import org.sonar.api.utils.internal.Uuids;
  28. import org.sonar.db.DbTester;
  29. import org.sonar.db.source.FileSourceDto;
  30. import org.sonar.server.db.DbClient;
  31. import org.sonar.server.source.db.FileSourceDao;
  32. import org.sonar.server.source.db.FileSourceDb;
  33. import org.sonar.server.source.index.FileSourcesUpdaterHelper;
  34. import org.sonar.server.source.index.SourceLineResultSetIterator;
  35. import java.io.IOException;
  36. import java.sql.Connection;
  37. import java.sql.SQLException;
  38. import java.util.Arrays;
  39. import java.util.Timer;
  40. import java.util.concurrent.atomic.AtomicLong;
  41. import static org.assertj.core.api.Assertions.assertThat;
  42. public class SourceDbBenchmarkTest {
  43. public static final Logger LOGGER = LoggerFactory.getLogger("benchmarkSourceDbScrolling");
  44. public static final int NUMBER_OF_FILES = 1000;
  45. public static final int NUMBER_OF_LINES = 3220;
  46. public static final String PROJECT_UUID = Uuids.create();
  47. @Rule
  48. public DbTester dbTester = new DbTester();
  49. @Rule
  50. public Benchmark benchmark = new Benchmark();
  51. @Test
  52. public void benchmark() throws Exception {
  53. prepareTable();
  54. scrollRows();
  55. }
  56. private void scrollRows() throws SQLException {
  57. LOGGER.info("Scroll table FILE_SOURCES");
  58. DbClient dbClient = new DbClient(dbTester.database(), dbTester.myBatis());
  59. Connection connection = dbTester.openConnection();
  60. AtomicLong counter = new AtomicLong();
  61. ProgressTask progress = new ProgressTask(LOGGER, "source file", counter);
  62. Timer timer = new Timer("SourceDbScroll");
  63. timer.schedule(progress, ProgressTask.PERIOD_MS, ProgressTask.PERIOD_MS);
  64. try {
  65. long start = System.currentTimeMillis();
  66. SourceLineResultSetIterator it = SourceLineResultSetIterator.create(dbClient, connection, 0L, null);
  67. while (it.hasNext()) {
  68. FileSourcesUpdaterHelper.Row row = it.next();
  69. assertThat(row.getUpdateRequests().size()).isEqualTo(NUMBER_OF_LINES);
  70. assertThat(row.getFileUuid()).isNotEmpty();
  71. counter.incrementAndGet();
  72. }
  73. long end = System.currentTimeMillis();
  74. long period = end - start;
  75. long throughputPerSecond = 1000L * counter.get() / period;
  76. LOGGER.info(String.format("%d FILE_SOURCES rows scrolled in %d ms (%d rows/second)", counter.get(), period, throughputPerSecond));
  77. benchmark.expectBetween("Throughput to scroll FILE_SOURCES", throughputPerSecond, 9, 13);
  78. } finally {
  79. DbUtils.closeQuietly(connection);
  80. timer.cancel();
  81. }
  82. }
  83. private void prepareTable() throws IOException {
  84. LOGGER.info("Populate table FILE_SOURCES");
  85. FileSourceDao dao = new FileSourceDao(dbTester.myBatis());
  86. for (int i = 0; i < NUMBER_OF_FILES; i++) {
  87. dao.insert(generateDto());
  88. }
  89. }
  90. private FileSourceDto generateDto() throws IOException {
  91. long now = System.currentTimeMillis();
  92. byte[] data = generateData();
  93. FileSourceDto dto = new FileSourceDto();
  94. dto.setCreatedAt(now);
  95. dto.setUpdatedAt(now);
  96. dto.setBinaryData(data);
  97. dto.setDataHash("49d7230271f2bd24c759e54bcd66547d");
  98. dto.setProjectUuid(PROJECT_UUID);
  99. dto.setFileUuid(Uuids.create());
  100. dto.setLineHashes(IOUtils.toString(getClass().getResourceAsStream("SourceDbBenchmarkTest/line_hashes.txt")));
  101. dto.setDataType(FileSourceDto.Type.SOURCE);
  102. return dto;
  103. }
  104. private byte[] generateData() {
  105. FileSourceDb.Data.Builder dataBuilder = FileSourceDb.Data.newBuilder();
  106. FileSourceDb.Line.Builder lineBuilder = FileSourceDb.Line.newBuilder();
  107. for (int i = 1; i <= NUMBER_OF_LINES; i++) {
  108. lineBuilder.clear();
  109. dataBuilder.addLines(lineBuilder
  110. .setLine(i)
  111. .setScmRevision("REVISION_" + i)
  112. .setScmAuthor("a_guy")
  113. .setSource("this is not java code " + i)
  114. .setUtLineHits(i)
  115. .setUtConditions(i + 1)
  116. .setUtCoveredConditions(i)
  117. .setItLineHits(i)
  118. .setItConditions(i + 1)
  119. .setItCoveredConditions(i)
  120. .setOverallLineHits(i)
  121. .setOverallConditions(i + 1)
  122. .setOverallCoveredConditions(i)
  123. .setScmDate(1_500_000_000_000L)
  124. .setHighlighting("2,9,k;9,18,k")
  125. .addAllDuplication(Arrays.asList(19, 33, 141))
  126. .build());
  127. }
  128. return FileSourceDto.encodeSourceData(dataBuilder.build());
  129. }
  130. }