Using feeder to build query string

Hi everyone,

I want to hit a url with a query string built based on a feeder. It’s easier with an example:

ids.csv
id
1
2
3
4
5

http://localhost?ids=1,2,3

Each time I run the scenario, I’d like to have a different number of randomly picked IDs.
http://localhost?ids=2,5

http://localhost?ids=1,3,5

etc.

I’ve tried to do the following but it doesn’t work.

val scn = scenario(“My scenario”)
.feed(csv(“ids.csv”).random)
.exec(
http(“request_1”)
.get(session => “?ids=” + buildIdList)
.check(status.is(200)))

// Will create a string like “{id}{id}{id}” with a random number of {id}
def buildIdList = (0 to new Random().nextInt(10) + 1).map(n => “${id}”).toList.mkString(",")

I was hoping Gatling will replace each {id} in the built query string with a value from the feeder but id doesn’t.

I’d appreciate if you have any ideas. Thanks,

Ariel

Hi,

Fist, you won’t be able to directly use the whole Feeder API, but you can re-use the Feeder utilities.

Then, I might be wrong, but it’s more likely that you actually need unique randomly picked ids.
If so, a simple approach would be to inject into a Set, but if your possible values list is not big enough, collisions would be high, so your distribution would change and you’d need a more complex approach.

With Gatling 2:

val data: Array[Map[String, String]] = csv(“ids.csv”).data // get the underlying array

val feedIds = (session: Session) => {
val random = ThreadLocalRandom.current
val ids = (0 to random.nextInt(10 + 1).map(n = data(n)(“id”).toSet.mkString(",")
session.set(“ids”, ids)
}

exec(feedIds)
.exec(http(“request_1”).get("?ids=${ids}").check(status.is(200)))

}

Get it?

Cheers,

Stéphane

Hi Stéphane,

Thanks for the quick response. I’ve been trying to make it work but I haven’t been successful.

exec seems to take a Session => Validation[Session] as a parameter, so I’ve changed the last line of feedIds to be

new Success(session.set(“ids”, ids))

I set the log level to DEBUG and when I try to run the simulation, apparently no requests are being made. I just read

14:53:46.947 [DEBUG] i.g.c.r.Runner - Finished Launching scenarios executions
14:53:46.949 [INFO ] i.g.c.a.UserStart - Start user #0
14:53:46.949 [INFO ] i.g.c.a.UserEnd - End user #0
14:53:48.076 [INFO ] i.g.c.a.UserStart - Start user #1
14:53:48.076 [INFO ] i.g.c.a.UserEnd - End user #1

Does it ring any bell? Thanks a lot,

Ariel

Hi Stéphane,

Thanks for the quick response. I've been trying to make it work but I
haven't been successful.

exec seems to take a Session => Validation[Session] as a parameter, so
I've changed the last line of feedIds to be

new Success(session.set("ids", ids))

There's an implicit conversion in core.Predef that turns T into
Validation[T], so you should have been fine. Do you import core.Predef?

I set the log level to DEBUG and when I try to run the simulation,
apparently no requests are being made. I just read

14:53:46.947 [DEBUG] i.g.c.r.Runner - Finished Launching scenarios
executions
14:53:46.949 [INFO ] i.g.c.a.UserStart - Start user #0
14:53:46.949 [INFO ] i.g.c.a.UserEnd - End user #0
14:53:48.076 [INFO ] i.g.c.a.UserStart - Start user #1
14:53:48.076 [INFO ] i.g.c.a.UserEnd - End user #1

Does it ring any bell? Thanks a lot,

Usually happens when once forgets a dot, so that execs are not properly
chained.

Could you share a gist, please?

https://gist.github.com/arikogan/7084579

Regarding the implicit not happening, my fault (directly coded in gmail): you need to declare expected type to make implicit conversion happen:

import io.gatling.core.session.Expression

val feedIds: Expression[Session] = …

Otherwise, the “cool” way is:

import io.gatling.core.validation._

session.set(“ids”, ids).success

Regarding the “no requests”, as I expected, there’s a missing dot line 25 in your gist:

https://gist.github.com/arikogan/7084579#file-retrieveids-scala-L25

That’s great, that did work Stéphane. Thank you.

I’m still confused about when to use the methods imported by import bootstrap._ and when not to. That was the problem in this thread. If you can shed some light on that, that would be helpful.

bootstrap is actually an object. importing bootstrap._ puts all its methods in scope, like exec.

It will be removed in M4.