Apache Lucene open-source search software
Adding profiling support for concurrent segment search (#14413)
Ankit Jainf30c7b6e74df
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txtindex 3181ef0061e..bc5fd58775d 100644--- a/lucene/CHANGES.txt+++ b/lucene/CHANGES.txt@@ -137,6 +137,8 @@ New Features* GITHUB#14404: Introducing DocValuesMultiRangeQuery.SortedNumericStabbingBuilder into sandbox.(Mikhail Khludnev)+* GITHUB#14413: Adding profiling support for concurrent segment search. (Ankit Jain)+* GITHUB#14565: Add ParentsChildrenBlockJoinQuery that supports parent and child filter in the same queryalong with limiting number of child documents to retrieve per parent. (Jinny Wang)diff --git a/lucene/sandbox/src/java/org/apache/lucene/sandbox/search/AggregatedQueryLeafProfilerResult.java b/lucene/sandbox/src/java/org/apache/lucene/sandbox/search/AggregatedQueryLeafProfilerResult.javanew file mode 100644index 00000000000..d64dd7fb858--- /dev/null+++ b/lucene/sandbox/src/java/org/apache/lucene/sandbox/search/AggregatedQueryLeafProfilerResult.java@@ -0,0 +1,80 @@+/*+ * 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.sandbox.search;++import java.util.Collections;+import java.util.Map;+import java.util.Objects;++/**+ * This class is the internal representation of a profiled Query, corresponding to a single node in+ * the query tree. It is built after the query has finished executing and is merely a structured+ * representation, rather than the entity that collects the timing profile.+ */+public class AggregatedQueryLeafProfilerResult {+ private final Thread thread;+ private final Map<String, Long> breakdown;+ private final long startTime;+ private final long totalTime;++ public AggregatedQueryLeafProfilerResult(+ Thread thread, Map<String, Long> breakdown, long startTime, long totalTime) {+ this.thread = thread;+ this.breakdown =+ Collections.unmodifiableMap(+ Objects.requireNonNull(breakdown, "required breakdown argument missing"));+ this.startTime = startTime;+ this.totalTime = totalTime;+ }++ /**+ * Returns the thread that executed query for these leaves+ *+ * @return thread that executed query node+ */+ public Thread getThread() {+ return thread;+ }++ /**+ * Returns the timing breakdown for this node.+ *+ * @return map containing time breakdown across different operation types+ */+ public Map<String, Long> getTimeBreakdown() {+ return breakdown;+ }++ /**+ * Returns the start time for this query node execution.+ *+ * @return start time in nanoseconds+ */+ public long getStartTime() {+ return startTime;+ }++ /**+ * Returns the total time (inclusive of children) for this query node.+ *+ * @return elapsed time in nanoseconds+ */+ public long getTotalTime() {+ return totalTime;+ }+}diff --git a/lucene/sandbox/src/java/org/apache/lucene/sandbox/search/QueryLeafProfilerBreakdown.java b/lucene/sandbox/src/java/org/apache/lucene/sandbox/search/QueryLeafProfilerBreakdown.javanew file mode 100644index 00000000000..e53a54c92e5--- /dev/null+++ b/lucene/sandbox/src/java/org/apache/lucene/sandbox/search/QueryLeafProfilerBreakdown.java@@ -0,0 +1,90 @@+/*+ * 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.sandbox.search;++import java.util.Arrays;+import java.util.Collection;+import java.util.Collections;+import java.util.EnumMap;+import java.util.Map;+import org.apache.lucene.util.CollectionUtil;++/**+ * A record of timings for the various operations that may happen during query execution. A node's+ * time may be composed of several internal attributes (rewriting, weighting, scoring, etc).+ */+public class QueryLeafProfilerBreakdown {+ private static final Collection<QueryProfilerTimingType> LEAF_LEVEL_TIMING_TYPE =+ Arrays.stream(QueryProfilerTimingType.values()).filter(t -> t.isLeafLevel()).toList();++ /** The accumulated timings for this query node */+ private final QueryProfilerTimer[] timers;++ EnumMap<QueryProfilerTimingType, QueryProfilerTimer> map;++ /** Sole constructor. */+ public QueryLeafProfilerBreakdown() {+ timers = new QueryProfilerTimer[LEAF_LEVEL_TIMING_TYPE.size()];+ for (int i = 0; i < timers.length; ++i) {+ timers[i] = new QueryProfilerTimer();+ }+ }++ public QueryProfilerTimer getTimer(QueryProfilerTimingType type) {+ return timers[type.ordinal()];+ }++ /** Build a timing count breakdown. */+ public final Map<String, Long> toBreakdownMap() {+ Map<String, Long> map = CollectionUtil.newHashMap(timers.length * 2);+ for (QueryProfilerTimingType type : LEAF_LEVEL_TIMING_TYPE) {+ map.put(type.toString(), timers[type.ordinal()].getApproximateTiming());+ map.put(type.toString() + "_count", timers[type.ordinal()].getCount());+ }+ return Collections.unmodifiableMap(map);+ }++ public final AggregatedQueryLeafProfilerResult getLeafProfilerResult(Thread thread) {+ final Map<String, Long> map = CollectionUtil.newHashMap(LEAF_LEVEL_TIMING_TYPE.size() * 2);+ long sliceStartTime = Long.MAX_VALUE;+ long sliceEndTime = Long.MIN_VALUE;+ for (QueryProfilerTimingType type : LEAF_LEVEL_TIMING_TYPE) {+ final QueryProfilerTimer timer = timers[type.ordinal()];+ // Consider timer for updating start/total time only+ // if it was used+ if (timer.getCount() > 0) {+ sliceStartTime = Math.min(sliceStartTime, timer.getEarliestTimerStartTime());+ sliceEndTime =+ Math.max(+ sliceEndTime, timer.getEarliestTimerStartTime() + timer.getApproximateTiming());+ }+ map.put(type.toString(), timer.getApproximateTiming());+ map.put(type.toString() + "_count", timer.getCount());+ }+ return new AggregatedQueryLeafProfilerResult(+ thread, map, sliceStartTime, sliceEndTime - sliceStartTime);+ }++ public final long toTotalTime() {+ long total = 0;+ for (QueryProfilerTimer timer : timers) {+ total += timer.getApproximateTiming();+ }+ return total;+ }+}diff --git a/lucene/sandbox/src/java/org/apache/lucene/sandbox/search/QueryLeafProfilerThreadAggregator.java b/lucene/sandbox/src/java/org/apache/lucene/sandbox/search/QueryLeafProfilerThreadAggregator.javanew file mode 100644index 00000000000..ca9e7e38ea2--- /dev/null+++ b/lucene/sandbox/src/java/org/apache/lucene/sandbox/search/QueryLeafProfilerThreadAggregator.java@@ -0,0 +1,74 @@+/*+ * 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.sandbox.search;++import java.util.ArrayList;+import java.util.List;+import java.util.concurrent.ConcurrentHashMap;+import java.util.concurrent.ConcurrentMap;++/** QueryLeafProfilerAggregator that aggregates leaf breakdowns at thread level */+class QueryLeafProfilerThreadAggregator {+ private final ConcurrentMap<Thread, QueryLeafProfilerBreakdown> queryThreadBreakdowns;+ private long queryStartTime = Long.MAX_VALUE;+ private long queryTotalTime = 0;++ public QueryLeafProfilerThreadAggregator() {+ queryThreadBreakdowns = new ConcurrentHashMap<>();+ }++ public long getQueryStartTime() {+ return queryStartTime;+ }++ public long getQueryTotalTime() {+ return queryTotalTime;+ }++ private QueryLeafProfilerBreakdown getQueryThreadProfilerBreakdown() {+ final Thread currentThread = Thread.currentThread();+ // See please https://bugs.openjdk.java.net/browse/JDK-8161372+ final QueryLeafProfilerBreakdown profilerBreakdown = queryThreadBreakdowns.get(currentThread);++ if (profilerBreakdown != null) {+ return profilerBreakdown;+ }++ return queryThreadBreakdowns.computeIfAbsent(+ currentThread, _ -> new QueryLeafProfilerBreakdown());+ }++ public QueryProfilerTimer getTimer(QueryProfilerTimingType timingType) {+ assert timingType.isLeafLevel();++ return getQueryThreadProfilerBreakdown().getTimer(timingType);+ }++ public List<AggregatedQueryLeafProfilerResult> getAggregatedQueryLeafProfilerResults() {+ final List<AggregatedQueryLeafProfilerResult> profilerResults = new ArrayList<>();+ for (Thread thread : queryThreadBreakdowns.keySet()) {+ final AggregatedQueryLeafProfilerResult aggregatedQueryLeafProfilerResult =+ queryThreadBreakdowns.get(thread).getLeafProfilerResult(thread);+ queryStartTime = Math.min(queryStartTime, aggregatedQueryLeafProfilerResult.getStartTime());+ queryTotalTime += aggregatedQueryLeafProfilerResult.getTotalTime();+ profilerResults.add(aggregatedQueryLeafProfilerResult);+ }++ return profilerResults;+ }+}diff --git a/lucene/sandbox/src/java/org/apache/lucene/sandbox/search/QueryProfilerBreakdown.java b/lucene/sandbox/src/java/org/apache/lucene/sandbox/search/QueryProfilerBreakdown.javaindex 03a1880d48e..29e5c09598b 100644--- a/lucene/sandbox/src/java/org/apache/lucene/sandbox/search/QueryProfilerBreakdown.java+++ b/lucene/sandbox/src/java/org/apache/lucene/sandbox/search/QueryProfilerBreakdown.java@@ -17,8 +17,13 @@package org.apache.lucene.sandbox.search;+import java.util.Arrays;+import java.util.Collection;import java.util.Collections;+import java.util.HashMap;+import java.util.List;import java.util.Map;+import org.apache.lucene.search.Query;import org.apache.lucene.util.CollectionUtil;/**@@ -26,37 +31,75 @@ import org.apache.lucene.util.CollectionUtil;* time may be composed of several internal attributes (rewriting, weighting, scoring, etc).*/class QueryProfilerBreakdown {+ private static final Collection<QueryProfilerTimingType> QUERY_LEVEL_TIMING_TYPE =+ Arrays.stream(QueryProfilerTimingType.values()).filter(t -> !t.isLeafLevel()).toList();+ private final Map<QueryProfilerTimingType, QueryProfilerTimer> queryProfilerTimers;- /** The accumulated timings for this query node */- private final QueryProfilerTimer[] timers;+ private final QueryLeafProfilerThreadAggregator queryLeafProfilerAggregator;/** Sole constructor. */public QueryProfilerBreakdown() {- timers = new QueryProfilerTimer[QueryProfilerTimingType.values().length];- for (int i = 0; i < timers.length; ++i) {- timers[i] = new QueryProfilerTimer();+ queryProfilerTimers = new HashMap<>();+ this.queryLeafProfilerAggregator = new QueryLeafProfilerThreadAggregator();++ for (QueryProfilerTimingType timingType : QUERY_LEVEL_TIMING_TYPE) {+ queryProfilerTimers.put(timingType, new QueryProfilerTimer());}}- public QueryProfilerTimer getTimer(QueryProfilerTimingType type) {- return timers[type.ordinal()];+ public final QueryProfilerTimer getTimer(QueryProfilerTimingType timingType) {+ if (timingType.isLeafLevel()) {+ return queryLeafProfilerAggregator.getTimer(timingType);+ }++ // Return the query level profiler timer if not+ // slice level+ return queryProfilerTimers.get(timingType);}/** Build a timing count breakdown. */- public final Map<String, Long> toBreakdownMap() {- Map<String, Long> map = CollectionUtil.newHashMap(timers.length * 2);- for (QueryProfilerTimingType type : QueryProfilerTimingType.values()) {- map.put(type.toString(), timers[type.ordinal()].getApproximateTiming());- map.put(type.toString() + "_count", timers[type.ordinal()].getCount());+ public final QueryProfilerResult getQueryProfilerResult(+ Query query, List<QueryProfilerResult> childrenProfileResults) {+ long queryStartTime = Long.MAX_VALUE;+ long queryTotalTime = 0;+ final Map<String, Long> breakdownMap =+ CollectionUtil.newHashMap(QUERY_LEVEL_TIMING_TYPE.size() * 2);+ for (QueryProfilerTimingType type : QUERY_LEVEL_TIMING_TYPE) {+ final QueryProfilerTimer timer = queryProfilerTimers.get(type);+ if (timer.getCount() > 0) {+ queryStartTime = Math.min(queryStartTime, timer.getEarliestTimerStartTime());+ queryTotalTime += timer.getApproximateTiming();+ }+ // TODO: Should we put only non-zero timer values in the final output?+ breakdownMap.put(type.toString(), queryProfilerTimers.get(type).getApproximateTiming());+ breakdownMap.put(type.toString() + "_count", queryProfilerTimers.get(type).getCount());}- return Collections.unmodifiableMap(map);++ final List<AggregatedQueryLeafProfilerResult> threadProfilerResults =+ queryLeafProfilerAggregator.getAggregatedQueryLeafProfilerResults();+ queryStartTime = Math.min(queryStartTime, queryLeafProfilerAggregator.getQueryStartTime());+ queryTotalTime += queryLeafProfilerAggregator.getQueryTotalTime();++ return new QueryProfilerResult(+ getTypeFromQuery(query),+ getDescriptionFromQuery(query),+ Collections.unmodifiableMap(breakdownMap),+ threadProfilerResults,+ childrenProfileResults,+ queryStartTime,+ queryTotalTime);}- public final long toTotalTime() {- long total = 0;- for (QueryProfilerTimer timer : timers) {- total += timer.getApproximateTiming();+ private String getTypeFromQuery(Query query) {+ // Anonymous classes won't have a name,+ // we need to get the super class+ if (query.getClass().getSimpleName().isEmpty()) {+ return query.getClass().getSuperclass().getSimpleName();}- return total;+ return query.getClass().getSimpleName();+ }++ private String getDescriptionFromQuery(Query query) {+ return query.toString();}}diff --git a/lucene/sandbox/src/java/org/apache/lucene/sandbox/search/QueryProfilerResult.java b/lucene/sandbox/src/java/org/apache/lucene/sandbox/search/QueryProfilerResult.javaindex eb3f7aca8a8..9e8a204bcae 100644--- a/lucene/sandbox/src/java/org/apache/lucene/sandbox/search/QueryProfilerResult.java+++ b/lucene/sandbox/src/java/org/apache/lucene/sandbox/search/QueryProfilerResult.java@@ -31,39 +31,52 @@ import java.util.Objects;* queries if applicable*/public class QueryProfilerResult {-private final String type;private final String description;- private final Map<String, Long> breakdown;+ private final long startTime;private final long totalTime;- private final List<QueryProfilerResult> children;+ private final Map<String, Long> queryBreakdowns;+ private final List<AggregatedQueryLeafProfilerResult> aggregatedQueryLeafBreakdowns;+ private final List<QueryProfilerResult> childrenProfileResults;public QueryProfilerResult(String type,String description,- Map<String, Long> breakdown,- long totalTime,- List<QueryProfilerResult> children) {+ Map<String, Long> queryBreakdowns,+ List<AggregatedQueryLeafProfilerResult> aggregatedQueryLeafBreakdowns,+ List<QueryProfilerResult> childrenProfileResults,+ long startTime,+ long totalTime) {this.type = type;this.description = description;- this.breakdown = Objects.requireNonNull(breakdown, "required breakdown argument missing");- this.children = children == null ? Collections.emptyList() : children;+ this.queryBreakdowns =+ Objects.requireNonNull(queryBreakdowns, "required breakdown argument missing");+ this.aggregatedQueryLeafBreakdowns =+ Objects.requireNonNull(+ aggregatedQueryLeafBreakdowns, "required slice breakdowns argument missing");+ this.childrenProfileResults =+ childrenProfileResults == null ? Collections.emptyList() : childrenProfileResults;+ this.startTime = startTime;this.totalTime = totalTime;}- /** Retrieve the lucene description of this query (e.g. the "explain" text) */- public String getDescription() {- return description;- }-/** Retrieve the name of the entry (e.g. "TermQuery" or "LongTermsAggregator") */public String getQueryName() {return type;}+ /** Retrieve the lucene description of this query (e.g. the "explain" text) */+ public String getDescription() {+ return description;+ }+/** The timing breakdown for this node. */public Map<String, Long> getTimeBreakdown() {- return Collections.unmodifiableMap(breakdown);+ return Collections.unmodifiableMap(queryBreakdowns);+ }++ public long getStartTime() {+ return startTime;}/**@@ -75,8 +88,12 @@ public class QueryProfilerResult {return totalTime;}+ public List<AggregatedQueryLeafProfilerResult> getAggregatedQueryLeafBreakdowns() {+ return Collections.unmodifiableList(aggregatedQueryLeafBreakdowns);+ }+/** Returns a list of all profiled children queries */public List<QueryProfilerResult> getProfiledChildren() {- return Collections.unmodifiableList(children);+ return Collections.unmodifiableList(childrenProfileResults);}}diff --git a/lucene/sandbox/src/java/org/apache/lucene/sandbox/search/QueryProfilerTimer.java b/lucene/sandbox/src/java/org/apache/lucene/sandbox/search/QueryProfilerTimer.javaindex 1d2784f4cca..ed364ca4288 100644--- a/lucene/sandbox/src/java/org/apache/lucene/sandbox/search/QueryProfilerTimer.java+++ b/lucene/sandbox/src/java/org/apache/lucene/sandbox/search/QueryProfilerTimer.java@@ -33,9 +33,21 @@ package org.apache.lucene.sandbox.search;* </pre>*/class QueryProfilerTimer {-private boolean doTiming;- private long timing, count, lastCount, start;+ private long timing, count, lastCount, start, earliestTimerStartTime;++ public QueryProfilerTimer() {+ this(0, 0, 0, 0, 0);+ }++ public QueryProfilerTimer(+ long timing, long count, long lastCount, long start, long earliestTimerStartTime) {+ this.timing = timing;+ this.count = count;+ this.lastCount = lastCount;+ this.start = start;+ this.earliestTimerStartTime = earliestTimerStartTime;+ }/** pkg-private for testing */long nanoTime() {@@ -55,6 +67,9 @@ class QueryProfilerTimer {doTiming = (count - lastCount) >= Math.min(lastCount >>> 8, 1024);if (doTiming) {start = nanoTime();+ if (count == 0) {+ earliestTimerStartTime = start;+ }}count++;}@@ -76,6 +91,14 @@ class QueryProfilerTimer {return count;}+ /** Return the timer start time in nanoseconds. */+ public final long getEarliestTimerStartTime() {+ if (start != 0) {+ throw new IllegalStateException("#start call misses a matching #stop call");+ }+ return earliestTimerStartTime;+ }+/*** Return an approximation of the total time spent between consecutive calls of #start and #stop.*/diff --git a/lucene/sandbox/src/java/org/apache/lucene/sandbox/search/QueryProfilerTimingType.java b/lucene/sandbox/src/java/org/apache/lucene/sandbox/search/QueryProfilerTimingType.javaindex f358b66cce2..8845fe6381a 100644--- a/lucene/sandbox/src/java/org/apache/lucene/sandbox/search/QueryProfilerTimingType.java+++ b/lucene/sandbox/src/java/org/apache/lucene/sandbox/search/QueryProfilerTimingType.java@@ -21,16 +21,30 @@ import java.util.Locale;/** This enum breaks down the query into different sections to describe what was timed. */public enum QueryProfilerTimingType {- CREATE_WEIGHT,- COUNT,- BUILD_SCORER,- NEXT_DOC,- ADVANCE,- MATCH,- SCORE,- SHALLOW_ADVANCE,- COMPUTE_MAX_SCORE,- SET_MIN_COMPETITIVE_SCORE;+ COUNT(true),+ BUILD_SCORER(true),+ NEXT_DOC(true),+ ADVANCE(true),+ MATCH(true),+ SCORE(true),+ SHALLOW_ADVANCE(true),+ COMPUTE_MAX_SCORE(true),+ SET_MIN_COMPETITIVE_SCORE(true),++ // IMPORTANT: Global timer types must be defined after all the+ // slice level timers to preserve the contiguous enum ordinals+ CREATE_WEIGHT(false);++ private boolean leafLevel;++ private QueryProfilerTimingType(boolean leafLevel) {+ this.leafLevel = leafLevel;+ }++ /** Indicates if the operation runs on a LeafReader as opposed to the top-level IndexReader */+ public boolean isLeafLevel() {+ return leafLevel;+ }@Overridepublic String toString() {diff --git a/lucene/sandbox/src/java/org/apache/lucene/sandbox/search/QueryProfilerTree.java b/lucene/sandbox/src/java/org/apache/lucene/sandbox/search/QueryProfilerTree.javaindex 83335e5334b..236e0452340 100644--- a/lucene/sandbox/src/java/org/apache/lucene/sandbox/search/QueryProfilerTree.java+++ b/lucene/sandbox/src/java/org/apache/lucene/sandbox/search/QueryProfilerTree.java@@ -141,10 +141,15 @@ class QueryProfilerTree {* @return a hierarchical representation of the profiled query tree*/public List<QueryProfilerResult> getTree() {- ArrayList<QueryProfilerResult> results = new ArrayList<>(roots.size());+ // The profiler tree is shared at query level and we need+ // to recursively collect all the children for single slice+ // Another way could be to show the slice level breakdown at+ // each query level for each child, which looks less intuitive+ final List<QueryProfilerResult> results = new ArrayList<>(roots.size());for (IntCursor root : roots) {results.add(doGetTree(root.value));}+return results;}@@ -156,6 +161,7 @@ class QueryProfilerTree {*/private QueryProfilerResult doGetTree(int token) {Query query = queries.get(token);+ // Can query slice profiler breakdown be null??QueryProfilerBreakdown breakdown = breakdowns.get(token);IntArrayList children = tree.get(token);List<QueryProfilerResult> childrenProfileResults = Collections.emptyList();@@ -170,27 +176,7 @@ class QueryProfilerTree {// TODO this would be better done bottom-up instead of top-down to avoid// calculating the same times over and over...but worth the effort?- String type = getTypeFromQuery(query);- String description = getDescriptionFromQuery(query);- return new QueryProfilerResult(- type,- description,- breakdown.toBreakdownMap(),- breakdown.toTotalTime(),- childrenProfileResults);- }-- private String getTypeFromQuery(Query query) {- // Anonymous classes won't have a name,- // we need to get the super class- if (query.getClass().getSimpleName().isEmpty()) {- return query.getClass().getSuperclass().getSimpleName();- }- return query.getClass().getSimpleName();- }-- private String getDescriptionFromQuery(Query query) {- return query.toString();+ return breakdown.getQueryProfilerResult(query, childrenProfileResults);}/**diff --git a/lucene/sandbox/src/test/org/apache/lucene/sandbox/search/TestQueryProfilerIndexSearcher.java b/lucene/sandbox/src/test/org/apache/lucene/sandbox/search/TestQueryProfilerIndexSearcher.javaindex 36e9a839fcf..ce815dca6a3 100644--- a/lucene/sandbox/src/test/org/apache/lucene/sandbox/search/TestQueryProfilerIndexSearcher.java+++ b/lucene/sandbox/src/test/org/apache/lucene/sandbox/search/TestQueryProfilerIndexSearcher.java@@ -81,8 +81,18 @@ public class TestQueryProfilerIndexSearcher extends LuceneTestCase {List<QueryProfilerResult> results = searcher.getProfileResult();assertEquals(1, results.size());Map<String, Long> breakdown = results.get(0).getTimeBreakdown();+ // Validate query level breakdowns firstMatcherAssert.assertThat(breakdown.get(QueryProfilerTimingType.CREATE_WEIGHT.toString()), greaterThan(0L));+ MatcherAssert.assertThat(+ breakdown.get(QueryProfilerTimingType.CREATE_WEIGHT.toString() + "_count"),+ greaterThan(0L));++ // Validate thread level breakdowns now+ List<AggregatedQueryLeafProfilerResult> queryThreadProfilerResults =+ results.get(0).getAggregatedQueryLeafBreakdowns();+ assertEquals(1, queryThreadProfilerResults.size());+ breakdown = queryThreadProfilerResults.get(0).getTimeBreakdown();MatcherAssert.assertThat(breakdown.get(QueryProfilerTimingType.COUNT.toString()), equalTo(0L));MatcherAssert.assertThat(breakdown.get(QueryProfilerTimingType.BUILD_SCORER.toString()), greaterThan(0L));@@ -94,9 +104,6 @@ public class TestQueryProfilerIndexSearcher extends LuceneTestCase {breakdown.get(QueryProfilerTimingType.SCORE.toString()), greaterThan(0L));MatcherAssert.assertThat(breakdown.get(QueryProfilerTimingType.MATCH.toString()), equalTo(0L));- MatcherAssert.assertThat(- breakdown.get(QueryProfilerTimingType.CREATE_WEIGHT.toString() + "_count"),- greaterThan(0L));MatcherAssert.assertThat(breakdown.get(QueryProfilerTimingType.COUNT.toString() + "_count"), equalTo(0L));MatcherAssert.assertThat(@@ -157,8 +164,20 @@ public class TestQueryProfilerIndexSearcher extends LuceneTestCase {List<QueryProfilerResult> results = searcher.getProfileResult();assertEquals(1, results.size());Map<String, Long> breakdown = results.get(0).getTimeBreakdown();++ // Validate query level breakdowns firstMatcherAssert.assertThat(breakdown.get(QueryProfilerTimingType.CREATE_WEIGHT.toString()), greaterThan(0L));+ MatcherAssert.assertThat(+ breakdown.get(QueryProfilerTimingType.CREATE_WEIGHT.toString() + "_count"),+ greaterThan(0L));++ // Validate thread level breakdowns now+ List<AggregatedQueryLeafProfilerResult> queryThreadProfilerResults =+ results.get(0).getAggregatedQueryLeafBreakdowns();+ assertEquals(1, queryThreadProfilerResults.size());+ breakdown = queryThreadProfilerResults.get(0).getTimeBreakdown();+MatcherAssert.assertThat(breakdown.get(QueryProfilerTimingType.COUNT.toString()), equalTo(0L));MatcherAssert.assertThat(breakdown.get(QueryProfilerTimingType.BUILD_SCORER.toString()), greaterThan(0L));@@ -169,9 +188,6 @@ public class TestQueryProfilerIndexSearcher extends LuceneTestCase {MatcherAssert.assertThat(breakdown.get(QueryProfilerTimingType.SCORE.toString()), equalTo(0L));MatcherAssert.assertThat(breakdown.get(QueryProfilerTimingType.MATCH.toString()), equalTo(0L));- MatcherAssert.assertThat(- breakdown.get(QueryProfilerTimingType.CREATE_WEIGHT.toString() + "_count"),- greaterThan(0L));MatcherAssert.assertThat(breakdown.get(QueryProfilerTimingType.COUNT.toString() + "_count"), equalTo(0L));MatcherAssert.assertThat(@@ -197,8 +213,20 @@ public class TestQueryProfilerIndexSearcher extends LuceneTestCase {List<QueryProfilerResult> results = searcher.getProfileResult();assertEquals(1, results.size());Map<String, Long> breakdown = results.get(0).getTimeBreakdown();++ // Validate query level breakdowns firstMatcherAssert.assertThat(breakdown.get(QueryProfilerTimingType.CREATE_WEIGHT.toString()), greaterThan(0L));+ MatcherAssert.assertThat(+ breakdown.get(QueryProfilerTimingType.CREATE_WEIGHT.toString() + "_count"),+ greaterThan(0L));++ // Validate thread level breakdowns now+ List<AggregatedQueryLeafProfilerResult> queryThreadProfilerResults =+ results.get(0).getAggregatedQueryLeafBreakdowns();+ assertEquals(1, queryThreadProfilerResults.size());+ breakdown = queryThreadProfilerResults.get(0).getTimeBreakdown();+MatcherAssert.assertThat(breakdown.get(QueryProfilerTimingType.COUNT.toString()), greaterThan(0L));MatcherAssert.assertThat(@@ -210,9 +238,6 @@ public class TestQueryProfilerIndexSearcher extends LuceneTestCase {MatcherAssert.assertThat(breakdown.get(QueryProfilerTimingType.SCORE.toString()), equalTo(0L));MatcherAssert.assertThat(breakdown.get(QueryProfilerTimingType.MATCH.toString()), equalTo(0L));- MatcherAssert.assertThat(- breakdown.get(QueryProfilerTimingType.CREATE_WEIGHT.toString() + "_count"),- greaterThan(0L));MatcherAssert.assertThat(breakdown.get(QueryProfilerTimingType.COUNT.toString() + "_count"), greaterThan(0L));MatcherAssert.assertThat(@@ -237,8 +262,20 @@ public class TestQueryProfilerIndexSearcher extends LuceneTestCase {List<QueryProfilerResult> results = searcher.getProfileResult();assertEquals(1, results.size());Map<String, Long> breakdown = results.get(0).getTimeBreakdown();++ // Validate query level breakdowns firstMatcherAssert.assertThat(breakdown.get(QueryProfilerTimingType.CREATE_WEIGHT.toString()), greaterThan(0L));+ MatcherAssert.assertThat(+ breakdown.get(QueryProfilerTimingType.CREATE_WEIGHT.toString() + "_count"),+ greaterThan(0L));++ // Validate thread level breakdowns now+ List<AggregatedQueryLeafProfilerResult> queryThreadProfilerResults =+ results.get(0).getAggregatedQueryLeafBreakdowns();+ assertEquals(1, queryThreadProfilerResults.size());+ breakdown = queryThreadProfilerResults.get(0).getTimeBreakdown();+MatcherAssert.assertThat(breakdown.get(QueryProfilerTimingType.COUNT.toString()), greaterThan(0L));MatcherAssert.assertThat(@@ -251,9 +288,6 @@ public class TestQueryProfilerIndexSearcher extends LuceneTestCase {MatcherAssert.assertThat(breakdown.get(QueryProfilerTimingType.MATCH.toString()), greaterThan(0L));- MatcherAssert.assertThat(- breakdown.get(QueryProfilerTimingType.CREATE_WEIGHT.toString() + "_count"),- greaterThan(0L));MatcherAssert.assertThat(breakdown.get(QueryProfilerTimingType.COUNT.toString() + "_count"), greaterThan(0L));MatcherAssert.assertThat(diff --git a/lucene/sandbox/src/test/org/apache/lucene/sandbox/search/TestQueryProfilerTimer.java b/lucene/sandbox/src/test/org/apache/lucene/sandbox/search/TestQueryProfilerTimer.javanew file mode 100644index 00000000000..b81c33f9b59--- /dev/null+++ b/lucene/sandbox/src/test/org/apache/lucene/sandbox/search/TestQueryProfilerTimer.java@@ -0,0 +1,71 @@+/*+ * 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.sandbox.search;++import java.util.concurrent.atomic.AtomicLong;+import org.apache.lucene.tests.util.LuceneTestCase;++public class TestQueryProfilerTimer extends LuceneTestCase {+ public void testTimingInterval() {+ final AtomicLong nanoTimeCallCounter = new AtomicLong();+ QueryProfilerTimer t =+ new QueryProfilerTimer() {+ long time = 50;++ @Override+ long nanoTime() {+ nanoTimeCallCounter.incrementAndGet();+ return time += 1;+ }+ };+ for (int i = 0; i < 100000; ++i) {+ t.start();+ t.stop();+ if (i < 256) {+ // for the first 256 calls, nanoTime() is called+ // once for `start` and once for `stop`+ assertEquals((i + 1) * 2, nanoTimeCallCounter.get());+ }+ }+ // only called nanoTime() 3356 times, which is significantly less than 100000+ assertEquals(3356L, nanoTimeCallCounter.get());+ }++ public void testExtrapolate() {+ QueryProfilerTimer t =+ new QueryProfilerTimer() {+ long time = 50;++ @Override+ long nanoTime() {+ return time += 42;+ }+ };+ t.start();+ t.stop();+ long timerStartTime = t.getEarliestTimerStartTime();+ for (int i = 2; i < 100000; ++i) {+ t.start();+ t.stop();+ assertEquals(i, t.getCount());+ assertEquals(timerStartTime, t.getEarliestTimerStartTime());+ // Make sure the cumulated timing is 42 times the number of calls as expected+ assertEquals(i * 42L, t.getApproximateTiming());+ }+ }+}