mikemccand commented on code in PR #15950: URL: https://github.com/apache/lucene/pull/15950#discussion_r3094351326
########## lucene/benchmark-jmh/src/java/org/apache/lucene/benchmark/jmh/ScoreDocSortBenchmark.java: ########## @@ -0,0 +1,611 @@ +/* + * 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.util.Arrays; +import java.util.Comparator; +import java.util.IdentityHashMap; +import java.util.SplittableRandom; +import java.util.concurrent.TimeUnit; +import org.apache.lucene.search.ScoreDoc; +import org.apache.lucene.util.ArrayUtil; +import org.apache.lucene.util.InPlaceMergeSorter; +import org.apache.lucene.util.IntroSorter; +import org.apache.lucene.util.LSBRadixSorter; +import org.apache.lucene.util.TimSorter; +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; + +/** + * Benchmark comparing different sort implementations for sorting {@link ScoreDoc}[] by ascending + * doc ID. Simulates realistic ScoreDoc arrays with random doc IDs drawn from a 5M-doc index and + * random scores. + * + * <h2>Running</h2> + * + * Use {@code run-benchmark.sh} which automatically recompiles if sources changed, then runs JMH: + * + * <pre>{@code + * ./lucene/benchmark-jmh/run-benchmark.sh ScoreDocSortBenchmark \ + * -rf json -rff results.json + * }</pre> + * + * <p>Or build and run manually: + * + * <pre>{@code + * ./gradlew :lucene:benchmark-jmh:assemble + * java --module-path lucene/benchmark-jmh/build/benchmarks \ + * --module org.apache.lucene.benchmark.jmh \ + * ScoreDocSortBenchmark \ + * -rf json -rff results.json + * }</pre> + * + * <h2>Visualizing results</h2> + * + * The companion {@code jmh-table.py} script (in the same directory as this source file) converts + * JMH JSON output into an interactive HTML report: + * + * <pre>{@code + * python3 lucene/benchmark-jmh/jmh-table.py \ + * lucene/benchmark-jmh/src/java/org/apache/lucene/benchmark/jmh/ScoreDocSortBenchmark.java \ + * < results.json > results.html + * }</pre> + * + * <p>The HTML report provides: + * + * <ul> + * <li>A heatmap table with algorithms as rows and array sizes as columns. Green cells are the + * fastest, red cells are the slowest within each column. + * <li>Inline sparkline histograms in each cell showing the distribution of raw iteration samples, + * making outliers immediately visible. + * <li>Click any column header to sort the table by that column (click again to reverse). + * <li>Click any data cell to show a full histogram below the table with detailed statistics + * (mean, median, stddev, p5/p95, range) and the benchmark method source code to the right. + * <li>Clicking a cell updates the URL hash (e.g. {@code #introSorterAnonymous|1000}) so you can + * share a direct link to a specific result. + * <li>A configuration banner at the top showing JMH settings (mode, forks, threads, warmup, + * measurement iterations, JVM args). + * </ul> + */ +@BenchmarkMode(Mode.AverageTime) +@OutputTimeUnit(TimeUnit.MICROSECONDS) +@State(Scope.Thread) +@Warmup(iterations = 5, time = 1) +@Measurement(iterations = 5, time = 1) +@Fork( + value = 10, + jvmArgsAppend = {"-Xmx1g", "-Xms1g", "-XX:+AlwaysPreTouch"}) +public class ScoreDocSortBenchmark { + + private static final Comparator<ScoreDoc> BY_DOC_ASC = (a, b) -> Integer.compare(a.doc, b.doc); + + private static final int MAX_DOC = 5_000_000; + + @Param({"10", "50", "100", "500", "1000", "10000"}) + int size; + + // add "nearly_sorted", "reversed" to test other distributions + @Param({"random"}) + String distribution; + + /** Template array; copied before each invocation so every sort sees the same random order. */ + private ScoreDoc[] template; + + /** Working copy that each benchmark method sorts in place. */ + private ScoreDoc[] work; + + @Setup(Level.Trial) + public void setupTrial() { + SplittableRandom rng = new SplittableRandom(0xCAFEBABE); + template = new ScoreDoc[size]; + for (int i = 0; i < size; i++) { + int doc = rng.nextInt(MAX_DOC); Review Comment: Ooh good catch, you're right -- Lucene hits would never duplicate docid. -- 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]
