Gatling Repeat Loop not working for my post request attempt?

245 views
Skip to first unread message

Thomas Polaske

unread,
Aug 25, 2021, 3:17:08 PM8/25/21
to Gatling User Group
see the following scenario.  When I run this scenario I get a successful "POST createRuleSet" from the createRulesetUid method.  
  
  val createRulesetScenario: ScenarioBuilder = scenario("Create Ruleset scenario")
    .feed(TestUtil.configValues)
    .exec(TestUtil.getAccessToken)
    .exec(TestUtil.createRulesetUid(rulesetFilePath,0))
    .exec(TestUtil.createRulesetUid(rulesetFilePath,1))

I get the expected response 2 successful requests to POST createRuleSet
================================================================================
2021-08-25 15:05:17                                           3s elapsed
---- Requests ------------------------------------------------------------------
> Global                                                   (OK=7      KO=0     )
> POST OAuth                                               (OK=4      KO=0     )
> POST createRuleSet - data/FBP-ruleset-medium             (OK=2      KO=0     )

However, when I move this into a repeat loop like below.

  val createRulesetScenario: ScenarioBuilder = scenario("Create Ruleset scenario")
    .feed(TestUtil.configValues)
    .exec(TestUtil.getAccessToken)
    // .exec(TestUtil.createRulesetUid(rulesetFilePath,numberOfRulesets))
    .repeat(numberOfRulesets,"indexCount") {
      exec(session => {
      exec(TestUtil.createRulesetUid(rulesetFilePath, Integer.parseInt(session.attributes("indexCount").toString)).pause(5))
  println("INDEX " + session.attributes("indexCount").toString)
      session
    })
    }
I do NOT get any successful createRuleSet post requests?? see below

================================================================================
2021-08-25 15:08:09                                           0s elapsed
---- Requests ------------------------------------------------------------------
> Global                                                   (OK=3      KO=0     )
> POST OAuth                                               (OK=2      KO=0     )

