Scenario

Learn how to execute requests, pauses, loops and conditions

This is the reference of the different components available to write scenarios with Gatling.

Bootstrapping

scenario is the way to bootstrap a new scenario.

     
ScenarioBuilder scn = scenario("Scenario");
val scn = scenario("Scenario")
val scn = scenario("Scenario")

You can use any character in the name of the scenario except tabulations: \t.

Structure Elements

All the components in this section can be either:

  • attached to a scenario
  • directly created, so they can be passed as parameters, stored in constants, etc
  • attached to another component in this section

Exec

The exec method is used to execute an action. Actions are usually requests (HTTP, LDAP, POP, IMAP, etc) that will be sent during the simulation. Any action that will be executed will be called with exec.

For example, when using the Gatling HTTP module you would write the following line:

     
// attached to a scenario
scenario("Scenario")
  .exec(http("Home").get("https://gatling.io"));

// directly created and stored in a reference
ChainBuilder chain = exec(http("Home").get("https://gatling.io"));

// attached to another
exec(http("Home").get("https://gatling.io"))
  .exec(http("Enterprise").get("https://gatling.io/enterprise"));
// attached to a scenario
scenario("Scenario")
  .exec(http("Home").get("https://gatling.io"))

// directly created and stored in a reference
val chain = exec(http("Home").get("https://gatling.io"))

// attached to another
exec(http("Home").get("https://gatling.io"))
  .exec(http("Enterprise").get("https://gatling.io/enterprise"))
// attached to a scenario
scenario("Scenario")
  .exec(http("Home").get("https://gatling.io"))

// directly created and stored in a reference
val chain = exec(http("Home").get("https://gatling.io"))

// attached to another
exec(http("Home").get("https://gatling.io"))
  .exec(http("Enterprise").get("https://gatling.io/enterprise"))

exec can also be passed a Function.

This can be used for manual debugging or to edit the Session, e.g.:

     
exec(session -> {
  // displays the content of the session in the console (debugging only)
  System.out.println(session);
  // return the original session
  return session;
});

exec(session ->
  // return a new session instance
  // with a new "foo" attribute whose value is "bar"
  session.set("foo", "bar")
);
exec { session ->
  // displays the content of the session in the console (debugging only)
  println(session)
  // return the original session
  session
}

exec { session ->
  // return a new session instance
  // with a new "foo" attribute whose value is "bar"
  session.set("foo", "bar")
}
exec { session =>
  // displays the content of the session in the console (debugging only)
  println(session)
  // return the original session
  session
}

exec { session =>
  // return a new session instance
  // with a new "foo" attribute whose value is "bar"
  session.set("foo", "bar")
}

Pause

pause

When a user sees a page he/she often reads what is shown before choosing to click on another link. To reproduce this behavior, the pause method is used.

There are several ways to use it.

Fixed pause takes a single parameter:

  • duration: can be an Int for a duration in seconds, a duration, a Gatling EL String or a function
     
// with a number of seconds
pause(10);
// with a java.time.Duration
pause(Duration.ofMillis(100));
// with a Gatling EL string resolving to a number of seconds or a java.time.Duration
pause("#{pause}");
// with a function that returns a java.time.Duration
pause(session -> Duration.ofMillis(100));
// with a number of seconds
pause(10)
// with a java.time.Duration
pause(Duration.ofMillis(100))
// with a Gatling EL string resolving to a number of seconds or a java.time.Duration
pause("#{pause}")
// with a function that returns a java.time.Duration
pause { session -> Duration.ofMillis(100) }
// with a number of seconds
pause(10)
// with a scala.concurrent.duration.FiniteDuration
pause(100.millis)
// with a Gatling EL string resolving to a number of seconds or a scala.concurrent.duration.FiniteDuration
pause("#{pause}")
// with a function that returns a scala.concurrent.duration.FiniteDuration
pause(session => 100.millis)

Uniform random pause takes 2 parameters:

  • min: can be an Int for a duration in seconds, a duration, a Gatling EL String or a function
  • max: can be an Int for a duration in seconds, a duration, a Gatling EL String or a function
     
