Functions
Use functions to programmatically generate dynamic parameters
Sometimes, you might want to dynamic parameters that are too complex to compute for Gatling EL. Most Gatling DSL methods can also be passed a function to compute your parameter value programmatically.
Those functions are executed in Gatling’s shared threads, so you must absolutely avoid performing long blocking operations in there, such as remote API calls.
Remember that the Gatling DSL components are merely definitions. They only are effective when chained with other DSL components and ultimately passed to the
setUp
. In particular, they have no effect when used inside functions.Syntax
Those functions always take a Session
parameter, so you can extract previously stored data.
The generic signature of these functions is:
- In Java and Kotlin:
Session -> T
- In Scala:
Expression[T]
is an alias forSession => Validation[T]
. Values can implicitly lifted inValidation
.
// inline usage with a Java lambda
exec(http("name")
.get(session -> "/foo/" + session.getString("param").toLowerCase(Locale.getDefault())));
// passing a reference to a function
Function<Session, String> f =
session -> "/foo/" + session.getString("param").toLowerCase(Locale.getDefault());
exec(http("name").get(f));
// inline usage with a lambda
exec(http("name")
.get((session) => "/foo/" + session.get("param").toLocaleLowerCase()));
// passing a reference to a function
const f =
(session) => "/foo/" + session.get("param").toLocaleLowerCase();
exec(http("name").get(f));
// inline usage with a Kotlin lambda
exec(http("name")
.get { session -> "/foo/${session.getString("param")!!.lowercase()}" })
// passing a reference to a function
val f =
{ session: Session -> "/foo/${session.getString("param")!!.lowercase()}" }
exec(http("name").get(f))
// inline usage with an anonymous function
exec(http("name")
.get(session => s"/foo/${session("param").as[String].toLowerCase(Locale.getDefault)}"))
// passing a reference to a function
val f: Expression[String] =
session => s"/foo/${session("param").as[String].toLowerCase(Locale.getDefault)}"
exec(http("name").get(f));
(Scala Only): For more information about
Validation
, please check out the Validation reference.