Skip to content

Commit 10cb3ee

Browse files
authored
Random overhead improvements (#15327)
* Fix the code so that testing with -Ptests.asserts=false passes. * More tests that don't work with assertions disabled. * Pick tests.asserts randomly based on the main seed. Use assertions 75% of the time. * Changes entry. * Clean up duplicate assertion-verifying code. * Add a new build option tests.random.maxcalls for debugging too many calls to the asserting Random returned from LuceneTestCase.random(). Added LuceneTestCase.nonAssertingRandom(Random) to acquire a fast, non-asserting random instance for intense local computation. Modified a bunch of tests that exceeded 1 million calls to random() result. * Added another option for low-level debugging of intense random() calls. tests.random.maxacquires will terminate if too many random() calls are called from within a single test. Corrected a few tests where this was the case. * Tidy and correct a debugging check in LuceneTestCase. * Final minor touches. * Changes entry. * Changes entry.
1 parent b44f550 commit 10cb3ee

File tree

34 files changed

+321
-164
lines changed

34 files changed

+321
-164
lines changed

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,15 @@ public void apply(Project project) {
221221
buildOptions.addIntOption("tests.timeoutSuite", "Timeout (in millis) for an entire suite.");
222222
optionsInheritedAsProperties.add("tests.timeoutSuite");
223223

224+
buildOptions.addIntOption(
225+
"tests.random.maxcalls",
226+
"Max number of calls to Randoms returned by LuceneTestCase.random()");
227+
optionsInheritedAsProperties.add("tests.random.maxcalls");
228+
229+
buildOptions.addIntOption(
230+
"tests.random.maxacquires", "Max number of per-test calls to LuceneTestCase.random()");
231+
optionsInheritedAsProperties.add("tests.random.maxacquires");
232+
224233
Provider<Boolean> assertsOption =
225234
buildOptions.addBooleanOption(
226235
"tests.asserts",

lucene/CHANGES.txt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,6 @@ Improvements
6363
This avoids clashes with other ASM versions on classpath because it removes the dependency.
6464
(Uwe Schindler)
6565

66-
67-
6866
* GITHUB#15002: Remove synchronized WeakHashMap from IndexReader. This was added to help prevent
6967
SIGSEGV with the old unsafe "mmap hack", which has been replaced by MemorySegments.
7068
(Uwe Schindler, Robert Muir)
@@ -106,7 +104,11 @@ Changes in Runtime Behavior
106104

107105
Build
108106
---------------------
109-
* GITHUB#15325: Run the tests with assertions disabled ~25% of the time #15325 (Dawid Weiss)
107+
* GITHUB#15327: New low-level build options to detect abuse of LuceneTestCase.random():
108+
tests.random.maxacquires and tests.random.maxcalls (Robert Muir, Dawid Weiss)
109+
110+
* GITHUB#15325: Run the tests with assertions disabled ~25% of the time #15325
111+
(Robert Muir, Dawid Weiss)
110112

111113
* GITHUB#14804: Detect and ban wildcard imports in Java (Robert Muir, Dawid Weiss)
112114

lucene/analysis/common/src/test/org/apache/lucene/analysis/util/TestRollingCharBuffer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public void test() throws Exception {
2828

2929
RollingCharBuffer buffer = new RollingCharBuffer();
3030

31-
Random random = random();
31+
Random random = nonAssertingRandom(random());
3232
for (int iter = 0; iter < ITERS; iter++) {
3333
final int stringLen = random.nextBoolean() ? random.nextInt(50) : random.nextInt(20000);
3434
final String s;

lucene/backward-codecs/src/test/org/apache/lucene/backward_codecs/compressing/AbstractTestCompressionMode.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ static byte[] randomArray(Random random) {
3939
}
4040

4141
static byte[] randomArray(Random random, int length, int max) {
42+
random = nonAssertingRandom(random);
4243
final byte[] arr = new byte[length];
4344
for (int i = 0; i < arr.length; ++i) {
4445
arr[i] = (byte) RandomNumbers.randomIntBetween(random, 0, max);
@@ -83,7 +84,7 @@ byte[] decompress(byte[] compressed, int originalLength, int offset, int length)
8384
}
8485

8586
public void testDecompress() throws IOException {
86-
Random random = random();
87+
Random random = nonAssertingRandom(random());
8788
final int iterations = atLeast(random, 3);
8889
for (int i = 0; i < iterations; ++i) {
8990
final byte[] decompressed = randomArray(random);
@@ -99,7 +100,7 @@ public void testDecompress() throws IOException {
99100
}
100101

101102
public void testPartialDecompress() throws IOException {
102-
Random random = random();
103+
Random random = nonAssertingRandom(random());
103104
final int iterations = atLeast(random, 3);
104105
for (int i = 0; i < iterations; ++i) {
105106
final byte[] decompressed = randomArray(random);

lucene/classification/src/test/org/apache/lucene/classification/utils/TestDataSplitter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public void setUp() throws Exception {
6363
ft.setStoreTermVectorPositions(true);
6464

6565
Document doc;
66-
Random rnd = random();
66+
Random rnd = nonAssertingRandom(random());
6767
int numDocs = atLeast(100);
6868
for (int i = 0; i < numDocs; i++) {
6969
doc = new Document();

lucene/core/src/test/org/apache/lucene/analysis/standard/TestStandardAnalyzer.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,19 +76,20 @@ public void testLargePartiallyMatchingToken() throws Exception {
7676
};
7777

7878
StringBuilder builder = new StringBuilder();
79-
int numChars = TestUtil.nextInt(random(), 100 * 1024, 1024 * 1024);
79+
var rnd = nonAssertingRandom(random());
80+
int numChars = TestUtil.nextInt(rnd, 100 * 1024, 1024 * 1024);
8081
for (int i = 0; i < numChars; ) {
8182
builder.append(
82-
WordBreak_ExtendNumLet_chars[random().nextInt(WordBreak_ExtendNumLet_chars.length)]);
83+
WordBreak_ExtendNumLet_chars[rnd.nextInt(WordBreak_ExtendNumLet_chars.length)]);
8384
++i;
84-
if (random().nextBoolean()) {
85-
int numFormatExtendChars = TestUtil.nextInt(random(), 1, 8);
85+
if (rnd.nextBoolean()) {
86+
int numFormatExtendChars = TestUtil.nextInt(rnd, 1, 8);
8687
for (int j = 0; j < numFormatExtendChars; ++j) {
8788
int codepoint;
88-
if (random().nextBoolean()) {
89-
codepoint = WordBreak_Format_chars[random().nextInt(WordBreak_Format_chars.length)];
89+
if (rnd.nextBoolean()) {
90+
codepoint = WordBreak_Format_chars[rnd.nextInt(WordBreak_Format_chars.length)];
9091
} else {
91-
codepoint = WordBreak_Extend_chars[random().nextInt(WordBreak_Extend_chars.length)];
92+
codepoint = WordBreak_Extend_chars[rnd.nextInt(WordBreak_Extend_chars.length)];
9293
}
9394
char[] chars = Character.toChars(codepoint);
9495
builder.append(chars);

lucene/core/src/test/org/apache/lucene/codecs/lucene103/blocktree/TestTrie.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,9 +194,10 @@ private void testTrieLookup(Supplier<byte[]> randomBytesSupplier, int round) thr
194194
}
195195

196196
private static byte[] randomBytes() {
197-
byte[] bytes = new byte[random().nextInt(256) + 1];
197+
var random = nonAssertingRandom(random());
198+
byte[] bytes = new byte[random.nextInt(256) + 1];
198199
for (int i = 1; i < bytes.length; i++) {
199-
bytes[i] = (byte) random().nextInt(1 << (i % 9));
200+
bytes[i] = (byte) random.nextInt(1 << (i % 9));
200201
}
201202
return bytes;
202203
}

lucene/core/src/test/org/apache/lucene/geo/TestGeoEncodingUtils.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import static org.apache.lucene.geo.GeoUtils.MIN_LAT_INCL;
2828
import static org.apache.lucene.geo.GeoUtils.MIN_LON_INCL;
2929

30+
import com.carrotsearch.randomizedtesting.RandomizedTest;
3031
import java.util.Random;
3132
import org.apache.lucene.tests.util.LuceneTestCase;
3233
import org.apache.lucene.tests.util.TestUtil;
@@ -42,8 +43,8 @@ public class TestGeoEncodingUtils extends LuceneTestCase {
4243
*/
4344
public void testLatitudeQuantization() throws Exception {
4445
final double LATITUDE_DECODE = 180.0D / (0x1L << 32);
45-
Random random = random();
46-
for (int i = 0; i < 10000; i++) {
46+
Random random = nonAssertingRandom(random());
47+
for (int i = 0; i < RandomizedTest.randomIntBetween(1000, 10000); i++) {
4748
int encoded = random.nextInt();
4849
double min = MIN_LAT_INCL + (encoded - (long) Integer.MIN_VALUE) * LATITUDE_DECODE;
4950
double decoded = decodeLatitude(encoded);
@@ -92,8 +93,8 @@ public void testLatitudeQuantization() throws Exception {
9293
*/
9394
public void testLongitudeQuantization() throws Exception {
9495
final double LONGITUDE_DECODE = 360.0D / (0x1L << 32);
95-
Random random = random();
96-
for (int i = 0; i < 10000; i++) {
96+
Random random = nonAssertingRandom(random());
97+
for (int i = 0; i < RandomizedTest.randomIntBetween(1000, 10000); i++) {
9798
int encoded = random.nextInt();
9899
double min = MIN_LON_INCL + (encoded - (long) Integer.MIN_VALUE) * LONGITUDE_DECODE;
99100
double decoded = decodeLongitude(encoded);

lucene/core/src/test/org/apache/lucene/geo/TestXYRectangle.java

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -139,17 +139,19 @@ public void testEqualsAndHashCode() {
139139

140140
/** make sure that if a point is inside a circle, it is inside of the bbox as well */
141141
public void testRandomCircleToBBox() {
142+
var random = nonAssertingRandom(random());
143+
142144
int iters = atLeast(100);
143145
for (int iter = 0; iter < iters; iter++) {
144146

145-
float centerX = ShapeTestUtil.nextFloat(random());
146-
float centerY = ShapeTestUtil.nextFloat(random());
147+
float centerX = ShapeTestUtil.nextFloat(random);
148+
float centerY = ShapeTestUtil.nextFloat(random);
147149

148150
final float radius;
149-
if (random().nextBoolean()) {
150-
radius = random().nextFloat() * TestUtil.nextInt(random(), 1, 100000);
151+
if (random.nextBoolean()) {
152+
radius = random.nextFloat() * TestUtil.nextInt(random, 1, 100000);
151153
} else {
152-
radius = Math.abs(ShapeTestUtil.nextFloat(random()));
154+
radius = Math.abs(ShapeTestUtil.nextFloat(random));
153155
}
154156

155157
XYRectangle bbox = XYRectangle.fromPointDistance(centerX, centerY, radius);
@@ -159,16 +161,16 @@ public void testRandomCircleToBBox() {
159161
for (int i = 0; i < numPointsToTry; i++) {
160162

161163
double x;
162-
if (random().nextBoolean()) {
163-
x = Math.min(Float.MAX_VALUE, centerX + radius + random().nextDouble());
164+
if (random.nextBoolean()) {
165+
x = Math.min(Float.MAX_VALUE, centerX + radius + random.nextDouble());
164166
} else {
165-
x = Math.max(-Float.MAX_VALUE, centerX + radius - random().nextDouble());
167+
x = Math.max(-Float.MAX_VALUE, centerX + radius - random.nextDouble());
166168
}
167169
double y;
168-
if (random().nextBoolean()) {
169-
y = Math.min(Float.MAX_VALUE, centerY + radius + random().nextDouble());
170+
if (random.nextBoolean()) {
171+
y = Math.min(Float.MAX_VALUE, centerY + radius + random.nextDouble());
170172
} else {
171-
y = Math.max(-Float.MAX_VALUE, centerY + radius - random().nextDouble());
173+
y = Math.max(-Float.MAX_VALUE, centerY + radius - random.nextDouble());
172174
}
173175

174176
// cartesian says it's within the circle:

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,14 @@ private static DirectoryReader reader(Directory dir) throws IOException {
9393
private static <C extends Collector> Object collectAll(
9494
LeafReaderContext ctx, Collection<Integer> values, CollectorManager<C, ?> collectorManager)
9595
throws IOException {
96+
var rnd = nonAssertingRandom(random());
97+
9698
List<C> collectors = new ArrayList<>();
9799
C collector = collectorManager.newCollector();
98100
collectors.add(collector);
99101
LeafCollector leafCollector = collector.getLeafCollector(ctx);
100102
for (Integer v : values) {
101-
if (random().nextInt(10) == 1) {
103+
if (rnd.nextInt(10) == 1) {
102104
collector = collectorManager.newCollector();
103105
collectors.add(collector);
104106
leafCollector = collector.getLeafCollector(ctx);

0 commit comments

Comments
 (0)