1

I come from a Java background and am taking over a Gatling project where I noticed what seems to me a bit of inconsistency when using what is a val or a def method. The picture below exemplifies that and I was wondering if there's any guidance on what is the best usage for these within the Gatling context please.

enter image description here

These are other examples where I'm not sure what should be used. I'm assuming a Switch makes sense being inside a method but not sure about the others?

private def teacherViewResources: ChainBuilder =
    exec(viewResourcesFlow)
      .randomSwitch(
        70.0 -> pause(1,2).exec(teacherLaunchResource),
        10.0 -> pause(1,2).exec(teacherAssignResource),
        20.0 -> pause(1,2).exec(teacherResourcesNext)
      )

  private def teacherLaunchResource: ChainBuilder =
    exec(launchResourcesFlow)

val rootTeacherScenario = scenario("Root Teacher Scenario " + currentScenario.toString)
    .doIfOrElse(currentScenario == PossibleScenarios.BRANCH)(
      feed(userFeederTeacher).during(EXECUTION_TIME_SEC) {
        exec(teacherBranching)
      }

      //For use with atOnceUsers for debugging
      //feed(userFeederTeacher).exec(simulationTeacherBranching)

    )(
      exec {
        session =>
          logger.debug("Invalid teacher scenario chosen")
          session
      }
    )

val loginFlowWithExit = exec(loginFlow).exitHereIfFailed

val teacherBranching = group("teacherBranching") {
    exec(loginFlow)
      .exec(session => sessionSetSessionVariable(session))
      .exec(execFlaggedScenario(teacherDashboard)) // First method to run for a teacher
      .exec(logout())
  }

Many thanks.

Francislainy Campos
  • 3,462
  • 4
  • 33
  • 81

1 Answers1

1

val is evaluated once while def is evaluated on every call. Remember that Gatling DSL components are just builders, not what is executed when your test is running. Everything that doesn't take a parameter could be a val, you just have to make sure you don't end up with using forward references, eg:

broken:

val foo = exec(???).exec(bar) // here, bar is still null because it's populated later in the code
val bar = exec(???)

correct:

val bar = exec(???)
val foo = exec(???).exec(bar) // fine because bar is already populated
Stéphane LANDELLE
  • 6,076
  • 2
  • 10
  • 12
  • Hi Stephane, thank you for your reply. Sorry, when you say val is evaluated only one what do you mean? And how's that in the context of multiple users coming through a csv file? – Francislainy Campos Apr 13 '21 at 09:30
  • And when you talk about params, does that include the naming for group piece, which would then be turned into a method? – Francislainy Campos Apr 13 '21 at 09:49
  • `val` is what could be seen as a constant or immutable value and, as a function as well. Maybe this post will be helpful for you as well https://alvinalexander.com/scala/fp-book-diffs-val-def-scala-functions/ where there are some semantics `val` vs `def` – user2963757 Apr 13 '21 at 10:09
  • Thanks. I think I am perhaps still failing on seeing what is really immutable inside Gatling. – – Francislainy Campos Apr 13 '21 at 12:25
  • I mean, is a scenario immutable that should be a val? I suppose so.. A chainBuilder, a group, a randomSwitch branch? hmm, I don't know. – Francislainy Campos Apr 13 '21 at 12:27