// with a number of seconds
pause(10, 20);
// with a java.time.Duration
pause(Duration.ofMillis(100), Duration.ofMillis(200));
// with a Gatling EL strings
pause("#{min}", "#{max}");
// with a function that returns a java.time.Duration
pause(session -> Duration.ofMillis(100), session -> Duration.ofMillis(200));
// with a number of seconds
pause(10, 20)
// with a java.time.Duration
pause(Duration.ofMillis(100), Duration.ofMillis(200))
// with a Gatling EL strings
pause("#{min}", "#{max}")
// with a function that returns a java.time.Duration
pause({ session -> Duration.ofMillis(100) }) { session -> Duration.ofMillis(200) }
// with a number of seconds
pause(10, 20)
// with a scala.concurrent.duration.FiniteDuration
pause(100.millis, 200.millis);
// with a Gatling EL strings
pause("#{min}", "#{max}")
// with a function that returns a scala.concurrent.duration.FiniteDuration
pause(session => 100.millis, session => 200.millis)

pace

You could want to control how frequently an action is executed, to target iterations per time type volumes. Gatling supports a dedicated type of pause: pace, which adjusts its wait time depending on the elapsed time since the virtual user last reached this action. E.g.:

     
forever().on(
  pace(5)
    .exec(
      // will be run every 5 seconds, irrespective of what pause time is used
      pause(1, 4)
    )
);
forever().on(
  pace(5)
    .exec(
      // will be run every 5 seconds, irrespective of what pause time is used
      pause(1, 4)
    )
)
forever(
  pace(5)
    .exec(
      // will be run every 5 seconds, irrespective of what pause time is used
      pause(1, 4)
    )
)

There are several ways to use it.

Fixed pace takes a single parameter:

  • duration: can be an Int for a duration in seconds, a duration, a Gatling EL String or a function
     
// with a number of seconds
pace(10);
// with a java.time.Duration
pace(Duration.ofMillis(100));
// with a Gatling EL string resolving to a number of seconds or a java.time.Duration
pace("#{pace}");
// with a function that returns a java.time.Duration
pace(session -> Duration.ofMillis(100));
// with a number of seconds
pace(10)
// with a java.time.Duration
pace(Duration.ofMillis(100))
// with a Gatling EL string resolving to a number of seconds or a java.time.Duration
pace("#{pace}")
// with a function that returns a java.time.Duration
pace { session -> Duration.ofMillis(100) }
// with a number of seconds
pace(10)
// with a scala.concurrent.duration.FiniteDuration
pace(100.millis)
// with a Gatling EL string resolving to a number of seconds or a scala.concurrent.duration.FiniteDuration
pace("#{pace}")
// with a function that returns a scala.concurrent.duration.FiniteDuration
pace(session => 100.millis)

Uniform random pace takes 2 parameters:

  • min: can be an Int for a duration in seconds, a duration, a Gatling EL String or a function
  • max: can be an Int for a duration in seconds, a duration, a Gatling EL String or a function
     
// with a number of seconds
pace(10, 20);
// with a java.time.Duration
pace(Duration.ofMillis(100), Duration.ofMillis(200));
// with a Gatling EL strings
pace("#{min}", "#{max}");
// with a function that returns a java.time.Duration
pace(session -> Duration.ofMillis(100), session -> Duration.ofMillis(200));
// with a number of seconds
pace(10, 20)
// with a java.time.Duration
pace(Duration.ofMillis(100), Duration.ofMillis(200))
// with a Gatling EL strings
pace("#{min}", "#{max}")
// with a function that returns a java.time.Duration
pace({ session -> Duration.ofMillis(100) }) { session -> Duration.ofMillis(200) }
// with a number of seconds
pace(10, 20)
// with a java.time.Duration
pace(100.millis, 200.millis)
// with a Gatling EL strings
pace("#{min}", "#{max}")
// with a function that returns a scala.concurrent.duration.FiniteDuration
pace(session => 100.millis, session => 200.millis)

rendezVous

In some cases, you may want to run some requests, then pause users until all other users have reached a rendez-vous point.

It takes a single parameter:

  • users: the number of users to wait before lifting the waiting point, an int
     
rendezVous(100);
rendezVous(100)
rendezVous(100)

Loop statements

repeat

Repeat the loop a specified amount of times.

It takes 2 parameters:

  • times: the number of times to repeat the loop content, can be an int, a Gatling EL String or a function
  • counterName (optional): the key to store the loop counter in the Session, starting at 0
     
