Declare and increment a global variable.

Is there any way I can declare and increment a global variable?

var my_var = 0

.exec(http(“Request 1”)
.post("/bindit")
my_var = my_var + 1
.headers(headers_20)
.fileBody(“bind”, Map(
“domain” → ${domain},
“rid” → “${rid} + my_var

)).asXML
)

I didn’t find a way to do it. Is that even possible or is it a silly question?

There's always a way, that's the beauty of having scripts that are code.

val myGlobalVar = new java.util.concurrent.atomicAtomicInteger(0)

.exec(session => session.setAttribute("*my_var*",
myGlobalVar.getAndIncrement))
.exec(http("Request 1")
.post("/bindit")
.headers(headers_20)
.fileBody("bind", Map(
"domain" -> ${domain},
"rid" -> "${rid}${*my_var}*"
                 )).asXML
)

Cheers,

Stéphane

I did that and i recieved

type atomicAtomicInteger is not a member of package java.util.concurrent

I tried importing

import java.util.concurrent.atomic.AtomicInteger

But nothing changed.

Found it!

a small typo

val myGlobalVar = new java.util.concurrent.atomicAtomicInteger(0)

vs

val myGlobalVar = new java.util.concurrent.atomic.AtomicInteger(0)

There’s a point missing:
val myGlobalVar = new java.util.concurrent.atomic**.**AtomicInteger(0)

In your example you are concatenating ${rid}${my_var}
But I want to add those two values
if ${rid} = 5 and ${my_var} = 2 the result should be 7, but now is 52.

How can I add those two numbers?

Ah, sorry, I didn’t understand that as what you initially wrote was indeed concatenating.

Very simple:

java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer

How can I convert a String to a Integer?
It appears that ${rid} comes as a string “5”.

Well, as I don’t know where your rid value comes from…
If it comes from a CSV, yes, it’s a String, so you have to convert it to a String:
session.getTypedAttribute[String(“rid”).toInt + myGlobalVar.getAndIncrement)

Missing brace
session.getTypedAttributeString.toInt + myGlobalVar.getAndIncrement

It is still concatenating the values, the rid value comes from a POST response from a JSON
.check(jsonPath("//rid").saveAs(“rid”))

the value in JSON is numeric
rid: 134

but when I do
.exec(session => session.setAttribute(“new_rid”, (newRid.getAndIncrement + (session.getTypedAttributeString.toInt))))

It still returns 1341 (if newRid is 1). I don’t know what to do or how to debug this case I don’t get it why it doesn’t just add the two numbers. :expressionless:

JsonPath currently extract as String, the "rid" attribute in the Session is
indeed a String. Seems complicated to change that, but I will give it a
thought.

Regarding the result you get, the problem is on your side, the code I sent
you is definitively correct and sums two Ints.
Moreover, with your example, if newRid is 1 and "rid" is 134, if the code
was concatenating String (which it doesn't), you would get 1134, not 1341.

My 2 cents is that you didn't change what you're passing to fileBody, like
I wrote previously.

.exec(http("Request 1")
.post("/bindit")
.headers(headers_20)
.fileBody("bind", Map(
"domain" -> ${domain},
"rid" -> "*${**new_rid**}*"
                 )).asXML
)

You are absolutely right. Unfortunately I fail again.

I forgot in one of my *.ssp templates rid=’<%= rid + 1 %>'
How dumb is that huh?

Thank you one more time for your help, and I hope that all my dumb questions will prevent others from doing the same mistakes.
Cheers!

No pro.

Have fun!

I am working on my code with the help of this post.
I am trying to make “token” variable below globally accessible (I do not want to increment it)
Error I get is “value setAttribute is not a member of io.gatling.core.session.Session”

Can you please guide me to resolve this.

val myGlobalVar = new java.util.concurrent.atomic.AtomicInteger(0)

def Authorize(scenarioName: String) : ChainBuilder ={

exec(http(scenarioName)

.post("/token")

.body(StringBody(""“grant_type=client_credentials”""))

.check(status is 200)

.check(jsonPath("$.access_token").saveAs(“token”))

).exec(session => {

session.setAttribute(“token”, myGlobalVar.getAndIncrement)})

}

It’s my first time with gatling . I have a doubt regarding the declaration of a variable and I think you could help me.

I have programmed a loop and have created a variable (‘iteration’) that should be incremented in one unit in each iteration. However, I am getting the initial value in every iteration.

I have already read the gatling documentation and the answers of this group. The suggested solution is to get the Session id in each iteration. However, this doesn’t function in our case. Why? Because we need this variable to be an integer not a string. Thank you!

    val elements = 10000    

    val numberOfParallelReq = 100

    var iteration = -1

    val execute_parallel = repeat( elements / numberOfParallelReq , "m") {
        exec{
            iteration = iteration + 1

            http("Iteration ${m}")
                .get("url")
                .resources(parallelRequests( iteration ): _*)
        }
    }

    def parallelRequests(iteration: Int): Seq[HttpRequestBuilder] =
        (0 until numberOfParallelReq).map(element => generatePageRequest(element + 1, iteration))

    def generatePageRequest(element: Int, iteration: Int): HttpRequestBuilder = {
        http("Element " + (element + (iteration * numberOfParallelReq)) )
            .get("/" + (element + (iteration * numberOfParallelReq)) )
    }   

Error I get is “value setAttribute is not a member of io.gatling.core.session.Session”

Go for session.set()instead of session.setAttribute()[works the same way] like:

`

.exec(session => {
session.set(“token”, myGlobalVar.getAndIncrement)
})

`

session.setAttribute() does not exist anymore (I reckon, that’s the old name of the method)

Is there a way for string?

Dne čtvrtek 28. února 2013 10:33:28 UTC+1 Alexandru Geana napsal(a):