scalagatling

Access randomly a line in an array of map using feeders


I am trying to do a very simple operation but it gets really complecated when we are beginners in scala/gatling.

I just need to randomly pick a line in an array of map at every request...

I work on a JSON that looks like that:

[
 { "name": "a", "age": 33 },
 { "name": "b", "age": 34 },
 { "name": "c", "age": 38 }
]

So here is a draft what i am trying to do:

val user = feed(jsonFile("peoples.json"))
  .exec( session => {
    val someone = session[random()] // How to code that ?
    http(someone.age)
      .get("/users/${someone.name}")
  })

val scn = scenario("").exec(user)

The session seems to contain the user datas, but i don't know how to access it to build the path of my http request!

I have seen many different ways to do things that could be close to what i am trying to achieve but i couldn't find a clear working example.


Solution

  • Check Feeders Doc: https://docs.gatling.io/reference/script/core/session/feeders/

    You can basically just use random feeder: feed(jsonFile("peoples.json")).random

    Then you can access it with DSL:

    val myFeeder = jsonFile("peoples.json").random
    val userRequest = exec(http("someones ${age}")
          .get("/users/${name}"))
    
    val scn = scenario("x").exec(feed(myFeeder),userRequest)