// with an Int times
repeat(5).on(
  exec(http("name").get("/"))
);
// with a Gatling EL string resolving an Int
repeat("#{times}").on(
  exec(http("name").get("/"))
);
// with a function times
repeat(session -> 5).on(
  exec(http("name").get("/"))
);
// with a counter name
repeat(5, "counter").on(
  exec(session -> {
    System.out.println(session.getInt("counter"));
    return session;
  })
);
// with an Int times
repeat(5).on(
  exec(http("name").get("/"))
)
// with a Gatling EL string resolving an Int
repeat("#{times}").on(
  exec(http("name").get("/"))
)
// with a function times
repeat { session -> 5 }.on(
  exec(http("name").get("/"))
)
// with a counter name
repeat(5, "counter").on(
  exec { session ->
    println(session.getInt("counter"))
    session
  }
)
// with an Int times
repeat(5) {
  exec(http("name").get("/"))
}
// with a Gatling EL string resolving an Int
repeat("#{times}") {
  exec(http("name").get("/"))
}
// with a function times
repeat(session => 5) {
  exec(http("name").get("/"))
}
// with a counter name
repeat(5, "counter") {
  exec { session =>
    System.out.println(session("counter").as[Int])
    session
  }
}

foreach

Repeat the loop for each element in the specified sequence.

It takes 3 parameters:

  • seq: the list of elements to iterate over, can be a List, a Gatling EL String or a function
  • elementName: the key to the current element in the Session
  • counterName (optional): the key to store the loop counter in the Session, starting at 0
     
// with a static List
foreach(Arrays.asList("elt1", "elt2"), "elt").on(
  exec(http("name").get("/"))
);
// with a Gatling EL string
foreach("#{elts}", "elt").on(
  exec(http("name").get("/"))
);
// with a function
foreach(session -> Arrays.asList("elt1", "elt2"), "elt").on(
  exec(http("name").get("/"))
);
// with a counter name
foreach(Arrays.asList("elt1", "elt2"), "elt", "counter").on(
  exec(session -> {
    System.out.println(session.getString("elt2"));
    return session;
  })
);
// with a static List
foreach(listOf("elt1", "elt2"), "elt").on(
  exec(http("name").get("/"))
)
// with a Gatling EL string
foreach("#{elts}", "elt").on(
  exec(http("name").get("/"))
)
// with a function
foreach({ session -> listOf("elt1", "elt2") }, "elt").on(
  exec(http("name").get("/"))
)
// with a counter name
foreach(listOf("elt1", "elt2"), "elt", "counter").on(
  exec { session ->
    println(session.getString("elt2"))
    session
  }
)
// with a static Seq
foreach(Seq("elt1", "elt2"), "elt") {
  exec(http("name").get("/"))
}
// with a Gatling EL string
foreach("#{elts}", "elt") {
  exec(http("name").get("/"))
}
// with a function
foreach(session => Seq("elt1", "elt2"), "elt") {
  exec(http("name").get("/"))
}
// with a counter name
foreach(Seq("elt1", "elt2"), "elt", "counter") {
  exec { session =>
    System.out.println(session("elt2").as[String])
    session
  }
}

during

Iterate over the loop during the specified amount of time.

It takes 3 parameters:

  • duration: can be an Int for a duration in seconds, a duration, a Gatling EL String or a function
  • counterName (optional): the key to store the loop counter in the Session, starting at 0
  • exitASAP (optional, default true): if true, the condition will be evaluated for each element inside the loop, possibly causing to exit the loop before reaching the end of the iteration.
     
