-
Notifications
You must be signed in to change notification settings - Fork 24.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve test times for tests using RandomObjects::addFields
#31556
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -175,7 +175,7 @@ public static BytesReference randomSource(Random random, XContentType xContentTy | |
public static BytesReference randomSource(Random random, XContentType xContentType, int minNumFields) { | ||
try (XContentBuilder builder = XContentFactory.contentBuilder(xContentType)) { | ||
builder.startObject(); | ||
addFields(random, builder, minNumFields, 0); | ||
addFields(random, builder, minNumFields, 0, 0); | ||
builder.endObject(); | ||
return BytesReference.bytes(builder); | ||
} catch(IOException e) { | ||
|
@@ -185,14 +185,21 @@ public static BytesReference randomSource(Random random, XContentType xContentTy | |
|
||
/** | ||
* Randomly adds fields, objects, or arrays to the provided builder. The maximum depth is 5. | ||
* @return Returns the number of fields in the document | ||
*/ | ||
private static void addFields(Random random, XContentBuilder builder, int minNumFields, int currentDepth) throws IOException { | ||
int numFields = randomIntBetween(random, minNumFields, 10); | ||
private static int addFields(Random random, XContentBuilder builder, int minNumFields, int currentDepth, | ||
int currentFields) throws IOException { | ||
int maxTotalFields = 200; | ||
int maxFields = Math.max(minNumFields, 10 - (currentFields * 10)/maxTotalFields); // Map to range 0-10 | ||
int numFields = randomIntBetween(random, minNumFields, maxFields); | ||
int maxDepth = 5 - (currentFields * 5)/maxTotalFields; // Map to range 0-5 | ||
currentFields += numFields; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. with the goal of trying to keep things simple, I wonder if decreasing the max number of fields e.g. to 5 would be enough. What do you think? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could be. Will need to test this for the average case. The potentially maximum number of fields would still be more than 3000. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe we should also decrease the chance that we add another child object compared to leaf fields. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I didn't quite understand. We, already have a check on the depth which takes care of this? Or do you mean something else? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes we have a check, but I think we end up adding more objects and arrays than we need.
the above if (taken from addFields method) means that we add a new object or array if maxDepth allows, one out of two times. Maybe we want to lower that probability? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah okay. I will try to lower the probability based on current depth. What did you mean by leaf fields? The neighbours? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you can also do something similar to what LuceneTestCase#rarely does. we can do it regardless depth to keep things simple? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes. Okay. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @javanna I simplified it now. So that max fields is 5 and the probability is 30%. It seems to give similar results in terms of timing. |
||
for (int i = 0; i < numFields; i++) { | ||
if (currentDepth < 5 && random.nextBoolean()) { | ||
if (currentDepth < maxDepth && random.nextBoolean()) { | ||
if (random.nextBoolean()) { | ||
builder.startObject(RandomStrings.randomAsciiOfLengthBetween(random, 6, 10)); | ||
addFields(random, builder, minNumFields, currentDepth + 1); | ||
currentFields = | ||
addFields(random, builder, minNumFields, currentDepth + 1, currentFields); | ||
builder.endObject(); | ||
} else { | ||
builder.startArray(RandomStrings.randomAsciiOfLengthBetween(random, 6, 10)); | ||
|
@@ -205,7 +212,8 @@ private static void addFields(Random random, XContentBuilder builder, int minNum | |
for (int j = 0; j < numElements; j++) { | ||
if (object) { | ||
builder.startObject(); | ||
addFields(random, builder, minNumFields, 5); | ||
currentFields = | ||
addFields(random, builder, minNumFields, 5, currentFields); | ||
builder.endObject(); | ||
} else { | ||
builder.value(randomFieldValue(random, dataType)); | ||
|
@@ -215,9 +223,10 @@ private static void addFields(Random random, XContentBuilder builder, int minNum | |
} | ||
} else { | ||
builder.field(RandomStrings.randomAsciiOfLengthBetween(random, 6, 10), | ||
randomFieldValue(random, randomDataType(random))); | ||
randomFieldValue(random, randomDataType(random))); | ||
} | ||
} | ||
return currentFields; | ||
} | ||
|
||
private static int randomDataType(Random random) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure what value is good for this. From my results, 200 seemed to give decently big documents without going beyond 100KB.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think that we need such big docs, 100kb seems a lot still even if it's the upper bound.