2 * SonarQube, open source software quality management tool.
3 * Copyright (C) 2008-2014 SonarSource
4 * mailto:contact AT sonarsource DOT com
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.
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.
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.
21 package org.sonar.server.benchmark;
23 import com.google.common.collect.Maps;
24 import org.apache.commons.io.FileUtils;
25 import org.apache.commons.lang.StringUtils;
26 import org.junit.ClassRule;
27 import org.junit.Rule;
28 import org.junit.Test;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31 import org.sonar.server.source.index.SourceLineDoc;
32 import org.sonar.server.source.index.SourceLineIndex;
33 import org.sonar.server.source.index.SourceLineIndexer;
34 import org.sonar.server.source.index.SourceLineResultSetIterator;
35 import org.sonar.server.tester.ServerTester;
37 import java.util.Iterator;
38 import java.util.List;
39 import java.util.Timer;
40 import java.util.concurrent.atomic.AtomicLong;
42 import static org.fest.assertions.Assertions.assertThat;
44 public class SourceIndexBenchmarkTest {
46 private static final Logger LOGGER = LoggerFactory.getLogger("benchmarkSources");
48 final static long FILES = 10000L;
49 static final int LINES_PER_FILE = 200;
52 public ServerTester tester = new ServerTester();
55 public Benchmark benchmark = new Benchmark();
58 public void benchmark() throws Exception {
62 // execute some queries
66 private void benchmarkIndexing() {
67 LOGGER.info("Indexing source lines");
69 SourceIterator files = new SourceIterator(FILES, LINES_PER_FILE);
70 ProgressTask progressTask = new ProgressTask(LOGGER, "files of " + LINES_PER_FILE + " lines", files.count());
71 Timer timer = new Timer("SourceIndexer");
72 timer.schedule(progressTask, ProgressTask.PERIOD_MS, ProgressTask.PERIOD_MS);
74 long start = System.currentTimeMillis();
75 tester.get(SourceLineIndexer.class).index(files);
76 long end = System.currentTimeMillis();
79 long period = end - start;
80 long nbLines = files.count.get() * LINES_PER_FILE;
81 LOGGER.info(String.format("%d lines indexed in %d ms (%d docs/second)", nbLines, period, nbLines / period));
83 long dirSize = FileUtils.sizeOfDirectory(tester.getEsServerHolder().getHomeDir());
84 LOGGER.info(String.format("ES dir: " + FileUtils.byteCountToDisplaySize(dirSize)));
85 benchmark.expectBetween("ES dir size (b)", dirSize, 73L * FileUtils.ONE_MB, 80L * FileUtils.ONE_MB);
88 private void benchmarkQueries() {
89 SourceLineIndex index = tester.get(SourceLineIndex.class);
90 for (int i = 1; i <= 100; i++) {
91 long start = System.currentTimeMillis();
92 List<SourceLineDoc> result = index.getLines("FILE" + i, 20, 150);
93 long end = System.currentTimeMillis();
94 assertThat(result).hasSize(131);
95 LOGGER.info("Request: {} docs in {} ms", result.size(), end - start);
99 private static class SourceIterator implements Iterator<SourceLineResultSetIterator.SourceFile> {
100 private final long nbFiles;
101 private final int nbLinesPerFile;
102 private int currentProject = 0;
103 private AtomicLong count = new AtomicLong(0L);
105 SourceIterator(long nbFiles, int nbLinesPerFile) {
106 this.nbFiles = nbFiles;
107 this.nbLinesPerFile = nbLinesPerFile;
110 public AtomicLong count() {
115 public boolean hasNext() {
116 return count.get() < nbFiles;
120 public SourceLineResultSetIterator.SourceFile next() {
121 String fileUuid = "FILE" + count.get();
122 SourceLineResultSetIterator.SourceFile file = new SourceLineResultSetIterator.SourceFile(fileUuid, System.currentTimeMillis());
123 for (int indexLine = 1; indexLine <= nbLinesPerFile; indexLine++) {
124 SourceLineDoc line = new SourceLineDoc(Maps.<String, Object>newHashMap());
125 line.setFileUuid(fileUuid);
126 line.setLine(indexLine);
127 line.setHighlighting(StringUtils.repeat("HIGHLIGHTING", 5));
128 line.setItConditions(4);
129 line.setItCoveredConditions(2);
130 line.setItLineHits(2);
131 line.setOverallConditions(8);
132 line.setOverallCoveredConditions(2);
133 line.setOverallLineHits(2);
134 line.setUtConditions(8);
135 line.setUtCoveredConditions(2);
136 line.setUtLineHits(2);
137 line.setProjectUuid("PROJECT" + currentProject);
138 line.setScmAuthor("a_guy");
139 line.setScmRevision("ABCDEFGHIJKL");
140 line.setSource(StringUtils.repeat("SOURCE", 10));
143 count.incrementAndGet();
144 if (count.get() % 500 == 0) {
151 public void remove() {
152 throw new UnsupportedOperationException();