Dynamic parameters

I have a dynamic part in url which changes when you move through urls. For example you start with “someurl_1” and when you click somewhere you get “someurl_2”. We can write function that will insert this part of url:

val dynamicValue = new AtomicInteger(1)

def dynamicPart = {
“someurl_” + dynamicValue.getAndIncrement
}

Then this value will also be dynamic. However when I put a couple of users, I have the following problem:

Users don’t start with new dynamicValue, but they increment the existing. I need every user to start with dynamicValue = new AtomicInteger(1). How can I achieve it?

If you write a loop, you can set the index counter name, which is a session attribute, then access, for example with Gatling EL.

https://github.com/excilys/gatling/wiki/Structure-Elements#repeat

repeat(5, “foo”) {
exec(http(“request”).get(“someurl_${foo}”))
}

If you don’t want a loop, you can do it manually: store a counter in the session and increment it the way you want.

What if I have the following function:

def delete = {
getIt(“Delete”, session => “/?”

  • generateDynamic(0)
  • “SomeUrlPart1_”
  • someCounter.getAndIncrement
  • “_SomeUrlOtherPart”)
    }

And I need the counter to work with session (as it does, and it works properly), but the generateDynamic doesn’t need to work with session (so I get proper values for each user). Is it overkill and I need to find another solution?

No, that’s not overkill, that’s the only way.

Some comments:

  • if you don’t use the session, you can omit the parameter name (use the _ wildcard)

  • you don’t need the {} as you only have one instruction

  • use a Scala String interpolation instead of concatenating Strings

def delete = getIt(“Delete”, _ => s"/?${generateDynamic(0)}SomeUrlPart1_${someCounter.getAndIncrement}_SomeUrlOtherPart")

Isn’t this way cleaner?

I need to use session for ${someCounter.getAndIncrement}, but I don’t need it for ${generateDynamic(0)}. If I understand correctly then using _ omits session for both of the functions. And what does “s” in _ => s stand for?

I need to use session for ${someCounter.getAndIncrement},

Where???

but I don't need it for ${generateDynamic(0)}. If I understand correctly

then using _ omits session for both of the functions.

No, _ just omit naming a parameter that you don't use, yet need to meet the
expected signature.

And what does "s" in _ => s stand for?

I mentioned "Scala String interpolation", did you try googling it?

Ok I got things clear with the string interpolation. Regarding the session:

If I use:

def delete = getIt(“Delete”, session => s"/?${generateDynamic(0)}SomeUrlPart1_${someCounter.getAndIncrement}_SomeUrlOtherPart")

I will get someCounter being incremented just the way I need it. However, ${generateDynamic(0)} also has increment method. It needs to be incremented the other way. Like this:

def delete = getIt(“Delete”, s"/?${generateDynamic(0)}SomeUrlPart1_${someCounter.getAndIncrement}_SomeUrlOtherPart")

Here, however, ${someCounter.getAndIncrement} doesn’t get incremented as needed. So I am lost at how to combine these two. I apologize for asking so much questions, but I just cannot figure it out myself.

So is it possible to make:
def delete = getIt(“Delete”, session => s"/?${generateDynamic(0)}SomeUrlPart1_${someCounter.getAndIncrement}_SomeUrlOtherPart")

where “session =>” is used for “someCounter.getAndIncrement” and ignored for “generateDynamic(0)”?
Or it is very messy and I need to look for another solution?

where “session =>” is used for “someCounter.getAndIncrement” and ignored for “generateDynamic(0)”?

This doesn’t make any sense.
Neither “someCounter.getAndIncrement” nor “generateDynamic(0)” use the session parameter.

They are both part of a URL which is type:
io.gatling.core.session.Expression[String]

In some other topic you told me that if I want to get someCounter incremented for each session, I needed to change the url type to io.gatling.core.session.Expression[String] and then use “Session =>”. And it works perfectly.

“SomeCounter” gets incremented for each session. However, the other part of url “generateDynamic” doesn’t need to be incremented the same way (because it breaks the dynamic url building). So I am confused, how can I make because I cannot just remove “Session =>”

You’re confusing the fact that you have to pass a function (whose expected type is Session => something) so the function body is evaluated on every request instead of a regular value that’s only evaluated once, and the fact that the function input parameter is indeed used or not.

However, the other part of url “generateDynamic” doesn’t need to be incremented the same way (because it breaks the dynamic url building).

So how is it supposed to be incremented.

the generateDynamic has to be equal for every user. For example, if it starts with value “0” and through the simulation gets to “3”, then for the second user it has to be the same (starts with “0”) but not to continue with “3” and so on. This is how it operates now:
getIt(“Delete”, session => s"/?${generateDynamic(0)}SomeUrlPart1_${someCounter.getAndIncrement}_SomeUrlOtherPart")

So if I get it right, the 0 to 3 counter is user scoped.
Then, you can either use a loop where you explicitly set the counter name, or build your own counter that you store in the user session.

I think that building my own counter, that will be stored in the user session is more suitable for my case. Do I achieve this by

session.setAttribute(${myCounter}, new AtomicInteger()) ?

That’s the easiest solution, yes.

I got a bit confused about how exactly do I make a session attribute using
session.set(${myCounter}, new AtomicInteger())

Should val myCounter be already declared?
Where in my scenario I need to do “session.set” so it would be available through whole scenario?
Some example would be good.

session.set(“myCounter”, new AtomicInteger())

I have the following scenario:
val scenarioAddDeclarations = scenario(“Lisa uued deklaratsioonid1”)
.exec(loginProcess)
.exec(addNew(“1-1.”))
.exec(addNewYearMonth(“2-1.”))
.exec(addParameters(“4-1.”))
.exec(backToList(“5-2.”))

At the moment “1-1.” and other are static. I have written a method “generateDynamic” which does the incrementing. I also have val p1 and val p2 which I am incrementing. So it has to be like:

.exec(session.set(p1, generateDynamic) session.set(p2.generateDynamic) => addNew("${p1}-${p2}."))

Because I still have no idea how to use the sessions.