// with a duration in seconds
during(5).on(
  exec(http("name").get("/"))
);
// with a java.time.Duration
during(Duration.ofMinutes(10)).on(
  exec(http("name").get("/"))
);
// with a Gatling EL string resolving a duration
during("#{times}").on(
  exec(http("name").get("/"))
);
// with a function times
during(session -> Duration.ofMinutes(10)).on(
  exec(http("name").get("/"))
);
// with a counter name
during(5, "counter").on(
  exec(http("name").get("/"))
);
// with exitASAP
during(5, "counter", false).on(
  exec(http("name").get("/"))
);
// with a duration in seconds
during(5).on(
  exec(http("name").get("/"))
)
// with a java.time.Duration
during(Duration.ofMinutes(10)).on(
  exec(http("name").get("/"))
)
// with a Gatling EL string resolving a duration
during("#{times}").on(
  exec(http("name").get("/"))
)
// with a function times
during { session -> Duration.ofMinutes(10) }.on(
  exec(http("name").get("/"))
)
// with a counter name
during(5, "counter").on(
  exec(http("name").get("/"))
)
// with exitASAP
during(5, "counter", false).on(
  exec(http("name").get("/"))
)
// with a duration in seconds
during(5) {
  exec(http("name").get("/"))
}
// with a java.time.Duration
during(10.minutes) {
  exec(http("name").get("/"))
}
// with a Gatling EL string resolving a duration
during("#{times}") {
  exec(http("name").get("/"))
}
// with a function times
during(session => 10.minutes) {
  exec(http("name").get("/"))
}
// with a counter name
during(5, "counter") {
  exec(http("name").get("/"))
}
// with exitASAP
during(5, "counter", false) {
  exec(http("name").get("/"))
}

asLongAs

Iterate over the loop as long as the condition is satisfied.

It takes 3 parameters:

  • condition: can be a boolean, a Gatling EL String resolving a boolean or a function
  • counterName (optional): the key to store the loop counter in the Session, starting at 0
  • exitASAP (optional, default true): if true, the condition will be evaluated for each element inside the loop, possibly causing to exit the loop before reaching the end of the iteration.
     
// with a Gatling EL string resolving to a boolean
asLongAs("#{condition}").on(
  exec(http("name").get("/"))
);
// with a function
asLongAs(session -> session.getBoolean("condition")).on(
  exec(http("name").get("/"))
);
// with a counter name and exitASAP
asLongAs("#{condition}", "counter", false).on(
  exec(http("name").get("/"))
);
// with a Gatling EL string resolving to a boolean
asLongAs("#{condition}").on(
  exec(http("name").get("/"))
)
// with a function
asLongAs { session -> session.getBoolean("condition") }.on(
  exec(http("name").get("/"))
)
// with a counter name and exitASAP
asLongAs("#{condition}", "counter", false).on(
  exec(http("name").get("/"))
)
// with a Gatling EL string resolving a boolean
asLongAs("#{condition}") {
  exec(http("name").get("/"))
}
// with a function
asLongAs(session => session("condition").as[Boolean]) {
  exec(http("name").get("/"))
}
// with a counter name and exitASAP
asLongAs("#{condition}", "counter", false) {
  exec(http("name").get("/"))
}

doWhile

Similar to asLongAs but the condition is evaluated after the loop.

It takes 2 parameters:

  • condition can be a boolean, a Gatling EL String resolving a boolean or a function
  • counterName (optional): the key to store the loop counter in the Session, starting at 0
     
// with a Gatling EL string resolving to a boolean
doWhile("#{condition}").on(
  exec(http("name").get("/"))
);
// with a function
doWhile(session -> session.getBoolean("condition")).on(
  exec(http("name").get("/"))
);
// with a counter name
doWhile("#{condition}", "counter").on(
  exec(http("name").get("/"))
);
// with a Gatling EL string resolving to a boolean
doWhile("#{condition}").on(
  exec(http("name").get("/"))
)
// with a function
doWhile { session -> session.getBoolean("condition") }.on(
  exec(http("name").get("/"))
)
// with a counter name
doWhile("#{condition}", "counter").on(
  exec(http("name").get("/"))
)
// with a Gatling EL string resolving to a boolean
doWhile("#{condition}") {
  exec(http("name").get("/"))
}
// with a function
doWhile(session => session("condition").as[Boolean]) {
  exec(http("name").get("/"))
}
// with a counter name
doWhile("#{condition}", "counter") {
  exec(http("name").get("/"))
}

asLongAsDuring

Iterate over the loop as long as the condition is satisfied and the duration hasn’t been reached.

It takes 4 parameters:

  • condition can be a boolean, a Gatling EL String resolving a boolean or a function
  • duration can be an Int for a duration in seconds, a duration, a Gatling EL String or a function
  • counterName (optional): the key to store the loop counter in the Session, starting at 0
  • exitASAP (optional, default true). If true, the condition will be evaluated for each element inside the loop, possibly causing to exit the loop before reaching the end of the iteration.
     
