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
.
/*
* Copyright 2011-2024 GatlingCorp (https://gatling.io)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import static io.gatling.javaapi.core.CoreDsl.*;
import static io.gatling.javaapi.http.HttpDsl.*;
import java.util.Locale;
import io.gatling.javaapi.core.*;
import java.util.function.Function;
class FunctionSampleJava {
{
//#function
// 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));
//#function
}
}
/*
* Copyright 2011-2024 GatlingCorp (https://gatling.io)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import java.util.Locale
import io.gatling.javaapi.core.*
import io.gatling.javaapi.core.CoreDsl.*
import io.gatling.javaapi.http.HttpDsl.*
class FunctionSampleKotlin {
init {
//#function
// 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))
//#function
}
}
/*
* Copyright 2011-2024 GatlingCorp (https://gatling.io)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import java.util.Locale
import io.gatling.core.Predef._
import io.gatling.core.session._
import io.gatling.http.Predef._
class FunctionSampleScala {
//#function
// 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));
//#function
}
(Scala Only): For more information about
Validation
, please check out the Validation reference.