|
| 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 | +package org.apache.lucene.benchmark.jmh; |
| 18 | + |
| 19 | +import java.io.IOException; |
| 20 | +import java.nio.file.Files; |
| 21 | +import java.nio.file.Path; |
| 22 | +import java.util.Comparator; |
| 23 | +import java.util.Random; |
| 24 | +import java.util.concurrent.TimeUnit; |
| 25 | +import java.util.stream.Stream; |
| 26 | +import org.apache.lucene.document.Document; |
| 27 | +import org.apache.lucene.document.LongPoint; |
| 28 | +import org.apache.lucene.document.NumericDocValuesField; |
| 29 | +import org.apache.lucene.index.DirectoryReader; |
| 30 | +import org.apache.lucene.index.IndexReader; |
| 31 | +import org.apache.lucene.index.IndexWriter; |
| 32 | +import org.apache.lucene.index.IndexWriterConfig; |
| 33 | +import org.apache.lucene.sandbox.facet.plain.histograms.HistogramCollectorManager; |
| 34 | +import org.apache.lucene.search.IndexSearcher; |
| 35 | +import org.apache.lucene.search.MatchAllDocsQuery; |
| 36 | +import org.apache.lucene.store.Directory; |
| 37 | +import org.apache.lucene.store.MMapDirectory; |
| 38 | +import org.openjdk.jmh.annotations.Benchmark; |
| 39 | +import org.openjdk.jmh.annotations.BenchmarkMode; |
| 40 | +import org.openjdk.jmh.annotations.Fork; |
| 41 | +import org.openjdk.jmh.annotations.Level; |
| 42 | +import org.openjdk.jmh.annotations.Measurement; |
| 43 | +import org.openjdk.jmh.annotations.Mode; |
| 44 | +import org.openjdk.jmh.annotations.OutputTimeUnit; |
| 45 | +import org.openjdk.jmh.annotations.Param; |
| 46 | +import org.openjdk.jmh.annotations.Scope; |
| 47 | +import org.openjdk.jmh.annotations.Setup; |
| 48 | +import org.openjdk.jmh.annotations.State; |
| 49 | +import org.openjdk.jmh.annotations.TearDown; |
| 50 | +import org.openjdk.jmh.annotations.Warmup; |
| 51 | + |
| 52 | +@State(Scope.Thread) |
| 53 | +@BenchmarkMode(Mode.Throughput) |
| 54 | +@OutputTimeUnit(TimeUnit.SECONDS) |
| 55 | +@Fork(value = 1, warmups = 1) |
| 56 | +@Warmup(iterations = 1, time = 1) |
| 57 | +@Measurement(iterations = 3, time = 3) |
| 58 | +public class HistogramCollectorBenchmark { |
| 59 | + Directory dir; |
| 60 | + IndexReader reader; |
| 61 | + Path path; |
| 62 | + |
| 63 | + @Setup(Level.Trial) |
| 64 | + public void setup(BenchmarkParams params) throws Exception { |
| 65 | + path = Files.createTempDirectory("forUtil"); |
| 66 | + Directory dir = MMapDirectory.open(path); |
| 67 | + IndexWriter w = new IndexWriter(dir, new IndexWriterConfig()); |
| 68 | + Random r = new Random(0); |
| 69 | + |
| 70 | + for (int i = 0; i < params.docCount; i++) { |
| 71 | + Document doc = new Document(); |
| 72 | + long value = r.nextInt(0, params.docCount); |
| 73 | + if (params.pointEnabled) { |
| 74 | + // Adding indexed point field to verify multi range collector |
| 75 | + doc.add(new LongPoint("f", value)); |
| 76 | + } else { |
| 77 | + doc.add(NumericDocValuesField.indexedField("f", value)); |
| 78 | + } |
| 79 | + w.addDocument(doc); |
| 80 | + } |
| 81 | + // Force merging into single segment for testing more documents in segment scenario |
| 82 | + w.forceMerge(1, true); |
| 83 | + reader = DirectoryReader.open(w); |
| 84 | + w.close(); |
| 85 | + } |
| 86 | + |
| 87 | + @TearDown(Level.Trial) |
| 88 | + public void tearDown() throws Exception { |
| 89 | + reader.close(); |
| 90 | + if (dir != null) { |
| 91 | + dir.close(); |
| 92 | + dir = null; |
| 93 | + } |
| 94 | + |
| 95 | + // Clean up the segment files before next run |
| 96 | + if (Files.exists(path)) { |
| 97 | + try (Stream<Path> walk = Files.walk(path)) { |
| 98 | + walk.sorted(Comparator.reverseOrder()) |
| 99 | + .forEach( |
| 100 | + path -> { |
| 101 | + try { |
| 102 | + Files.delete(path); |
| 103 | + } catch ( |
| 104 | + @SuppressWarnings("unused") |
| 105 | + IOException unused) { |
| 106 | + // Do nothing |
| 107 | + } |
| 108 | + }); |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + @State(Scope.Benchmark) |
| 114 | + public static class BenchmarkParams { |
| 115 | + // Test with both point enabled and disabled |
| 116 | + @Param({"true", "false"}) |
| 117 | + public boolean pointEnabled; |
| 118 | + |
| 119 | + @Param({"500000", "5000000"}) |
| 120 | + public int docCount; |
| 121 | + |
| 122 | + @Param({"5000", "25000"}) |
| 123 | + public long bucketWidth; |
| 124 | + } |
| 125 | + |
| 126 | + @Benchmark |
| 127 | + public void collectHistogram(BenchmarkParams params) throws IOException { |
| 128 | + IndexSearcher searcher = new IndexSearcher(reader); |
| 129 | + searcher.search( |
| 130 | + new MatchAllDocsQuery(), new HistogramCollectorManager("f", params.bucketWidth, 10000)); |
| 131 | + } |
| 132 | +} |
0 commit comments