// with a Gatling EL string resolving to a boolean and an int duration
asLongAsDuring("#{condition}", 5).on(
  exec(http("name").get("/"))
);
// with a counter name and exitASAP
asLongAsDuring("#{condition}", session -> Duration.ofMinutes(10), "counter", false).on(
  exec(http("name").get("/"))
);
// with a Gatling EL string resolving to a boolean and an int duration
asLongAsDuring("#{condition}", 5).on(
  exec(http("name").get("/"))
)
// with a counter name and exitASAP
asLongAsDuring("#{condition}", { session -> Duration.ofMinutes(10) }, "counter", false).on(
  exec(http("name").get("/"))
)
// with a Gatling EL string resolving to a boolean and an int duration
asLongAsDuring("#{condition}", 5) {
  exec(http("name").get("/"))
}
// with a counter name and exitASAP
asLongAsDuring("#{condition}", session => 10.minutes, "counter", false) {
  exec(http("name").get("/"))
}

doWhileDuring

Similar to asLongAsDuring but the condition is evaluated after the loop.

It takes 3 parameters:

  • condition can be a boolean, a Gatling EL String resolving a boolean or a function
  • duration can be an Int for a duration in seconds, a duration, a Gatling EL String or a function
  • counterName (optional): the key to store the loop counter in the Session, starting at 0
     
// with a Gatling EL string resolving to a boolean and an int duration
doWhileDuring("#{condition}", 5).on(
  exec(http("name").get("/"))
);
// with a counter name and exitASAP
doWhileDuring("#{condition}", session -> Duration.ofMinutes(10), "counter", false).on(
  exec(http("name").get("/"))
);
// with a Gatling EL string resolving to a boolean and an int duration
doWhileDuring("#{condition}", 5).on(
  exec(http("name").get("/"))
)
// with a counter name and exitASAP
doWhileDuring("#{condition}", { session -> Duration.ofMinutes(10) }, "counter", false).on(
  exec(http("name").get("/"))
)
// with a Gatling EL string resolving to a boolean and an int duration
doWhileDuring("#{condition}", 5) {
  exec(http("name").get("/"))
}
// with a counter name and exitASAP
doWhileDuring("#{condition}", session => 10.minutes, "counter", false) {
  exec(http("name").get("/"))
}

forever

Iterate over the loop content forever.

     
forever().on(
  exec(http("name").get("/"))
);
// with a counter name
forever("counter").on(
  exec(http("name").get("/"))
);
forever().on(
  exec(http("name").get("/"))
)
// with a counter name
forever("counter").on(
  exec(http("name").get("/"))
)
forever {
  exec(http("name").get("/"))
}
// with a counter name
forever("counter") {
  exec(http("name").get("/"))
}

counterName is optional.

Conditional statements

Gatling’s DSL has conditional execution support.

doIf

Used to execute a specific chain of actions only when some condition is satisfied.

It takes one single parameter:

  • condition can be a boolean, a Gatling EL String resolving a boolean or a function
     
// with a Gatling EL string resolving to a boolean
doIf("#{condition}").then(
  exec(http("name").get("/"))
);

// with a function
doIf(session -> session.getBoolean("condition")).then(
  exec(http("name").get("/"))
);
// with a Gatling EL string resolving to a boolean
doIf("#{condition}").then(
  exec(http("name").get("/"))
)

// with a function
doIf { session -> session.getBoolean("condition") }.then(
  exec(http("name").get("/"))
)
// with a Gatling EL string resolving to a boolean
doIf("#{condition}") {
  exec(http("name").get("/"))
}

// with a function
doIf(session => session("condition").as[Boolean]) {
  exec(http("name").get("/"))
}

doIfEquals

If your test condition is simply to compare two values, you can simply use doIfEquals:

It takes 2 parameters:

  • actual can be a static value, a Gatling EL String or a function
  • expected can be a static value, a Gatling EL String or a function
     
doIfEquals("#{actual}", "expectedValue").then(
  // executed if the session value stored in "actual" is equal to "expectedValue"
  exec(http("name").get("/"))
);
doIfEquals("#{actual}", "expectedValue").then(
  // executed if the session value stored in "actual" is equal to "expectedValue"
  exec(http("name").get("/"))
)
doIfEquals("#{actual}", "expectedValue") {
  // executed if the session value stored in "actual" is equal to "expectedValue"
  exec(http("name").get("/"))
}

