Injecting User Data

Hello!

I’ve got a scenario in which I want to read a variety of data and have the simulation use a random selection of that data. I have a feeder that creates users in our application and for each user(feed), they should utilize a random piece of provided data

I tried referencing
https://github.com/excilys/gatling/wiki/Inject-User-Specific-Data and https://github.com/excilys/gatling/blob/81a3e10347e2c3e9c774771dca1fb4b8abc8a129/src/sphinx/session/feeder.rst#non-shared-data, but I believe those target Gatling v1 and I am using Gating 2 snapshot

There might be a better overall solution than using csv as not a feeder

Thanks

Hi,

What’s your problem exactly? What have you done so far, and what’s not working?

So what I’ve tried so far is roughly the following

//setup
val itemIds = csv(“item_ids.csv”).groupBy(_(“item_id”))

val injectProductIds = (session: Session) => {
session.set(“itemIds”, itemIds.toSeq)
}

//execution
val scn = scenario(“Sale”)
.feed(csv(“fake_users.csv”))
.exec(
//account set up with fake_users.csv
)
.exec(injectItemIds)
.exec(
//continue on normally
)

//item_ids.csv
item_id
1
2
3
4

I’m trying to simulate having certain things on sale to see what happens when a user wants to buy a particular subset of items. Ideally I want item ids to be set in some external file so that other engineers don’t have to alter code and it’s a bit more flexible for different testing environments

I just want to be able to aggregate all of the item_ids so that I can assign one to a user session randomly. The random assignment per user session is already working for a different simulation that I’ve completed, so that’s not an issue

The above code fails to compile with the following two errors:

value groupBy is not a member of io.gatling.core.feeder.RecordSeqFeederBuilder[String]

and

overloaded method value exec with alternatives:
(scenario: io.gatling.core.structure.ScenarioBuilder)io.gatling.core.structure.ScenarioBuilder
(chains: Iterable[io.gatling.core.structure.ChainBuilder])io.gatling.core.structure.ScenarioBuilder
(chains: Iterator[io.gatling.core.structure.ChainBuilder])io.gatling.core.structure.ScenarioBuilder
(chains: io.gatling.core.structure.ChainBuilder*)io.gatling.core.structure.ScenarioBuilder
(actionBuilder: io.gatling.core.action.builder.ActionBuilder)io.gatling.core.structure.ScenarioBuilder
(sessionFunction: io.gatling.core.session.Session => io.gatling.core.validation.Validation[io.gatling.core.session.Session])io.gatling.core.structure.ScenarioBuilder
cannot be applied to (io.gatling.core.Predef.Session => io.gatling.core.session.Session)
12:31:46.395 [ERROR] i.g.a.ZincCompiler$ - .exec(injectItemIds)

Did you check RecordSeqFeederBuilder source? https://github.com/excilys/gatling/blob/master/gatling-core/src/main/scala/io/gatling/core/feeder/FeederBuilder.scala#L28
The actual data is the records fields.

There’s actually many ways of achieving this, but what I can suggest really depends on how familiar you are with Scala and the Scala Collection API.
I’ll be assuming you’re a beginner (no offense).

The easiest way for you is probably this way:

  1. groupBy all your data by item_id so you can easily fetch products with the same item_id
  2. build a feeder of the item_ids
  3. feed on this feeder
  4. have an exec(session) step where you push the items of the current item_id
  5. loop on the item with a foreach loop
  6. copy the item (which is a Map) content as individual attributes

// 1

val groupedRecords = csv(“item_ids.csv”).records.groupBy(record => record(“item_id”))

// 2
val itemIdFeeder = groupedRecords.keys.map(itemId => Map(“item_id” → itemId)).toIndexedSeq

val scn = scenario(“foo”)
// 3
.feed(itemIdFeeder)
// 4
.exec { session =>
session.set(“items”, groupedRecords(session(“item_id”).as[String]))
}
// 5
.foreach("${items}", “item”) {
// 6 there’s now a build-in for this, so you can write exec(flattenMapIntoAttributes("${item}")) instead
exec{session => val item = session(“item”).as[Map[String, String]] session.setAll(item) }

//…
}

Get it?

Cheers,

Stéphane

You’re assumption is correct and I am definitely not offended :slight_smile:

I think maybe I wasn’t being clear in my explanation. Thanks to your code snippets, I was able to figure it out and I’m going to put it here in case it helps anyone else. And if you see anything obviously wrong, please point it out

//setup
val itemIds = csv(“item_ids.csv”).records.groupBy(_(“item_id”)).keys.toSeq

//execution
val scn = scenario(“Sale”)
.feed(csv(“fake_users.csv”))
.exec(
//account set up with fake_users.csv
)
.exec(session => {
session.set(“itemIds”, itemIds)
})
.exec(
//continue on normally
)

//later in the simulation
exec(session => {
val saleItemIds = session(“itemIds”).as[Seq[String]]
session.set(“id”, Random.shuffle(saleItemIds).head)
})

I don’t really want to feed all of the item ids into the simulation, just needed to read a set and to randomly pick one later in the simulation

Thank you for all of your help, I appreciate it