Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to increment a variable in Gatlling Loop

Tags:

gatling

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?
}
like image 837
pparthi Avatar asked Jun 11 '14 21:06

pparthi


1 Answers

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}
}
like image 127
Stephane Landelle Avatar answered Jan 03 '23 00:01

Stephane Landelle