doIfOrElse

Similar to doIf, but with a fallback if the condition evaluates to false.

It takes one single parameter:

  • condition can be a boolean, a Gatling EL String resolving a boolean or a function
     
doIfOrElse("#{condition}").then(
  exec(http("name").get("/"))
).orElse(
  exec(http("else").get("/"))
);
doIfOrElse("#{condition}").then(
  exec(http("name").get("/"))
).orElse(
  exec(http("else").get("/"))
)
doIfOrElse("#{condition}") {
  exec(http("name").get("/"))
} {
  exec(http("else").get("/"))
}

doIfEqualsOrElse

Similar to doIfEquals but with a fallback if the condition evaluates to false.

It takes 2 parameters:

  • actual can be a static value, a Gatling EL String or a function
  • expected can be a static value, a Gatling EL String or a function
     
doIfEqualsOrElse("#{actual}", "expectedValue").then(
  // executed if the session value stored in "actual" equals to "expectedValue"
  exec(http("name").get("/"))
).orElse(
  // executed if the session value stored in "actual" is not equal to "expectedValue"
  exec(http("else").get("/"))
);
doIfEqualsOrElse("#{actual}", "expectedValue").then(
  // executed if the session value stored in "actual" equals to "expectedValue"
  exec(http("name").get("/"))
).orElse(
  // executed if the session value stored in "actual" is not equal to "expectedValue"
  exec(http("else").get("/"))
)
doIfEqualsOrElse("#{actual}", "expectedValue") {
  // executed if the session value stored in "actual" equals to "expectedValue"
  exec(http("name").get("/"))
} {
  // executed if the session value stored in "actual" is not equal to "expectedValue"
  exec(http("else").get("/"))
}

doSwitch

Add a switch in the chain. Every possible sub-chain is defined with a key. Switch is selected through the matching of a key with the evaluation of the passed expression. If no switch is selected, the switch is bypassed.

     
doSwitch("#{myKey}").on(
  Choice.withKey("foo", exec(http("name1").get("/foo"))),
  Choice.withKey("bar", exec(http("name2").get("/bar")))
);
doSwitch("#{myKey}").on(
  Choice.withKey("foo", exec(http("name1").get("/foo"))),
  Choice.withKey("bar", exec(http("name2").get("/bar")))
)
doSwitch("#{myKey}")( // beware: use parentheses, not curly braces!
  "foo" -> exec(http("name1").get("/foo")),
  "bar" -> exec(http("name2").get("/bar"))
)

doSwitchOrElse

Similar to doSwitch, but with a fallback if no switch is selected.

     
doSwitchOrElse("#{myKey}").on(
  Choice.withKey("foo", exec(http("name1").get("/foo"))),
  Choice.withKey("bar", exec(http("name2").get("/bar")))
).orElse(
  exec(http("name3").get("/baz"))
);
doSwitchOrElse("#{myKey}").on(
  Choice.withKey("foo", exec(http("name1").get("/foo"))),
  Choice.withKey("bar", exec(http("name2").get("/bar")))
).orElse(
  exec(http("name3").get("/baz"))
)
doSwitchOrElse("#{myKey}")( // beware: use parentheses, not curly braces!
  "foo" -> exec(http("name1").get("/foo")),
  "bar" -> exec(http("name2").get("/bar"))
)(
  exec(http("name3").get("/baz"))
)

randomSwitch

randomSwitch can be used to emulate simple Markov chains. Simple means cyclic graphs are not currently supported.

     
randomSwitch().on(
  Choice.withWeight(60.0, exec(http("name1").get("/foo"))),
  Choice.withWeight(40.0, exec(http("name2").get("/bar")))
);
randomSwitch().on(
  Choice.withWeight(60.0, exec(http("name1").get("/foo"))),
  Choice.withWeight(40.0, exec(http("name2").get("/bar")))
)
randomSwitch( // beware: use parentheses, not curly braces!
  60.0 -> exec(http("name1").get("/foo")),
  40.0 -> exec(http("name2").get("/bar"))
)

Percentages sum can’t exceed 100%. If sum is less than 100%, users that won’t fall into one of the chains will simply exit the switch and continue. Once users are done with the switch, they simply continue with the rest of the scenario.

randomSwitchOrElse

