Random .param name

148 views
Skip to first unread message

Andrei Proskurin

unread,
Jul 1, 2014, 4:43:45 AM7/1/14
to gat...@googlegroups.com
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?

Stéphane Landelle

unread,
Jul 1, 2014, 5:09:46 AM7/1/14
to gat...@googlegroups.com
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)



2014-07-01 10:43 GMT+02:00 Andrei Proskurin <andrei.pr...@gmail.com>:
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?

--
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/d/optout.

Andrei Proskurin

unread,
Jul 1, 2014, 5:20:31 AM7/1/14
to gat...@googlegroups.com
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")

Stéphane Landelle

unread,
Jul 1, 2014, 5:22:59 AM7/1/14
to gat...@googlegroups.com
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.

Andrei Proskurin

unread,
Jul 1, 2014, 5:38:56 AM7/1/14
to gat...@googlegroups.com
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!

Stéphane Landelle

unread,
Jul 1, 2014, 5:48:01 AM7/1/14
to gat...@googlegroups.com
Doc: https://github.com/excilys/gatling/wiki/Advanced-Usage#scala-functions

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

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

Andrei Proskurin

unread,
Jul 4, 2014, 4:00:23 AM7/4/14
to gat...@googlegroups.com
Thanks! I got this to work. But what if instead of random ThreadLocalRandom.current.nextInt(0100) 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?

Stéphane Landelle

unread,
Jul 4, 2014, 4:14:04 AM7/4/14
to gat...@googlegroups.com

Andrei Proskurin

unread,
Jul 4, 2014, 4:20:37 AM7/4/14
to gat...@googlegroups.com
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")

Stéphane Landelle

unread,
Jul 4, 2014, 4:47:42 AM7/4/14
to gat...@googlegroups.com
Close :)


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


scn(...)
.get(session => "somePartOfUrl_"+ number. incrementAndGet +"_someOtherPartOfUrl")
Message has been deleted

Andrei Proskurin

unread,
Jul 4, 2014, 4:51:47 AM7/4/14
to gat...@googlegroups.com
Thanks! Worked like a charm. Is it ok that I ask such noobish questions here?

Stéphane Landelle

unread,
Jul 4, 2014, 5:21:42 AM7/4/14
to gat...@googlegroups.com
No pro, I don't mind explaining simple things as long as I don't have to explain them twice :)
Reply all
Reply to author
Forward
0 new messages