salvatorecampagna commented on code in PR #15413: URL: https://github.com/apache/lucene/pull/15413#discussion_r2511777081
########## lucene/benchmark-jmh/src/java/org/apache/lucene/benchmark/jmh/LiveDocsBenchmark.java: ########## @@ -0,0 +1,347 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.lucene.benchmark.jmh; + +import java.io.IOException; +import java.util.Random; +import java.util.concurrent.TimeUnit; +import org.apache.lucene.search.DocIdSetIterator; +import org.apache.lucene.util.DenseLiveDocs; +import org.apache.lucene.util.FixedBitSet; +import org.apache.lucene.util.SparseFixedBitSet; +import org.apache.lucene.util.SparseLiveDocs; +import org.openjdk.jmh.annotations.Benchmark; +import org.openjdk.jmh.annotations.BenchmarkMode; +import org.openjdk.jmh.annotations.Fork; +import org.openjdk.jmh.annotations.Level; +import org.openjdk.jmh.annotations.Measurement; +import org.openjdk.jmh.annotations.Mode; +import org.openjdk.jmh.annotations.OutputTimeUnit; +import org.openjdk.jmh.annotations.Param; +import org.openjdk.jmh.annotations.Scope; +import org.openjdk.jmh.annotations.Setup; +import org.openjdk.jmh.annotations.State; +import org.openjdk.jmh.annotations.Warmup; +import org.openjdk.jmh.infra.Blackhole; + +/** + * Benchmarks comparing {@link SparseLiveDocs} vs {@link DenseLiveDocs} performance across different + * deletion rates, patterns, and segment sizes. + * + * <p>This benchmark suite measures four key operations to evaluate the trade-offs between sparse + * and dense LiveDocs implementations: + * + * <ul> + * <li><b>Random access (get)</b> - O(1) for both, but sparse has additional indirection overhead + * <li><b>Deleted docs iteration</b> - O(deletedDocs) for sparse, O(maxDoc) for dense + * <li><b>Live docs iteration (full)</b> - O(maxDoc) for both, tests get() performance at scale + * <li><b>Live docs iteration (range)</b> - O(range) for both, tests advance() and get() on subset + * </ul> + * + * <h2>Benchmark Parameters</h2> + * + * <ul> + * <li><b>maxDoc</b> - Segment sizes: 100K, 1M, 10M documents + * <li><b>deletionRate</b> - Percentage of deleted documents: 0.1%, 1%, 5%, 10%, 20%, 30% + * <li><b>deletionPattern</b> - Distribution of deletions: + * <ul> + * <li>RANDOM: Deletions scattered uniformly across entire document space + * <li>CLUSTERED: Consecutive deletions at start of segment + * <li>UNIFORM: Deletions evenly spaced across segment + * </ul> + * </ul> + * + * <h2>Usage</h2> + * + * <p>Run all benchmarks: + * + * <pre> + * java -jar lucene-benchmark-jmh.jar "LiveDocsBenchmark" + * </pre> + * + * <p>Run specific operation for sparse only: + * + * <pre> + * java -jar lucene-benchmark-jmh.jar "LiveDocsBenchmark.sparseIterateDeleted" + * </pre> + * + * <p>Filter by specific parameters: + * + * <pre> + * java -jar lucene-benchmark-jmh.jar "LiveDocsBenchmark" -p deletionRate=0.01 -p deletionPattern=CLUSTERED + * </pre> + * + * @see SparseLiveDocs + * @see DenseLiveDocs + * @see LiveDocsPathologicalBenchmark + */ +@BenchmarkMode(Mode.AverageTime) +@OutputTimeUnit(TimeUnit.MICROSECONDS) +@State(Scope.Benchmark) +@Warmup(iterations = 3, time = 2) +@Measurement(iterations = 5, time = 3) +@Fork( + value = 1, + jvmArgsAppend = {"-Xmx2g", "-Xms2g"}) +public class LiveDocsBenchmark { + + /** Number of documents in the segment (100K, 1M, or 10M). */ + @Param({"100000", "1000000", "10000000"}) + int maxDoc; + + /** Percentage of documents to delete (0.1%, 1%, 5%, 10%, 20%, or 30%). */ + @Param({"0.001", "0.01", "0.05", "0.10", "0.20", "0.30"}) + double deletionRate; + + /** + * Pattern for distributing deletions: + * + * <ul> + * <li>RANDOM - Deletions scattered uniformly across document space + * <li>CLUSTERED - Consecutive deletions at start of segment + * <li>UNIFORM - Deletions evenly spaced across segment + * </ul> + */ + @Param({"RANDOM", "CLUSTERED", "UNIFORM"}) + String deletionPattern; + + /** Sparse LiveDocs implementation under test. */ + private SparseLiveDocs sparseLiveDocs; + + /** Dense LiveDocs implementation for comparison. */ + private DenseLiveDocs denseLiveDocs; + + /** Pre-generated random document IDs for random access benchmarks. */ + private int[] randomDocIds; + + /** Number of random accesses to perform in each benchmark iteration. */ + private static final int RANDOM_ACCESS_SIZE = 10000; + + /** + * Sets up the benchmark by creating both sparse and dense LiveDocs with identical deletion + * patterns. + * + * <p>This method is called once per trial (combination of parameters) before any benchmark + * iterations run. + */ + @Setup(Level.Trial) Review Comment: Had to remove the print statement s it makes the CI fail due to api not allowed...will see if I can add `AuxCounters` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
