Choose one particular value from the feeder.

Hi,
I can’t think of a way how do I make it syntactically correct. I have a feeder csv file which looks like:
passNum,passVal
1,aabb
2,bbaa
3,ssdd
and so on

In the beginning of the scenario I feed it:
.exec(feed(feeder))

From one of the replies body I parse a number and save it as a session parameter “paswordNumber”. And in the next request I need to take the appropriate password from the feeder file. For example, if “passwordNumber” was “1” then I need to take “aabb” from the feeder. I also created “password” empty session attribute that will hold my password. Now It looks like this:
def findPassword =
exec(session => session.set(“password”, ${passVal})
)

How do I write that I need such ${passVal} which ${passNum} == ${passwordNumber}? Thank you in advance.

Then, Feeders are not intended for this usage.
What you need is to convert your data to a Map indexed by passNum.

You can still use Gatling facilities:

val passwordsByNumber: Map[Int, String] =
csv(“passwords.csv”).records.map(record => record(“foo”).toInt → record(“bar”)).toMap

// once you have the passwordNumber, fetch and inject the
.exec(session => session(“passNum”).validate[Int].map(number => session.set(“passVal”, passwordsByNumber(number))))

Get it?

More or less. I followed your example:

val passwords = csv(“passwords.csv”)
val passwordsByNumber: Map[Int, String] =
passwords.records.map(record =>
record(“passNum”).toInt → record(“passVal”)).toMap

def findPassword =
exec(session => session(“passwordNumber”)
.validate[Int]
.map(number =>
session.set(“password”, passwordsByNumber(number))))

When I tried it out, my “passwordNumber” was parsed as 2. However, “passNum” was 1 and “passVal” was the password for 1 and not for 2. And “password” was empty. Did I miss something?

There’s a problem on your side.
There’s no way such an offset could occur due to the algorithm I suggested.

What could the problem be? I might got confused with the variables. So right now, as a debugging, i’m using this:

val passwords = csv(“passwords.csv”)
def setPasswordNumber = exec(session =>
session.set(“passwordNumber”, “3”))

val passwordsByNumber: Map[Int, String] =
passwords.records.map(record =>
record(“passNum”).toInt → record(“passVal”)).toMap

def findPassword =
exec(session => session(“passwordNumber”)
.validate[Int]
.map(number =>
session.set(“password”, passwordsByNumber(number))))

.exec(setPassNum)
.exec(findPassword)
.exec((session: Session) => {
println(session.get(“counter”))
session})

passwordNumber - the number of password that I need to receive from the csv file (third in this case)
passNum, passVal - two columns of the passwords.csv file
password - this will be set with the appropriate passVal value

After execution:
passwordNumber → 3
passNum → 1
passVal → aabb (first password)
and no attribute for password

I am using Gatling2.0.0-RC3. What could the problem be?

Could you please share a GIST with your WHOLE simulation, please?
Here, you’re calling a .exec(setPassNum) and setpassNum is missing from what you pasted.

https://gist.github.com/anonymous/e1ba8c4f508f25c8255e
setPassNum was a typo: setPasswordNumber

I have a pretty good idea about what happens…

You originally said:

From one of the replies body I parse a number and save it as a session parameter “paswordNumber”.

So my sample is based on this:

  • I made passwordsByNumber a map[String, Int]

  • I expected the attribute to be an Int (validate[Int])
    Now, you’re doing session.set(“passwordNumber”, “3”), so you’re setting a String, not an Int.

Thanks! Got it to work. Another thing is how can I transform session attribute into type Int. Is it something like:
Integer.parseInt(session(“passwordNumber”))

session(“passwordNumber”).as[String].toInt

But you can also use String everywhere instead.

So if I need to make a POST request and put a password as a parameter is there any difference between:
.formParam(“password”, “1111333”)
and
.formParam(“password”, 1111333)

No