How to increment a variable in Gatlling Loop
Asked Answered
J

2

6

I am trying to write a Gatling script where I read a starting number from a CSV file and loop through, say 10 times. In each iteration, I want to increment the value of the parameter.

It looks like some Scala or Java math is needed but could not find information on how to do it or how and where to combine Gatling EL with Scala or Java.

Appreciate any help or direction.

var numloop = new java.util.concurrent.atomic.AtomicInteger(0)

val scn = scenario("Scenario Name")

.asLongAs(_=> numloop.getAndIncrement() <3, exitASAP = false){
    feed(csv("ids.csv"))   //read ${ID} from the file
    .exec(http("request")
        .get("""http://finance.yahoo.com/q?s=${ID}""")
        .headers(headers_1))
    .pause(284 milliseconds)

    //How to increment ID for the next iteration and pass in the .get method?
}
Jana answered 11/6, 2014 at 21:42 Comment(0)
C
9

You copy-pasted this code from Gatling's Google Group but this use case was very specific. Did you first properly read the documentation regarding loops? What's your use case and how doesn't it fit with basic loops?

Edit: So the question is: how do I get a unique id per loop iteration and per virtual user?

You can compute one for the loop index and a virtual user id. Session already has a unique ID but it's a String UUID, so it's not very convenient for what you want to do.

// first, let's build a Feeder that set an numeric id:
val userIdFeeder = Iterator.from(0).map(i => Map("userId" -> i))

val iterations = 1000

// set this userId to every virtual user
feed(userIdFeeder)
// loop and define the loop index
.repeat(iterations, "index") {
  // set an new attribute named "id"
  exec{ session =>
    val userId = session("userId").as[Int]
    val index = session("index").as[Int]
    val id = iterations * userId + index
    session.set("id", id)
  }
  // use id attribute, for example with EL ${id}
}
Cormier answered 12/6, 2014 at 6:8 Comment(3)
I posted this question on the Gatling group too.Jana
I posted this question on the Gatling group too. The problem I am having is using some math functions in the scenario. IMO, the documentation gives a good starting point. However, I am having trouble solving my problem. Each user has to send, say 1000 GET but the ID values in each GET has to be incremented. So, if the first user starts with ID=1, it will send 1 to 1000 in the GETs. The next user will start from, say 2001 and send GET with 20001 to 3000, and so on. Thanks for taking the time to respond and also appreciate you actively helping others.Jana
@StepphaneLandelle This is the only question about this point on Stackoverflow, so why not just answer the question. Why complain and ask whether the guy has read the f*n manual?Intyre
C
0

Here is my answer to this: Problem Statement: I had to repeat the gatling execution for configured set of times, and my step name has to be dynamic.

    object UrlVerifier {

  val count = new java.util.concurrent.atomic.AtomicInteger(0)
  val baseUrl = Params.applicationBaseUrl

  val accessUrl = repeat(Params.noOfPagesToBeVisited,"index") {
    exec(session=> {
      val randomUrls: List[String] = UrlFeeder.getUrlsToBeTested()
      session.set("index", count.getAndIncrement).set("pageToTest", randomUrls(session("index").as[Int]))
    }
    ).
    exec(http("Accessing Page ${pageToTest}")
      .get(baseUrl+"${pageToTest}")
      .check(status.is(200))).pause(Params.timeToPauseInSeconds)
  }

So basically UrlFeeder give me list of String (urls to be tested) and in the exec, we are using count (AtomicInteger), and using this we are populating a variable named 'index' whose value will start from 0 and will be getAndIncremented in each iteration. This 'index' variable is the one which will be used within repeat() loop as we are specifying the name of counterVariable to be used as 'index'

Hope it helps others as well.

Carmellacarmelle answered 12/5, 2017 at 5:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.