Gatling Code Fragments

From PeformIQ Upgrade
Revision as of 09:18, 28 April 2021 by PeterHarding (talk | contribs)
Jump to navigation Jump to search

Approaches for Dynamic Feeder Assignment

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


One Approach

Feeders are instantiated at the same time as the Simulation. You can't defer it to the Simulation runtime, unless you hack with the underlying tools.

How many of those files do you have? If you have just a few "groupid" files, you could use either doSwitch.

.doSwitch("${groupid}") (
  "foo" -> feed(csv("foo.csv").random),
  "bar" -> feed(csv("bar.csv").random)
)

This can be generalized:

def groupIdFeed(groupId: String) = groupId -> feed(csv(groupId + ".csv").random)

.doSwitch("${groupid}") (
  groupIdFeed("foo"),
  groupIdFeed("bar")
)

An Alternative

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.