Skip to content

Instantly share code, notes, and snippets.

@mocobeta
Created December 20, 2020 04:03
Show Gist options
  • Save mocobeta/6ac6badd10a8d5b5278869b10e8cae63 to your computer and use it in GitHub Desktop.
Save mocobeta/6ac6badd10a8d5b5278869b10e8cae63 to your computer and use it in GitHub Desktop.
@State(Scope.Benchmark)
public class SearchBenchmark {
private static final String dirPath = System.getProperty("index.dir");
private static final String[] terms1 = new String[]{"電車", "列車", "鉄道"};
private Directory dir;
private IndexReader reader;
private Query query1;
@Param({"10"})
private int numHits;
@Setup
public void setUp() throws IOException {
this.dir = FSDirectory.open(Paths.get(dirPath));
this.reader = DirectoryReader.open(dir);
this.query1 = buildShouldQuery(terms1);
}
private Query buildShouldQuery(String[] terms) {
BooleanQuery.Builder builder = new BooleanQuery.Builder();
for (String term : terms) {
builder.add(new TermQuery(new Term("text", term)), BooleanClause.Occur.SHOULD);
}
return builder.build();
}
@Benchmark
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
@Warmup(iterations = 5, time = 1)
@Measurement(iterations = 5, time = 1)
public void measureSearchTimeWAND(final Blackhole bh) throws IOException {
IndexSearcher searcher = new IndexSearcher(reader);
TopDocs hits = searcher.search(query1, numHits);
bh.consume(hits);
}
@Benchmark
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
@Warmup(iterations = 5, time = 1)
@Measurement(iterations = 5, time = 1)
public void measureSearchTime(final Blackhole bh) throws IOException {
IndexSearcher searcher = new IndexSearcher(reader);
TopScoreDocCollector collector = TopScoreDocCollector.create(numHits, Integer.MAX_VALUE);
searcher.search(query1, collector);
TopDocs hits = collector.topDocs();
bh.consume(hits);
}
public static void main(String[] args) throws Exception {
Options opt = new OptionsBuilder()
.include(SearchBenchmark.class.getSimpleName())
.forks(1)
.build();
new Runner(opt).run();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment