Skip to content

Commit 6a1ff24

Browse files
dweissMichael Sokolov
andauthored
Speed up slow tests (#15212) (#15306)
* reduce numdocs in TestBPReorderingMergePolicy * Replace the testRemovalsAndInsertions with a test that compares against java's priority queue. Same net result. * Move TestAddTaxonomy.testBig to nightly tests. * Pass tests.verbose to tests. * Mark TestShardSearching as Nightly (slow). --------- Co-authored-by: Michael Sokolov <[email protected]>
1 parent fcbeefe commit 6a1ff24

File tree

4 files changed

+46
-53
lines changed

4 files changed

+46
-53
lines changed

build-tools/build-infra/src/main/java/org/apache/lucene/gradle/plugins/java/TestsAndRandomizationPlugin.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,8 @@ public void apply(Project project) {
165165
"tests.verbose",
166166
"Enables verbose test output mode (emits full test outputs immediately).",
167167
false);
168+
optionsInheritedAsProperties.add("tests.verbose");
169+
168170
Provider<Boolean> haltOnFailureOption =
169171
buildOptions.addBooleanOption(
170172
"tests.haltonfailure", "Stop processing on test failures.", true);

lucene/core/src/test/org/apache/lucene/search/TestShardSearching.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ public PreviousSearchState(
7070
}
7171
}
7272