Similar to randomSwitch, but with a fallback if no switch is selected (i.e.: random number exceeds percentages sum).

     
randomSwitchOrElse().on(
  Choice.withWeight(60.0, exec(http("name1").get("/foo"))),
  Choice.withWeight(20.0, exec(http("name2").get("/bar")))
).orElse(
  exec(http("name3").get("/baz"))
);
randomSwitchOrElse().on(
  Choice.withWeight(60.0, exec(http("name1").get("/foo"))),
  Choice.withWeight(20.0, exec(http("name2").get("/bar")))
).orElse(
  exec(http("name3").get("/baz"))
)
randomSwitchOrElse( // beware: use parentheses, not curly braces!
  60.0 -> exec(http("name1").get("/foo")),
  20.0 -> exec(http("name2").get("/bar"))
)(
  exec(http("name3").get("/baz"))
)

uniformRandomSwitch

Similar to randomSwitch, but with an uniform distribution amongst chains.

     
uniformRandomSwitch().on(
  exec(http("name1").get("/foo")),
  exec(http("name2").get("/bar"))
);
uniformRandomSwitch().on(
  exec(http("name1").get("/foo")),
  exec(http("name2").get("/bar"))
)
uniformRandomSwitch( // beware: use parentheses, not curly braces!
  exec(http("name1").get("/foo")),
  exec(http("name2").get("/bar"))
)

roundRobinSwitch

Similar to randomSwitch, but dispatch uses a round-robin strategy.

     
roundRobinSwitch().on(
  exec(http("name1").get("/foo")),
  exec(http("name2").get("/bar"))
);
roundRobinSwitch().on(
  exec(http("name1").get("/foo")),
  exec(http("name2").get("/bar"))
)
roundRobinSwitch( // beware: use parentheses, not curly braces!
  exec(http("name1").get("/foo")),
  exec(http("name2").get("/bar"))
)

Errors handling

tryMax

Any error (a technical exception such as a timeout, or a failed check) in the wrapped chain would cause the virtual user to interrupt and start over from the beginning, up to a maximum number of times.

It takes 2 parameters:

  • times: the maximum number of attempts, an int
  • counterName (optional): the key to store the loop counter in the Session, starting at 0
     
tryMax(5).on(
  exec(http("name").get("/"))
);

// with a counter name
tryMax(5, "counter").on(
  exec(http("name").get("/"))
);
tryMax(5).on(
  exec(http("name").get("/"))
)

// with a counter name
tryMax(5, "counter").on(
  exec(http("name").get("/"))
)
tryMax(5) {
  exec(http("name").get("/"))
}

// with a counter name
tryMax(5, "counter") {
  exec(http("name").get("/"))
}

exitBlockOnFail

Similar to tryMax, but without retrying on failure.

     
exitBlockOnFail(
  exec(http("name").get("/"))
);
exitBlockOnFail(
  exec(http("name").get("/"))
)
exitBlockOnFail {
  exec(http("name").get("/"))
}

exitHere

Make the user exit the scenario from this point.

     
exitHere();
exitHere()
exitHere

exitHereIf

Make the user exit the scenario from this point if the condition holds.

In takes one single parameter:

  • condition: can be a boolean, a Gatling EL String resolving a boolean or a function
     
exitHereIf("#{myBoolean}");
exitHereIf(session -> true);
exitHereIf("#{myBoolean}")
exitHereIf { session -> true }
exitHereIf("#{condition}")
exitHereIf(session => true)

exitHereIfFailed

Make the user exit the scenario from this point if it previously had an error.

     
exitHereIfFailed();
exitHereIfFailed()
exitHereIfFailed

stopInjector

Make the user exit abruptly stop the injector.

In takes one single parameter:

  • message: can be a static String, a Gatling EL String resolving a String or a function, used to log an ERROR message
     
stopInjector("#{someErrorMessage}");
stopInjector(session -> "someErrorMessage");
stopInjector("#{someErrorMessage}")
stopInjector { session -> "someErrorMessage"}
stopInjector("#{someErrorMessage}")
stopInjector(session => "someErrorMessage")

Groups

Create a group of requests to model processes or requests in the same page. Groups can be nested.

     
group("foo").on(
  exec(http("name").get("/"))
);
group("foo").on(
  exec(http("name").get("/"))
)
group("foo") {
  exec(http("name").get("/"))
}

Edit this page on GitHub