Declare and increment a global variable.

5,389 views
Skip to first unread message

Alexandru Geana

unread,
Feb 28, 2013, 4:33:28 AM2/28/13
to gat...@googlegroups.com
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
)

Alexandru Geana

unread,
Feb 28, 2013, 4:36:01 AM2/28/13
to gat...@googlegroups.com
I didn't find a way to do it. Is that even possible or is it a silly question?

Stéphane Landelle

unread,
Feb 28, 2013, 4:41:26 AM2/28/13
to gat...@googlegroups.com
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

2013/2/28 Alexandru Geana <hex...@gmail.com>
I didn't find a way to do it. Is that even possible or is it a silly question?

--
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.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Alexandru Geana

unread,
Feb 28, 2013, 4:46:43 AM2/28/13
to gat...@googlegroups.com
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.

Alexandru Geana

unread,
Feb 28, 2013, 4:49:57 AM2/28/13
to gat...@googlegroups.com
Found it!

a small typo

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

vs

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

Stéphane Landelle

unread,
Feb 28, 2013, 4:52:15 AM2/28/13
to gat...@googlegroups.com
There's a point missing:
val myGlobalVar = new java.util.concurrent.atomic.AtomicInteger(0)

Alexandru Geana

unread,
Feb 28, 2013, 5:20:35 AM2/28/13
to gat...@googlegroups.com
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?

Stéphane Landelle

unread,
Feb 28, 2013, 5:25:02 AM2/28/13
to gat...@googlegroups.com
Ah, sorry, I didn't understand that as what you initially wrote was indeed concatenating.

Very simple:

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



2013/2/28 Alexandru Geana <hex...@gmail.com>

Alexandru Geana

unread,
Feb 28, 2013, 5:33:14 AM2/28/13
to gat...@googlegroups.com
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".

Stéphane Landelle

unread,
Feb 28, 2013, 5:36:55 AM2/28/13
to gat...@googlegroups.com
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)


2013/2/28 Alexandru Geana <hex...@gmail.com>

Stéphane Landelle

unread,
Feb 28, 2013, 5:37:24 AM2/28/13
to gat...@googlegroups.com
Missing brace
session.getTypedAttribute[String]("rid").toInt + myGlobalVar.getAndIncrement


2013/2/28 Stéphane Landelle <slan...@excilys.com>

Alexandru Geana

unread,
Feb 28, 2013, 6:18:03 AM2/28/13
to gat...@googlegroups.com
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.getTypedAttribute[String]("rid").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. :|

Stéphane Landelle

unread,
Feb 28, 2013, 6:30:49 AM2/28/13
to gat...@googlegroups.com
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
)


2013/2/28 Alexandru Geana <hex...@gmail.com>
It is still concatenating the values, the rid value comes from a POST response from a JSON 

Alexandru Geana

unread,
Feb 28, 2013, 7:02:57 AM2/28/13
to gat...@googlegroups.com
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!

Stéphane Landelle

unread,
Feb 28, 2013, 7:10:34 AM2/28/13
to gat...@googlegroups.com
No pro.

Have fun!


2013/2/28 Alexandru Geana <hex...@gmail.com>

anuja dhawale

unread,
Nov 9, 2016, 1:25:22 AM11/9/16
to Gatling User Group, slan...@excilys.com


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

ssegu...@gmail.com

unread,
Jan 9, 2018, 3:06:21 AM1/9/18
to Gatling User Group

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

artemdwo

unread,
Jan 30, 2018, 10:32:59 AM1/30/18
to Gatling User Group
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)

David Baláček

unread,
May 30, 2019, 2:06:26 PM5/30/19
to Gatling User Group
Is there a way for string?

Dne čtvrtek 28. února 2013 10:33:28 UTC+1 Alexandru Geana napsal(a):
Reply all
Reply to author
Forward
0 new messages