---- Create Ruleset scenario ---------------------------------------------------
[##########################################################################]100%
          waiting: 0      / active: 0      / done: 1
================================================================================
This is the POST request that I am attempting to repeat 
 
  def createRulesetUid(ruleset:String, ruleSetNumber:Int) =
    feed(TestUtil.configValues)
      .exec(getAccessToken)
      .exec(_.set("auth_token", token))
      .exec(
        http("POST createRuleSet - " + ruleset.dropRight(5))
          .post(session => getCreateRulesetUrl(session,ruleset))
          .header("Authorization", "Bearer ${auth_token}")
          .header("GW-Tenant", "${gw_tenant}")
          .header("GW-User", "${gw_user}")
          .header("Content-Type", "application/json")
          .header("GW-request-grn", "${gw_grn}")
          .headers(TestUtil.serviceCallTracingHeaders)
          .body(StringBody(getResourceBody(ruleSetNumber)))
          .check(status.is(200)) // check successful update
          .check(jsonPath("$.resourceUid").exists.saveAs("resourceUid"))).exitHereIfFailed
      .exec {
        session => {
          if (ruleset.contains("small")) {
            resourceId_small = session("resourceUid").as[String]
          } else {
            rulesetUidArray.add(session("resourceUid").as[String])
            Console.println("-rulesetUidArray: " + rulesetUidArray.toString);
          }
          session
        }
      }
  

Any help on what I am doing wrong would be very much appreciated.  thanks!

Stéphane LANDELLE

unread,
Aug 25, 2021, 3:18:39 PM8/25/21
to gat...@googlegroups.com
This cannot work, please read the documentation: https://gatling.io/docs/gatling/reference/current/general/scenario/#exec

--

Stéphane Landelle

Chief Technical Officer

   

slan...@gatling.io
gatling.io
   
facebook twitter linkedin 


--
You received this message because you are subscribed to the Google Groups "Gatling User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gatling+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/gatling/ed19c2a0-421d-4c69-96cc-901454cce181n%40googlegroups.com.

Stéphane LANDELLE

unread,
Aug 25, 2021, 3:20:13 PM8/25/21
to gat...@googlegroups.com
You have to modify your TestUtil.createRulesetUid so it doesn't take an Int parameter but an Expression[Int].

--

Stéphane Landelle

Chief Technical Officer

   

slan...@gatling.io
gatling.io
   
facebook twitter linkedin 

Thomas Polaske

unread,
Aug 25, 2021, 3:41:42 PM8/25/21
to Gatling User Group

Thanks Stephane for the quick response. 

Sorry I am a bit new to Gatling and I am not sure why this will not work?  I tried your suggestion and I added the import io.gatling.core.session.Expression and updated the parameter to ruleSetNumber:Expression[Int] but still received the same result.

This is the only update I made

def createRulesetUid(ruleset:String, ruleSetNumber:Expression[Int]

Is there something else I am missing?

Thanks!
Tom

Stéphane LANDELLE

unread,
Aug 25, 2021, 3:43:33 PM8/25/21
to gat...@googlegroups.com

Extract from the documentation I linked:

Gatling DSL components are immutable ActionBuilder(s) that have to be chained altogether and are only built once on startup. The result is a workflow chain of Action(s). These builders don’t do anything by themselves, they don’t trigger any side effect, they are just definitions. As a result, creating such DSL components at runtime in functions is completely meaningless. If you want conditional paths in your execution flow, use the proper DSL components (doIfrandomSwitch, etc)
exec { session =>
  if (someSessionBasedCondition(session)) {
    // just create a builder that is immediately discarded, hence doesn't do anything
    // you should be using a doIf here
    http("Get Homepage").get("http://github.com/gatling/gatling")
  }
  session
}

--

Stéphane Landelle

Chief Technical Officer

   

slan...@gatling.io
gatling.io
   
facebook twitter linkedin 

Thomas Polaske

unread,
Aug 25, 2021, 4:59:18 PM8/25/21
to Gatling User Group

Thanks Stephane

Even when I remove the if statement it does not seem to work.

See

def createRulesetUid(ruleset:String, ruleSetNumber:Expression[Int]) =


  feed(TestUtil.configValues)
    .exec(getAccessToken)
    .exec(_.set("auth_token", token))
    .exec(
      http("POST createRuleSet - " + ruleset.dropRight(5))
        .post(session => getCreateRulesetUrl(session,ruleset))
        .header("Authorization", "Bearer ${auth_token}")
        .header("GW-Tenant", "${gw_tenant}")
        .header("GW-User", "${gw_user}")
        .header("Content-Type", "application/json")
        .header("GW-request-grn", "${gw_grn}")
        .headers(TestUtil.serviceCallTracingHeaders)

        .body(StringBody(getResourceBody(Integer.parseInt(ruleSetNumber.toString()))))


        .check(status.is(200)) // check successful update
        .check(jsonPath("$.resourceUid").exists.saveAs("resourceUid"))).exitHereIfFailed
    .exec {
      session => {

          rulesetUidArray.add(session("resourceUid").as[String])

          Console.println("-------###########################-------ruleSetNumber: " + rulesetUidArray.toString);
        session
      }
    }

 

When I call it from my scenario in the Repeat it does  not seem to enter the method

val createRulesetScenario: ScenarioBuilder = scenario("Create Ruleset scenario")
  .feed(TestUtil.configValues)
  .exec(TestUtil.getAccessToken)

//  .exec(TestUtil.createRulesetUid(rulesetFilePath,0))
//  .exec(TestUtil.createRulesetUid(rulesetFilePath,1))


  .repeat(numberOfRulesets,"indexCount") {
    exec(session => {

      exec(TestUtil.createRulesetUid(rulesetFilePath, Integer.parseInt(session.attributes("indexCount").toString))).pause(5)
    session
  })
  }

 

there must be something else I am missing about this repeat?

Stéphane LANDELLE

unread,
Aug 25, 2021, 5:02:39 PM8/25/21
to gat...@googlegroups.com
All the documentation I've provided you clearly states that calling Gatling DSL methods inside a function is fruitless.

exec(session => {
      exec(???)
      session
  })

does not and will never work.

--

Stéphane Landelle

Chief Technical Officer

   

slan...@gatling.io
gatling.io
   
facebook twitter linkedin 

Thomas Polaske

unread,
Aug 26, 2021, 1:57:17 AM8/26/21
to Gatling User Group

Ok gotcha Thanks!

So I guess what I am trying to do is something like this..

I have a list "jurisdictionCode": [

            "Hokkaido",

            "Aomori",

            "Iwate",

            "Miyagi"

        ]

I would like to loop through these values and use them for my post request .. is there a way to pass in each jurisdiction as a string or something to have in my post?  Something like this?:

    .foreach("${jurisdictionCode}", "jurisdiction") {

       exec(TestUtil.createRulesetUid2(rulesetFilePath, "${jurisdiction}"))

    }

butt I cant seem to get it to work using this post.  Note:  I want to save the response in the session see .check(jsonPath("$.resourceUid").exists.saveAs("resourceUid"))).

def createRulesetUid2(ruleset:String, jurisdictionParam:Expression[String]) =  // not sure if this is correct or how i would access this value?  it is unclear how Expression[String]  works?


     feed(TestUtil.configValues)
    .exec(getAccessToken)
    .exec(_.set("auth_token", token))
    .exec(
      http("POST createRuleSet - " + ruleset.dropRight(5))
        .post(session => getCreateRulesetUrl(session,ruleset))
        .header("Authorization", "Bearer ${auth_token}")
        .header("GW-Tenant", "${gw_tenant}")
        .header("GW-User", "${gw_user}")
        .header("Content-Type", "application/json")
        .header("GW-request-grn", "${gw_grn}")
        .headers(TestUtil.serviceCallTracingHeaders)

          .body(StringBody(getResourceBodyJurisdiction("${jurisdiction}")))  //This doesnt work.. how do I get the jurisdiction value in for the body??
        .check(status.is(200))
        .check(jsonPath("$.resourceUid").exists.saveAs("resourceUid"))).exitHereIfFailed // note I want to save  resourceUid for use later.
    .exec {
      session => {
        rulesetUidArray.add(session("resourceUid").as[String])  //here I was adding each resourceUid into a array to save
        session
      }
    }


suggestions would be very much appreciated ! 

Thomas Polaske

unread,
Aug 26, 2021, 8:24:45 AM8/26/21
to Gatling User Group
ok looks like  I got this to work by removing the  exec() in the forEach

.foreach("${jurisdictionCode}", "jurisdiction") {

       TestUtil.createRulesetUid2(rulesetFilePath, "${jurisdiction}")

    }

thanks!


Reply all
Reply to author
Forward
0 new messages