|
| 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.NumericDocValuesField; |
| 28 | +import org.apache.lucene.document.SortedNumericDocValuesField; |
| 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.search.BooleanClause.Occur; |
| 34 | +import org.apache.lucene.search.BooleanQuery; |
| 35 | +import org.apache.lucene.search.IndexSearcher; |
| 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 | +/** |
| 53 | + * Benchmarks BooleanQuery with multiple numeric range FILTER clauses. |
| 54 | + * |
| 55 | + * <p>Run with and without the MultiFieldDocValuesRangeQuery coordination changes to compare. To |
| 56 | + * benchmark the baseline, revert the BooleanQuery.rewrite() coordination rule and re-run. |
| 57 | + * |
| 58 | + * <p>Data patterns: |
| 59 | + * |
| 60 | + * <ul> |
| 61 | + * <li>clustered: values increase with docID (tight skip blocks, many YES/NO). Best case. |
| 62 | + * <li>mixed: field0 monotonic, field1 low-cardinality, rest random. Realistic. |
| 63 | + * <li>sorted: field0 monotonic (index sort key), rest random. Tests pre-sorted indexes. |
| 64 | + * <li>random: all fields uniform random. Worst case. |
| 65 | + * </ul> |
| 66 | + */ |
| 67 | +@State(Scope.Thread) |
| 68 | +@BenchmarkMode(Mode.Throughput) |
| 69 | +@OutputTimeUnit(TimeUnit.SECONDS) |
| 70 | +@Warmup(iterations = 3, time = 3) |
| 71 | +@Measurement(iterations = 5, time = 5) |
| 72 | +@Fork(value = 1, warmups = 1) |
| 73 | +public class MultiFieldDocValuesRangeBenchmark { |
| 74 | + |
| 75 | + private static final String CLUSTERED = "clustered"; |
| 76 | + private static final String MIXED = "mixed"; |
| 77 | + private static final String SORTED = "sorted"; |
| 78 | + private static final String RANDOM = "random"; |
| 79 | + |
| 80 | + private Directory dir; |
| 81 | + private IndexReader reader; |
| 82 | + private Path path; |
| 83 | + private BooleanQuery query; |
| 84 | + |
| 85 | + @State(Scope.Benchmark) |
| 86 | + public static class Params { |
| 87 | + @Param({"1000000", "10000000"}) |
| 88 | + public int docCount; |
| 89 | + |
| 90 | + @Param({"3", "5"}) |
| 91 | + public int fieldCount; |
| 92 | + |
| 93 | + @Param({CLUSTERED, MIXED, RANDOM, SORTED}) |
| 94 | + public String dataPattern; |
| 95 | + } |
| 96 | + |
| 97 | + @Setup(Level.Trial) |
| 98 | + public void setup(Params params) throws Exception { |
| 99 | + path = Files.createTempDirectory("multiFieldBench"); |
| 100 | + dir = MMapDirectory.open(path); |
| 101 | + |
| 102 | + IndexWriterConfig iwc = new IndexWriterConfig(); |
| 103 | + if (params.dataPattern.equals(SORTED)) { |
| 104 | + iwc.setIndexSort( |
| 105 | + new org.apache.lucene.search.Sort( |
| 106 | + new org.apache.lucene.search.SortField( |
| 107 | + "field0", org.apache.lucene.search.SortField.Type.LONG))); |
| 108 | + } |
| 109 | + |
| 110 | + IndexWriter w = new IndexWriter(dir, iwc); |
| 111 | + Random r = new Random(42); |
| 112 | + |
| 113 | + for (int i = 0; i < params.docCount; i++) { |
| 114 | + Document doc = new Document(); |
| 115 | + for (int f = 0; f < params.fieldCount; f++) { |
| 116 | + long value = generateValue(params.dataPattern, f, i, params.docCount, r); |
| 117 | + doc.add(NumericDocValuesField.indexedField("field" + f, value)); |
| 118 | + } |
| 119 | + w.addDocument(doc); |
| 120 | + } |
| 121 | + w.forceMerge(1); |
| 122 | + reader = DirectoryReader.open(w); |
| 123 | + w.close(); |
| 124 | + |
| 125 | + BooleanQuery.Builder bqBuilder = new BooleanQuery.Builder(); |
| 126 | + for (int f = 0; f < params.fieldCount; f++) { |
| 127 | + long[] range = getQueryRange(params.dataPattern, f, params.docCount); |
| 128 | + bqBuilder.add( |
| 129 | + SortedNumericDocValuesField.newSlowRangeQuery("field" + f, range[0], range[1]), |
| 130 | + Occur.FILTER); |
| 131 | + } |
| 132 | + query = bqBuilder.build(); |
| 133 | + } |
| 134 | + |
| 135 | + private static long generateValue( |
| 136 | + String pattern, int fieldIdx, int docIdx, int docCount, Random r) { |
| 137 | + switch (pattern) { |
| 138 | + case CLUSTERED: |
| 139 | + long scale = (fieldIdx + 1) * 100L; |
| 140 | + long noise = r.nextInt(50); |
| 141 | + return (docIdx * scale / docCount) * docCount / scale * scale + noise; |
| 142 | + case MIXED: |
| 143 | + if (fieldIdx == 0) { |
| 144 | + return (long) docIdx * 1000L + r.nextInt(100); |
| 145 | + } else if (fieldIdx == 1) { |
| 146 | + return r.nextInt(20); |
| 147 | + } else { |
| 148 | + return r.nextLong(0, docCount); |
| 149 | + } |
| 150 | + case SORTED: |
| 151 | + // field0: monotonically increasing (will be the index sort key) |
| 152 | + // field1+: random values (not sorted — these benefit from coordination) |
| 153 | + if (fieldIdx == 0) { |
| 154 | + return (long) docIdx * 1000L + r.nextInt(100); |
| 155 | + } else { |
| 156 | + return r.nextLong(0, docCount); |
| 157 | + } |
| 158 | + case RANDOM: |
| 159 | + return r.nextLong(0, docCount); |
| 160 | + default: |
| 161 | + throw new IllegalArgumentException("Unknown pattern: " + pattern); |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + private static long[] getQueryRange(String pattern, int fieldIdx, int docCount) { |
| 166 | + switch (pattern) { |
| 167 | + case CLUSTERED: |
| 168 | + long scale = (fieldIdx + 1) * 100L; |
| 169 | + long maxVal = scale; |
| 170 | + long rangeSize = maxVal / 10; |
| 171 | + long offset = (fieldIdx * maxVal / 5); |
| 172 | + return new long[] {offset, offset + rangeSize}; |
| 173 | + case MIXED: |
| 174 | + if (fieldIdx == 0) { |
| 175 | + long maxVal2 = (long) docCount * 1000L + 100; |
| 176 | + return new long[] {(long) (maxVal2 * 0.9), maxVal2}; |
| 177 | + } else if (fieldIdx == 1) { |
| 178 | + return new long[] {15, 19}; |
| 179 | + } else { |
| 180 | + long rangeSize2 = docCount / 10; |
| 181 | + long lower2 = (docCount - rangeSize2) / 2; |
| 182 | + return new long[] {lower2, lower2 + rangeSize2}; |
| 183 | + } |
| 184 | + case SORTED: |
| 185 | + if (fieldIdx == 0) { |
| 186 | + long maxVal3 = (long) docCount * 1000L + 100; |
| 187 | + return new long[] {(long) (maxVal3 * 0.9), maxVal3}; |
| 188 | + } else { |
| 189 | + long rangeSize3 = docCount / 10; |
| 190 | + long lower3 = (docCount - rangeSize3) / 2; |
| 191 | + return new long[] {lower3, lower3 + rangeSize3}; |
| 192 | + } |
| 193 | + case RANDOM: |
| 194 | + long rangeSize4 = docCount / 5; |
| 195 | + long lower4 = (docCount - rangeSize4) / 2; |
| 196 | + return new long[] {lower4, lower4 + rangeSize4}; |
| 197 | + default: |
| 198 | + throw new IllegalArgumentException("Unknown pattern: " + pattern); |
| 199 | + } |
| 200 | + } |
| 201 | + |
| 202 | + @TearDown(Level.Trial) |
| 203 | + public void tearDown() throws Exception { |
| 204 | + reader.close(); |
| 205 | + if (dir != null) { |
| 206 | + dir.close(); |
| 207 | + dir = null; |
| 208 | + } |
| 209 | + if (Files.exists(path)) { |
| 210 | + try (Stream<Path> walk = Files.walk(path)) { |
| 211 | + walk.sorted(Comparator.reverseOrder()) |
| 212 | + .forEach( |
| 213 | + p -> { |
| 214 | + try { |
| 215 | + Files.delete(p); |
| 216 | + } catch (IOException _) { |
| 217 | + } |
| 218 | + }); |
| 219 | + } |
| 220 | + } |
| 221 | + } |
| 222 | + |
| 223 | + @Benchmark |
| 224 | + public int searchMultiFieldRange() throws IOException { |
| 225 | + IndexSearcher searcher = new IndexSearcher(reader); |
| 226 | + return searcher.count(query); |
| 227 | + } |
| 228 | +} |
0 commit comments