|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.lucene.benchmark.jmh; |
| 19 | + |
| 20 | +import java.io.IOException; |
| 21 | +import java.util.concurrent.TimeUnit; |
| 22 | +import org.apache.lucene.document.Document; |
| 23 | +import org.apache.lucene.document.Field; |
| 24 | +import org.apache.lucene.document.TextField; |
| 25 | +import org.apache.lucene.index.DirectoryReader; |
| 26 | +import org.apache.lucene.index.IndexReader; |
| 27 | +import org.apache.lucene.index.IndexWriter; |
| 28 | +import org.apache.lucene.index.IndexWriterConfig; |
| 29 | +import org.apache.lucene.search.IndexSearcher; |
| 30 | +import org.apache.lucene.search.PhraseQuery; |
| 31 | +import org.apache.lucene.search.Sort; |
| 32 | +import org.apache.lucene.search.TopDocs; |
| 33 | +import org.apache.lucene.search.TopFieldCollectorManager; |
| 34 | +import org.apache.lucene.search.TopScoreDocCollectorManager; |
| 35 | +import org.apache.lucene.store.Directory; |
| 36 | +import org.apache.lucene.store.MMapDirectory; |
| 37 | +import org.openjdk.jmh.annotations.Benchmark; |
| 38 | +import org.openjdk.jmh.annotations.BenchmarkMode; |
| 39 | +import org.openjdk.jmh.annotations.Fork; |
| 40 | +import org.openjdk.jmh.annotations.Level; |
| 41 | +import org.openjdk.jmh.annotations.Measurement; |
| 42 | +import org.openjdk.jmh.annotations.Mode; |
| 43 | +import org.openjdk.jmh.annotations.OutputTimeUnit; |
| 44 | +import org.openjdk.jmh.annotations.Scope; |
| 45 | +import org.openjdk.jmh.annotations.Setup; |
| 46 | +import org.openjdk.jmh.annotations.State; |
| 47 | +import org.openjdk.jmh.annotations.TearDown; |
| 48 | +import org.openjdk.jmh.annotations.Warmup; |
| 49 | + |
| 50 | +@BenchmarkMode(Mode.Throughput) |
| 51 | +@OutputTimeUnit(TimeUnit.MILLISECONDS) |
| 52 | +@State(Scope.Benchmark) |
| 53 | +@Warmup(iterations = 5, time = 3) |
| 54 | +@Measurement(iterations = 10, time = 5) |
| 55 | +@Fork( |
| 56 | + value = 3, |
| 57 | + jvmArgsAppend = {"-Xmx2g", "-Xms2g"}) |
| 58 | +public class PhraseScorerBenchmark { |
| 59 | + |
| 60 | + private static final int NUM_HITS = 10; |
| 61 | + |
| 62 | + private Directory dir; |
| 63 | + private IndexReader reader; |
| 64 | + private IndexSearcher searcher; |
| 65 | + private PhraseQuery exactQuery; |
| 66 | + private PhraseQuery sloppyQuery; |
| 67 | + |
| 68 | + @Setup(Level.Trial) |
| 69 | + public void setUp() throws IOException { |
| 70 | + dir = new MMapDirectory(java.nio.file.Files.createTempDirectory("benchmark")); |
| 71 | + IndexWriterConfig config = new IndexWriterConfig(); |
| 72 | + try (IndexWriter writer = new IndexWriter(dir, config)) { |
| 73 | + // Create a corpus where most docs contain the individual query terms but only a small |
| 74 | + // fraction contain the actual phrase. This maximises the number of documents whose maxFreq |
| 75 | + // upper-bound check allows short-circuiting. |
| 76 | + for (int i = 0; i < 1_000_000; i++) { |
| 77 | + Document doc = new Document(); |
| 78 | + if (i % 1000 == 0) { |
| 79 | + // 0.1% of docs: exact phrase match |
| 80 | + doc.add( |
| 81 | + new TextField( |
| 82 | + "text", "the quick brown fox jumped over the lazy dog", Field.Store.NO)); |
| 83 | + } else if (i % 2 == 0) { |
| 84 | + // 50% of docs: terms present but not as a phrase (high freq, no match) |
| 85 | + StringBuilder sb = new StringBuilder("quick "); |
| 86 | + for (int j = 0; j < 100; j++) sb.append("padding "); |
| 87 | + sb.append("fox"); |
| 88 | + doc.add(new TextField("text", sb.toString(), Field.Store.NO)); |
| 89 | + } else { |
| 90 | + // 50% of docs: no query terms at all |
| 91 | + doc.add(new TextField("text", "unrelated words", Field.Store.NO)); |
| 92 | + } |
| 93 | + writer.addDocument(doc); |
| 94 | + } |
| 95 | + } |
| 96 | + reader = DirectoryReader.open(dir); |
| 97 | + searcher = new IndexSearcher(reader); |
| 98 | + exactQuery = new PhraseQuery("text", "quick", "brown", "fox"); |
| 99 | + sloppyQuery = new PhraseQuery(10, "text", "quick", "fox"); |
| 100 | + } |
| 101 | + |
| 102 | + @TearDown(Level.Trial) |
| 103 | + public void tearDown() throws IOException { |
| 104 | + reader.close(); |
| 105 | + dir.close(); |
| 106 | + } |
| 107 | + |
| 108 | + @Benchmark |
| 109 | + public TopDocs benchmarkExactTopScores() throws IOException { |
| 110 | + return searcher.search(exactQuery, 10); |
| 111 | + } |
| 112 | + |
| 113 | + @Benchmark |
| 114 | + public TopDocs benchmarkSloppyTopScores() throws IOException { |
| 115 | + return searcher.search(sloppyQuery, 10); |
| 116 | + } |
| 117 | + |
| 118 | + @Benchmark |
| 119 | + public TopDocs benchmarkExactComplete() throws IOException { |
| 120 | + return searcher.search( |
| 121 | + exactQuery, new TopScoreDocCollectorManager(NUM_HITS, Integer.MAX_VALUE)); |
| 122 | + } |
| 123 | + |
| 124 | + @Benchmark |
| 125 | + public TopDocs benchmarkExactCompleteNoScores() throws IOException { |
| 126 | + return searcher.search( |
| 127 | + exactQuery, new TopFieldCollectorManager(Sort.INDEXORDER, NUM_HITS, Integer.MAX_VALUE)); |
| 128 | + } |
| 129 | + |
| 130 | + @Benchmark |
| 131 | + public TopDocs benchmarkSloppyComplete() throws IOException { |
| 132 | + return searcher.search( |
| 133 | + sloppyQuery, new TopScoreDocCollectorManager(NUM_HITS, Integer.MAX_VALUE)); |
| 134 | + } |
| 135 | + |
| 136 | + @Benchmark |
| 137 | + public TopDocs benchmarkSloppyCompleteNoScores() throws IOException { |
| 138 | + return searcher.search( |
| 139 | + sloppyQuery, new TopFieldCollectorManager(Sort.INDEXORDER, NUM_HITS, Integer.MAX_VALUE)); |
| 140 | + } |
| 141 | +} |
0 commit comments