forked from opensearch-project/OpenSearch
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
benchmark a simple point range query
Signed-off-by: bowenlan-amzn <[email protected]>
- Loading branch information
1 parent
e3bbc74
commit 5bade60
Showing
1 changed file
with
122 additions
and
0 deletions.
There are no files selected for viewing
122 changes: 122 additions & 0 deletions
122
...src/main/java/org/opensearch/benchmark/search/aggregations/datehisto/PointRangeQuery.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
package org.opensearch.benchmark.search.aggregations.datehisto; | ||
|
||
import org.apache.lucene.document.Field; | ||
import org.apache.lucene.document.LongPoint; | ||
import org.apache.lucene.document.SortedNumericDocValuesField; | ||
import org.apache.lucene.document.StringField; | ||
import org.apache.lucene.index.DirectoryReader; | ||
import org.apache.lucene.index.IndexReader; | ||
import org.apache.lucene.index.IndexableField; | ||
import org.apache.lucene.search.IndexSearcher; | ||
import org.apache.lucene.search.Query; | ||
import org.apache.lucene.search.TotalHitCountCollectorManager; | ||
import org.apache.lucene.store.FSDirectory; | ||
import org.apache.lucene.index.IndexWriter; | ||
import org.apache.lucene.index.IndexWriterConfig; | ||
import org.apache.lucene.store.Directory; | ||
import org.openjdk.jmh.annotations.Benchmark; | ||
import org.openjdk.jmh.annotations.BenchmarkMode; | ||
import org.openjdk.jmh.annotations.Fork; | ||
import org.openjdk.jmh.annotations.Measurement; | ||
import org.openjdk.jmh.annotations.Mode; | ||
import org.openjdk.jmh.annotations.OutputTimeUnit; | ||
import org.openjdk.jmh.annotations.Scope; | ||
import org.openjdk.jmh.annotations.Setup; | ||
import org.openjdk.jmh.annotations.State; | ||
import org.openjdk.jmh.annotations.TearDown; | ||
import org.openjdk.jmh.annotations.Warmup; | ||
import org.openjdk.jmh.infra.Blackhole; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
/** | ||
* Index random docs | ||
* <p> | ||
* Search and collect into a map | ||
*/ | ||
@Fork(1) | ||
@Warmup(iterations = 10) | ||
@Measurement(iterations = 5) | ||
@BenchmarkMode(Mode.AverageTime) | ||
@OutputTimeUnit(TimeUnit.NANOSECONDS) | ||
@State(Scope.Benchmark) | ||
public class PointRangeQuery { | ||
|
||
private static final long docCount = 100_000; | ||
private static Path dirPath; | ||
private static IndexReader reader; | ||
|
||
@Setup | ||
public void indexRandomDocs() throws IOException { | ||
// dir = Files.createTempDirectory(PointRangeQuery.class.getSimpleName()); | ||
dirPath = Paths.get("benchmark-index-" + docCount); | ||
|
||
try (Directory directory = FSDirectory.open(dirPath); | ||
IndexWriter writer = new IndexWriter(directory, new IndexWriterConfig())) { | ||
|
||
if (DirectoryReader.indexExists(directory)) { | ||
reader = DirectoryReader.open(directory); | ||
System.out.println("Existing doc: " + reader.numDocs()); | ||
if (reader.numDocs() == docCount) { | ||
System.out.println("Already indexed"); | ||
return; | ||
} | ||
} | ||
|
||
for (int i = 0; i < docCount; i++) { | ||
List<IndexableField> doc = new ArrayList<>(); | ||
doc.add(new StringField("id", Integer.toString(i), Field.Store.YES)); | ||
doc.add(new LongPoint("number", i)); | ||
doc.add(new SortedNumericDocValuesField("number", i)); | ||
writer.addDocument(doc); | ||
} | ||
} | ||
|
||
try (Directory directory = FSDirectory.open(dirPath)) { | ||
reader = DirectoryReader.open(directory); | ||
if (reader.numDocs() != docCount) { | ||
System.out.println("Confirming doc: " + reader.numDocs()); | ||
throw new IllegalStateException("Not expected number of documents"); | ||
} | ||
} | ||
} | ||
|
||
@TearDown | ||
public void tearDown() throws IOException { | ||
// for (String indexFile : FSDirectory.listAll(dir)) { | ||
// Files.deleteIfExists(dir.resolve(indexFile)); | ||
// } | ||
// Files.deleteIfExists(dir); | ||
|
||
reader.close(); | ||
} | ||
|
||
@Benchmark | ||
public void searchAndCollect(Blackhole bh) throws IOException { | ||
IndexSearcher searcher = new IndexSearcher(reader); | ||
Query query = LongPoint.newRangeQuery("number", 0, docCount); | ||
int count = searcher.search(query, new TotalHitCountCollectorManager()); | ||
bh.consume(count); | ||
} | ||
|
||
@Benchmark | ||
public void iterateDVAndCollect(Blackhole bh) throws IOException { | ||
IndexSearcher searcher = new IndexSearcher(reader); | ||
final Query query = SortedNumericDocValuesField.newSlowRangeQuery("number", 0, docCount); | ||
int count = searcher.search(query, new TotalHitCountCollectorManager()); | ||
bh.consume(count); | ||
} | ||
} |