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