73+
@Nightly // TODO: -Ptests.seed=A148FC8983CFA7A - takes 12 seconds. very slow. wall-clock dependent
7374
public void testSimple() throws Exception {
7475
final int numNodes = TestUtil.nextInt(random(), 1, 10);
7576

lucene/core/src/test/org/apache/lucene/util/TestPriorityQueue.java

Lines changed: 40 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@
1818

1919
import static org.hamcrest.Matchers.equalTo;
2020
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
21-
import static org.hamcrest.Matchers.lessThanOrEqualTo;
2221

22+
import com.carrotsearch.randomizedtesting.Xoroshiro128PlusRandom;
23+
import com.carrotsearch.randomizedtesting.generators.RandomNumbers;
2324
import java.util.ArrayList;
2425
import java.util.Collections;
26+
import java.util.HashMap;
2527
import java.util.Iterator;
2628
import java.util.List;
2729
import java.util.NoSuchElementException;
@@ -30,7 +32,6 @@
3032
import org.apache.lucene.tests.util.TestUtil;
3133
import org.hamcrest.Matchers;
3234

33-
@SuppressWarnings("BoxedPrimitiveEquality")
3435
public class TestPriorityQueue extends LuceneTestCase {
3536

3637
private static class IntegerQueue extends PriorityQueue<Integer> {
@@ -208,59 +209,48 @@ public void testAddAllDoesNotFitIntoQueue() {
208209
() -> pq.addAll(list));
209210
}
210211

211-
// TODO: incredibly slow
212-
@Nightly
212+
/** Randomly add and remove elements, comparing against the reference java.util.PriorityQueue. */
213213
public void testRemovalsAndInsertions() {
214-
Random random = random();
215-
int numDocsInPQ = TestUtil.nextInt(random, 1, 100);
216-
IntegerQueue pq = new IntegerQueue(numDocsInPQ);
217-
Integer lastLeast = null;
218-
219-
// Basic insertion of new content
220-
ArrayList<Integer> sds = new ArrayList<>(numDocsInPQ);
221-
for (int i = 0; i < numDocsInPQ * 10; i++) {
222-
Integer newEntry = Math.abs(random.nextInt());
223-
sds.add(newEntry);
224-
Integer evicted = pq.insertWithOverflow(newEntry);
225-
pq.checkValidity();
226-
if (evicted != null) {
227-
assertTrue(sds.remove(evicted));
228-
if (evicted != newEntry) {
229-
assertSame(evicted, lastLeast);
214+
int maxElement = RandomNumbers.randomIntBetween(random(), 1, 10_000);
215+
int size = maxElement / 2 + 1;
216+
217+
var reference = new java.util.PriorityQueue<Integer>();
218+
var pq = new IntegerQueue(size);
219+
220+
Random localRandom = new Xoroshiro128PlusRandom(random().nextLong());
221+
222+
// Lucene's PriorityQueue.remove uses reference equality, not .equals to determine which
223+
// elements
224+
// to remove (!).
225+
HashMap<Integer, Integer> ints = new HashMap<>();
226+
227+
for (int i = 0, iters = size * 2; i < iters; i++) {
228+
Integer element = ints.computeIfAbsent(localRandom.nextInt(maxElement), k -> k);
229+
230+
var action = localRandom.nextInt(100);
231+
if (action < 25) {
232+
// removals, possibly misses.
233+
assertEquals("remove() difference: " + i, reference.remove(element), pq.remove(element));
234+
} else {
235+
// additions.
236+
var dropped = pq.insertWithOverflow(element);
237+
238+
reference.add(element);
239+
Integer droppedReference;
240+
if (reference.size() > size) {
241+
droppedReference = reference.remove();
242+
} else {
243+
droppedReference = null;
230244
}
231-
}
232-
Integer newLeast = pq.top();
233-
if ((lastLeast != null) && (newLeast != newEntry) && (newLeast != lastLeast)) {
234-
// If there has been a change of least entry and it wasn't our new
235-
// addition we expect the scores to increase
236-
assertThat(newLeast, lessThanOrEqualTo(newEntry));
237-
assertThat(newLeast, greaterThanOrEqualTo(lastLeast));
238-
}
239-
lastLeast = newLeast;
240-
}
241245

242-
// Try many random additions to existing entries - we should always see
243-
// increasing scores in the lowest entry in the PQ
244-
for (int p = 0; p < 500000; p++) {
245-
int element = (int) (random.nextFloat() * (sds.size() - 1));
246-
Integer objectToRemove = sds.get(element);
247-
assertSame(sds.remove(element), objectToRemove);
248-
assertTrue(pq.remove(objectToRemove));
249-
pq.checkValidity();
250-
Integer newEntry = Math.abs(random.nextInt());
251-
sds.add(newEntry);
252-
assertNull(pq.insertWithOverflow(newEntry));
253-
pq.checkValidity();
254-
Integer newLeast = pq.top();
255-
if ((objectToRemove != lastLeast) && (lastLeast != null) && (newLeast != newEntry)) {
256-
// If there has been a change of least entry and it wasn't our new
257-
// addition or the loss of our randomly removed entry we expect the
258-
// scores to increase
259-
assertThat(newLeast, lessThanOrEqualTo(newEntry));
260-
assertThat(newLeast, greaterThanOrEqualTo(lastLeast));
246+
assertEquals("insertWithOverflow() difference.", dropped, droppedReference);
261247
}
262-
lastLeast = newLeast;
248+
249+
assertEquals("insertWithOverflow() size difference?", reference.size(), pq.size());
250+
assertEquals("top() difference?", reference.peek(), pq.top());
263251
}
252+
253+
pq.checkValidity();
264254
}
265255

266256
public void testIteratorEmpty() {

lucene/misc/src/test/org/apache/lucene/misc/index/TestBPReorderingMergePolicy.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public void testReorderOnMerge() throws IOException {
7575
KnnFloatVectorField vectorField = new KnnFloatVectorField("vector", new float[] {0});
7676
doc.add(vectorField);
7777

78-
for (int i = 0; i < 10000; ++i) {
78+
for (int i = 0; i < 1000; ++i) {
7979
idField.setStringValue(Integer.toString(i));
8080
int intValue = i % 2 == 0 ? 0 : i % 10;
8181
bodyField.setStringValue(Integer.toString(intValue));
@@ -158,7 +158,7 @@ public void testReorderOnAddIndexes() throws IOException {
158158
KnnFloatVectorField vectorField = new KnnFloatVectorField("vector", new float[] {0});
159159
doc.add(vectorField);
160160

161-
for (int i = 0; i < 10000; ++i) {
161+
for (int i = 0; i < 1000; ++i) {
162162
idField.setStringValue(Integer.toString(i));
163163
int intValue = i % 2 == 0 ? 0 : i % 10;
164164
bodyField.setStringValue(Integer.toString(intValue));
@@ -171,7 +171,7 @@ public void testReorderOnAddIndexes() throws IOException {
171171
}
172172
}
173173

174-
for (int i = 0; i < 10000; i += 10) {
174+
for (int i = 0; i < 1000; i += 10) {
175175
w1.deleteDocuments(new Term("id", Integer.toString(i / 3)));
176176
}
177177

0 commit comments

Comments
 (0)