How to increment a variable in Gatlling Loop

pparthi picture pparthi · Jun 11, 2014 · Viewed 13.8k times · Source

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?
}

Answer

Stephane Landelle picture Stephane Landelle · Jun 12, 2014

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}
}