# Declare and increment a global variable.

**URL:** <https://community.gatling.io/t/declare-and-increment-a-global-variable/284>\
**Category:** Gatling (Open-Source)\
**Created:** [February 28, 2013, 9:33am UTC](https://community.gatling.io/t/declare-and-increment-a-global-variable/284 "2013-02-28T09:33:28Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alexandru\_Geana](https://avatars.discourse-cdn.com/v4/letter/a/46a35a/32.png) [@Alexandru\_Geana](https://community.gatling.io/u/Alexandru_Geana)\
**Post date:** [February 28, 2013, 9:33am UTC](https://community.gatling.io/t/declare-and-increment-a-global-variable/284/1 "2013-02-28T09:33:28Z")

</div>

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  
)

---

<div class="post-metadata">

**Author:** ![Alexandru\_Geana](https://avatars.discourse-cdn.com/v4/letter/a/46a35a/32.png) [@Alexandru\_Geana](https://community.gatling.io/u/Alexandru_Geana)\
**Post date:** [February 28, 2013, 9:36am UTC](https://community.gatling.io/t/declare-and-increment-a-global-variable/284/2 "2013-02-28T09:36:01Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Excilys](https://avatars.discourse-cdn.com/v4/letter/e/8797f3/32.png) [@Excilys](https://community.gatling.io/u/Excilys)\
**Post date:** [February 28, 2013, 9:41am UTC](https://community.gatling.io/t/declare-and-increment-a-global-variable/284/3 "2013-02-28T09:41:26Z")

</div>

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}\*"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;)).asXML  
)

Cheers,

Stéphane

---

<div class="post-metadata">

**Author:** ![Alexandru\_Geana](https://avatars.discourse-cdn.com/v4/letter/a/46a35a/32.png) [@Alexandru\_Geana](https://community.gatling.io/u/Alexandru_Geana)\
**Post date:** [February 28, 2013, 9:46am UTC](https://community.gatling.io/t/declare-and-increment-a-global-variable/284/4 "2013-02-28T09:46:43Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Alexandru\_Geana](https://avatars.discourse-cdn.com/v4/letter/a/46a35a/32.png) [@Alexandru\_Geana](https://community.gatling.io/u/Alexandru_Geana)\
**Post date:** [February 28, 2013, 9:49am UTC](https://community.gatling.io/t/declare-and-increment-a-global-variable/284/5 "2013-02-28T09:49:57Z")

</div>

Found it!

a small typo

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

vs

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

---

<div class="post-metadata">

**Author:** ![Excilys](https://avatars.discourse-cdn.com/v4/letter/e/8797f3/32.png) [@Excilys](https://community.gatling.io/u/Excilys)\
**Post date:** [February 28, 2013, 9:52am UTC](https://community.gatling.io/t/declare-and-increment-a-global-variable/284/6 "2013-02-28T09:52:15Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Alexandru\_Geana](https://avatars.discourse-cdn.com/v4/letter/a/46a35a/32.png) [@Alexandru\_Geana](https://community.gatling.io/u/Alexandru_Geana)\
**Post date:** [February 28, 2013, 10:20am UTC](https://community.gatling.io/t/declare-and-increment-a-global-variable/284/7 "2013-02-28T10:20:35Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Excilys](https://avatars.discourse-cdn.com/v4/letter/e/8797f3/32.png) [@Excilys](https://community.gatling.io/u/Excilys)\
**Post date:** [February 28, 2013, 10:25am UTC](https://community.gatling.io/t/declare-and-increment-a-global-variable/284/8 "2013-02-28T10:25:02Z")

</div>

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

Very simple:

---

<div class="post-metadata">

**Author:** ![Alexandru\_Geana](https://avatars.discourse-cdn.com/v4/letter/a/46a35a/32.png) [@Alexandru\_Geana](https://community.gatling.io/u/Alexandru_Geana)\
**Post date:** [February 28, 2013, 10:33am UTC](https://community.gatling.io/t/declare-and-increment-a-global-variable/284/9 "2013-02-28T10:33:14Z")

</div>

**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”.

---

<div class="post-metadata">

**Author:** ![Excilys](https://avatars.discourse-cdn.com/v4/letter/e/8797f3/32.png) [@Excilys](https://community.gatling.io/u/Excilys)\
**Post date:** [February 28, 2013, 10:36am UTC](https://community.gatling.io/t/declare-and-increment-a-global-variable/284/10 "2013-02-28T10:36:55Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![Excilys](https://avatars.discourse-cdn.com/v4/letter/e/8797f3/32.png) [@Excilys](https://community.gatling.io/u/Excilys)\
**Post date:** [February 28, 2013, 10:37am UTC](https://community.gatling.io/t/declare-and-increment-a-global-variable/284/11 "2013-02-28T10:37:24Z")

</div>

Missing brace  
session.getTypedAttributeString.toInt + myGlobalVar.getAndIncrement

---

<div class="post-metadata">

**Author:** ![Alexandru\_Geana](https://avatars.discourse-cdn.com/v4/letter/a/46a35a/32.png) [@Alexandru\_Geana](https://community.gatling.io/u/Alexandru_Geana)\
**Post date:** [February 28, 2013, 11:18am UTC](https://community.gatling.io/t/declare-and-increment-a-global-variable/284/12 "2013-02-28T11:18:03Z")

</div>

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. 😑

---

<div class="post-metadata">

**Author:** ![Excilys](https://avatars.discourse-cdn.com/v4/letter/e/8797f3/32.png) [@Excilys](https://community.gatling.io/u/Excilys)\
**Post date:** [February 28, 2013, 11:30am UTC](https://community.gatling.io/t/declare-and-increment-a-global-variable/284/13 "2013-02-28T11:30:49Z")

</div>

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\*\*}\*"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;)).asXML  
)

---

<div class="post-metadata">

**Author:** ![Alexandru\_Geana](https://avatars.discourse-cdn.com/v4/letter/a/46a35a/32.png) [@Alexandru\_Geana](https://community.gatling.io/u/Alexandru_Geana)\
**Post date:** [February 28, 2013, 12:02pm UTC](https://community.gatling.io/t/declare-and-increment-a-global-variable/284/14 "2013-02-28T12:02:57Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![Excilys](https://avatars.discourse-cdn.com/v4/letter/e/8797f3/32.png) [@Excilys](https://community.gatling.io/u/Excilys)\
**Post date:** [February 28, 2013, 12:10pm UTC](https://community.gatling.io/t/declare-and-increment-a-global-variable/284/15 "2013-02-28T12:10:34Z")

</div>

No pro.

Have fun!

---

<div class="post-metadata">

**Author:** ![anuja\_dhawale](https://avatars.discourse-cdn.com/v4/letter/a/dfb087/32.png) [@anuja\_dhawale](https://community.gatling.io/u/anuja_dhawale)\
**Post date:** [November 9, 2016, 6:25am UTC](https://community.gatling.io/t/declare-and-increment-a-global-variable/284/16 "2016-11-09T06:25:22Z")

</div>

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

}

---

<div class="post-metadata">

**Author:** ![sseguradata](https://avatars.discourse-cdn.com/v4/letter/s/97f17d/32.png) [@sseguradata](https://community.gatling.io/u/sseguradata)\
**Post date:** [January 9, 2018, 8:06am UTC](https://community.gatling.io/t/declare-and-increment-a-global-variable/284/17 "2018-01-09T08:06:21Z")

</div>

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](https://gatling.io/docs/2.3/general/scenario/#scenario-loops) 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!

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

```

---

<div class="post-metadata">

**Author:** ![artemdwo](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@artemdwo](https://community.gatling.io/u/artemdwo)\
**Post date:** [January 30, 2018, 3:32pm UTC](https://community.gatling.io/t/declare-and-increment-a-global-variable/284/18 "2018-01-30T15:32:59Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![David\_Balacek](https://avatars.discourse-cdn.com/v4/letter/d/ba8739/32.png) [@David\_Balacek](https://community.gatling.io/u/David_Balacek)\
**Post date:** [May 30, 2019, 3:21pm UTC](https://community.gatling.io/t/declare-and-increment-a-global-variable/284/19 "2019-05-30T15:21:42Z")

</div>

Is there a way for string?

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