Gatling Code Fragments

From PeformIQ Upgrade
Revision as of 09:12, 28 April 2021 by PeterHarding (talk | contribs) (Created page with "=Approaches for Dynamic Feeder Assignment= From - https://stackoverflow.com/questions/24533162/gatling-dynamic-feed-selection Define data so: <pre> object Data { var gro...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Approaches for Dynamic Feeder Assignment

From - https://stackoverflow.com/questions/24533162/gatling-dynamic-feed-selection

Define data so:

object Data {

  var groupList : List[Int] = List( ... ) // long list of IDs

  def branchByGroup ( path: String ) : ChainBuilder = {
    var c = bootstrap
    groupList.foreach( x => {
      c = c.doIf( "${groupId}", x.toString() ) {
        feed( csv( path + "/" + x.toString() + ".csv" ).random )
      }
    })
    return c
  }

  def searchCriteria () : ChainBuilder = branchByGroup( "search" )
  def other() : ChainBuilder = branchByGroup( "other" )
}

Then, inside the scenario:

def scn = scenario("My Scenario")
            .feed( credentialSource )
            .exec( Login.steps )
            .during( loopTime ) {
                Data.searchCriteria()
                .exec( Search.steps )

The call to Data.searchCriteria is injecting the .doIf() calls into the chain. If it was not the first thing in the block, it would have to be wrapped it in .exec() of course.

Notice the part that says c = c.doIf - the function needs to return the END of the chain, as opposed to the beginning. You can't build a chain by attaching everything to the first link in the chain! Simulating what the DSL does requires this call-and-assign approach.