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.
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 lamdba
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 Java lamdba
exec(http("name")
.get { session -> "/foo/${session.getString("param")!!.toLowerCase(Locale.getDefault())}" })
// passing a reference to a function
val f =
{ session: Session -> "/foo/${session.getString("param")!!.toLowerCase(Locale.getDefault())}" }
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.