Random .param name

Hi!
Thanks for the Gatling tool. It is amazing. However, I came across a problem, that I am not yet able to resolve myself. I have a .param with a name, for example, “”“deleteForm3_hf_0"”". The part after “hf” is generated dynamically. How can I write the script so that this parameter name would not matter?

Hi,

I’m not sure what you mean.
Do you need to generate this name randomly, or capture it from the page?

If the former, you can try something like:
session => “deleteForm3_hf_” + ThreadLocalRandom.current.nextInt(0, 100)

If the latter, check out https://github.com/excilys/gatling/wiki/Checks

For example, if in:
.get("/delete_this_declaration-12-delete") link, I want “12” as a random number, would it be syntactically correct to use:

.get("/delete_this_declaration-"+ThreadLocalRandom.current.nextInt(0, 100)+"-delete")

As I wrote in my previous email, you have to pass a function. Otherwise, your parameter will only be a value and it will be evaluated only once, when the Simulation is loaded.

Ok. I only started with Gatling and Scala yesterday, so could you give me a tip on where can I find some info on my problem, as I didn’t understand much from the last email. But thank you for your assist!

Doc: https://github.com/excilys/gatling/wiki/Advanced-Usage#scala-functions

Your previous example:
.get("/delete_this_declaration-"+ThreadLocalRandom.current.nextInt(0, 100)+"-delete") // wrong, get parameter is only evaluated once

.get(session => “/delete_this_declaration-”+ThreadLocalRandom.current.nextInt(0, 100)+"-delete") // right, get parameter is a lambda that will be evaluated for every request to be sent

Thanks! I got this to work. But what if instead of random ThreadLocalRandom.current.nextInt(0, 100) I needed a number that would increment for each session. For first user it would be 1, for second - 2 etc. What could I use then?

Back to basics, maybe? :slight_smile:
http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/atomic/AtomicInteger.html#incrementAndGet()

So I should declare a variable in scenario and make it 0. Then I insert it where I need it and put increment? Something like:

val number = 0;

scn(…)
.get(session => “somePartOfUrl_”+incrementAndGet(number)+"_someOtherPartOfUrl")

Close :slight_smile:

val number = new AtomicInteger(); // default value is 0

scn(…)
.get(session => “somePartOfUrl_”+ number. incrementAndGet +"_someOtherPartOfUrl")

Thanks! Worked like a charm. Is it ok that I ask such noobish questions here?

No pro, I don’t mind explaining simple things as long as I don’t have to explain them twice :slight_smile: