|
| 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.io.UncheckedIOException; |
| 21 | +import java.nio.file.Files; |
| 22 | +import java.nio.file.Path; |
| 23 | +import java.util.Comparator; |
| 24 | +import java.util.Random; |
| 25 | +import java.util.concurrent.TimeUnit; |
| 26 | +import java.util.stream.Stream; |
| 27 | +import org.apache.lucene.document.Document; |
| 28 | +import org.apache.lucene.document.Field; |
| 29 | +import org.apache.lucene.document.NumericDocValuesField; |
| 30 | +import org.apache.lucene.document.SortedNumericDocValuesField; |
| 31 | +import org.apache.lucene.document.StringField; |
| 32 | +import org.apache.lucene.index.DirectoryReader; |
| 33 | +import org.apache.lucene.index.IndexWriter; |
| 34 | +import org.apache.lucene.index.IndexWriterConfig; |
| 35 | +import org.apache.lucene.index.Term; |
| 36 | +import org.apache.lucene.search.BooleanClause.Occur; |
| 37 | +import org.apache.lucene.search.BooleanQuery; |
| 38 | +import org.apache.lucene.search.IndexSearcher; |
| 39 | +import org.apache.lucene.search.Query; |
| 40 | +import org.apache.lucene.search.TermQuery; |
| 41 | +import org.apache.lucene.store.Directory; |
| 42 | +import org.apache.lucene.store.MMapDirectory; |
| 43 | +import org.openjdk.jmh.annotations.Benchmark; |
| 44 | +import org.openjdk.jmh.annotations.BenchmarkMode; |
| 45 | +import org.openjdk.jmh.annotations.Fork; |
| 46 | +import org.openjdk.jmh.annotations.Level; |
| 47 | +import org.openjdk.jmh.annotations.Measurement; |
| 48 | +import org.openjdk.jmh.annotations.Mode; |
| 49 | +import org.openjdk.jmh.annotations.OutputTimeUnit; |
| 50 | +import org.openjdk.jmh.annotations.Param; |
| 51 | +import org.openjdk.jmh.annotations.Scope; |
| 52 | +import org.openjdk.jmh.annotations.Setup; |
| 53 | +import org.openjdk.jmh.annotations.State; |
| 54 | +import org.openjdk.jmh.annotations.TearDown; |
| 55 | +import org.openjdk.jmh.annotations.Warmup; |
| 56 | + |
| 57 | +/** Benchmarks range queries over dense numeric doc values encoded as raw, delta, GCD, or both. */ |
| 58 | +@State(Scope.Thread) |
| 59 | +@BenchmarkMode(Mode.Throughput) |
| 60 | +@OutputTimeUnit(TimeUnit.SECONDS) |
| 61 | +@Warmup(iterations = 3, time = 3) |
| 62 | +@Measurement(iterations = 5, time = 5) |
| 63 | +public class GcdDeltaRangeIntoBitSetBenchmark { |
| 64 | + |
| 65 | + private static final String FIELD = "val"; |
| 66 | + private static final String LEAD_FIELD = "lead"; |
| 67 | + private static final String LEAD_VALUE = "yes"; |
| 68 | + private static final String NONE = "none"; |
| 69 | + private static final String DELTA_ONLY = "delta_only"; |
| 70 | + private static final String GCD_1000 = "gcd_1000"; |
| 71 | + private static final String GCD_100_DELTA = "gcd_100_delta"; |
| 72 | + private static final long DOMAIN = 10_000_000L; |
| 73 | + private static final long DELTA = 1_700_000_000_000L; |
| 74 | + |
| 75 | + private Directory dir; |
| 76 | + private DirectoryReader reader; |
| 77 | + private IndexSearcher searcher; |
| 78 | + private Path path; |
| 79 | + private Query query; |
| 80 | + |
| 81 | + @Param({"1000000"}) |
| 82 | + public int numDocs; |
| 83 | + |
| 84 | + @Param({NONE, DELTA_ONLY, GCD_1000, GCD_100_DELTA}) |
| 85 | + public String encoding; |
| 86 | + |
| 87 | + @Param({"0.01", "0.1", "0.5"}) |
| 88 | + public double selectivity; |
| 89 | + |
| 90 | + @Setup(Level.Trial) |
| 91 | + public void setup() throws Exception { |
| 92 | + path = Files.createTempDirectory("gcdDeltaRangeIntoBitSet"); |
| 93 | + dir = MMapDirectory.open(path); |
| 94 | + |
| 95 | + Random random = new Random(0); |
| 96 | + try (IndexWriter writer = new IndexWriter(dir, new IndexWriterConfig())) { |
| 97 | + for (int i = 0; i < numDocs; i++) { |
| 98 | + Document doc = new Document(); |
| 99 | + doc.add(NumericDocValuesField.indexedField(FIELD, valueForDoc(encoding, i, random))); |
| 100 | + doc.add(new StringField(LEAD_FIELD, LEAD_VALUE, Field.Store.NO)); |
| 101 | + writer.addDocument(doc); |
| 102 | + } |
| 103 | + writer.forceMerge(1); |
| 104 | + } |
| 105 | + |
| 106 | + reader = DirectoryReader.open(dir); |
| 107 | + searcher = new IndexSearcher(reader); |
| 108 | + query = rangeQuery(encoding, selectivity); |
| 109 | + } |
| 110 | + |
| 111 | + private static long valueForDoc(String encoding, int doc, Random random) { |
| 112 | + if (doc == 0) { |
| 113 | + return minimumValue(encoding); |
| 114 | + } else if (doc == 1 && encoding.equals(GCD_100_DELTA)) { |
| 115 | + // Anchor entry.gcd to exactly 100 for the GCD_100_DELTA encoding: random multiples of 100 |
| 116 | + // could otherwise share a larger common factor under some seeds, which would change the |
| 117 | + // shape of the encoded values and what the benchmark measures. |
| 118 | + return DELTA + 100L; |
| 119 | + } |
| 120 | + |
| 121 | + long value = random.nextLong(0, DOMAIN); |
| 122 | + switch (encoding) { |
| 123 | + case NONE: |
| 124 | + return value; |
| 125 | + case DELTA_ONLY: |
| 126 | + return DELTA + value; |
| 127 | + case GCD_1000: |
| 128 | + return value * 1_000L; |
| 129 | + case GCD_100_DELTA: |
| 130 | + return DELTA + value * 100L; |
| 131 | + default: |
| 132 | + throw new IllegalArgumentException("Unknown encoding: " + encoding); |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + private static long minimumValue(String encoding) { |
| 137 | + switch (encoding) { |
| 138 | + case NONE: |
| 139 | + case GCD_1000: |
| 140 | + return 0; |
| 141 | + case DELTA_ONLY: |
| 142 | + case GCD_100_DELTA: |
| 143 | + return DELTA; |
| 144 | + default: |
| 145 | + throw new IllegalArgumentException("Unknown encoding: " + encoding); |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + private static Query rangeQuery(String encoding, double selectivity) { |
| 150 | + long range = Math.max(1, (long) (DOMAIN * selectivity)); |
| 151 | + long min = (DOMAIN - range) / 2; |
| 152 | + long max = min + range; |
| 153 | + Query rangeQuery = |
| 154 | + SortedNumericDocValuesField.newSlowRangeQuery( |
| 155 | + FIELD, actualValue(encoding, min), actualValue(encoding, max)); |
| 156 | + return new BooleanQuery.Builder() |
| 157 | + .add(new TermQuery(new Term(LEAD_FIELD, LEAD_VALUE)), Occur.FILTER) |
| 158 | + .add(rangeQuery, Occur.FILTER) |
| 159 | + .build(); |
| 160 | + } |
| 161 | + |
| 162 | + private static long actualValue(String encoding, long value) { |
| 163 | + switch (encoding) { |
| 164 | + case NONE: |
| 165 | + return value; |
| 166 | + case DELTA_ONLY: |
| 167 | + return DELTA + value; |
| 168 | + case GCD_1000: |
| 169 | + return value * 1_000L; |
| 170 | + case GCD_100_DELTA: |
| 171 | + return DELTA + value * 100L; |
| 172 | + default: |
| 173 | + throw new IllegalArgumentException("Unknown encoding: " + encoding); |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + @TearDown(Level.Trial) |
| 178 | + public void tearDown() throws Exception { |
| 179 | + reader.close(); |
| 180 | + dir.close(); |
| 181 | + if (Files.exists(path)) { |
| 182 | + try (Stream<Path> walk = Files.walk(path)) { |
| 183 | + walk.sorted(Comparator.reverseOrder()) |
| 184 | + .forEach( |
| 185 | + p -> { |
| 186 | + try { |
| 187 | + Files.delete(p); |
| 188 | + } catch (IOException e) { |
| 189 | + throw new UncheckedIOException(e); |
| 190 | + } |
| 191 | + }); |
| 192 | + } |
| 193 | + } |
| 194 | + } |
| 195 | + |
| 196 | + @Benchmark |
| 197 | + @Fork( |
| 198 | + value = 1, |
| 199 | + jvmArgsAppend = {"-Xmx2g", "-Xms2g", "-XX:+AlwaysPreTouch"}) |
| 200 | + public int rangeQueryDefaultProvider() throws IOException { |
| 201 | + return searcher.count(query); |
| 202 | + } |
| 203 | + |
| 204 | + @Benchmark |
| 205 | + @Fork( |
| 206 | + value = 1, |
| 207 | + jvmArgsAppend = { |
| 208 | + "--add-modules", |
| 209 | + "jdk.incubator.vector", |
| 210 | + "-Xmx2g", |
| 211 | + "-Xms2g", |
| 212 | + "-XX:+AlwaysPreTouch" |
| 213 | + }) |
| 214 | + public int rangeQueryPanamaProvider() throws IOException { |
| 215 | + return searcher.count(query); |
| 216 | + } |
| 217 | +} |
0 commit comments