I found an out-of-date example of Gatling Java code as follows (intended to log out 10% of the time)
public static ChainBuilder logout =
randomSwitch().on(
Choice.withWeight(10, exec(
http(“Logout”).post(“/logout”)
Choice.withWeight no longer is valid. Searching the Gatling documentation, I viewed the 3.11 release notes, which said " Drop deprecated Choice#withKey and Choice#withWeight, use onCase and percent instead"
However, I must be doing something incorrectly when searching the Java DSL. I could not find any Gatling documentation showing how to properly use onCase along with percent.
I ended up finding a working example on a different web site that used randomSwitch.
Not sure if this is the proper way to implement percent or if there is a cleaner way to create a ChainBuilder instance using onCase(percent(10.0).then(exec …
I would appreciate learning the right way to search and view the entire Java DSL to find examples. Can you share any tips / techniques on how to leverage the documentation?
Search for “randomSwitch” → returns link to scenario randomSwitch and includes example of using percent.
Search for “percent” → nothing found in the DSL Java documentation.
Search for “onCase” → nothing found in the DSL Java documentation.
Is this the cleanest way to implement the logic when there we only have one request we want apply percent to:
public static ChainBuilder logout =
randomSwitch().on(
percent(10.0).then(exec(
http(“Logout”).post(“/logout”)
Code samples are not indexed. doSwitch and randomSwitch are a bit of a special case where all other languages than Scala need an extra method as they don’t have nice native support for couples.
I’m not sure how we can improve things. cc @Shaun
Is this the cleanest way to implement the logic when there we only have one request we want apply percent to:
One documentation idea might be to offer paid Enterprise users to access a customized AI search that has a robust set of code examples to augment the search
Just for amusement, I tried Microsoft Copilot and it wildly missed the mark at first.
“generate a Java Chainbuilder for Gatling that runs a request 25% of the time”
First response was one that would work but is over-engineered:
ChainBuilder conditionalRequest = exec(session -> {
Random rand = new Random();
// Generate a value between 0 and 99. If less than 25, execute the request.
if (rand.nextInt(100) < 25) {
return session.set("shouldRun", true);
} else {
return session.set("shouldRun", false);
}
}).doIf("#{shouldRun}",
exec(http("25% Request")
.get("/your-endpoint-here"))
);
Second answer after additional prompt was close but not quite right:
After one more prompt it reverted to using Choice.withWeight which of course is no longer supported. Then with a final prompt it finally got things right.
ChainBuilder conditionalRequest =
randomSwitch().on(
// 25% chance to run request
percent(25).exec(
http("25% Chance")
.get("/your-endpoint-here")
),
// 75% or any other percentage, or can remove entirely if no other no action
percent(75).exec(
http("75% Chance")
.get("/your-other-endpoint-here")
),
// yet another percentage
percent(33).exec(
http("33% Chance")
.get("/your-additional-endpoint-here")
)
);
Hi @LeslieM, I help maintain the documentation at Gatling. Thanks for the feedback. I am working on some updates that should improve the search in cases like yours, but it might take a few days to notice a difference.
We will also investigate some AI-related enhancements after the summer.
Thanks for using Gatling and contributing to the community!