Search code examples
gatling

How do I bulk/chunk paginate existing Seq[(String)] session value in Gatling?


I am executing a call that saves a lot of values into a Seq[(String)], it looks as follows:

.exec(session => {session.set("Ids", session("externalIds").as[Seq[String]])})

There is a reason why I have to create another session variable called Ids our of externalIds but I wont get into it now.

I than have to execute another call and paginate 10 values out of ${Ids} until I send them all. (So in case of 100 values, I'll have to execute this call 10 times) The JSON looks as follows:

..."Ids": [
    "962950",
    "962955",
    "962959",
    "962966",
    "962971",
    "962974",
    "962978",
    "962983",
    "962988",
    "962991"
],...

What I usually do when I have to iterate through one value each time is simply:

  .foreach("${Ids}", "id") {
              exec(getSomething)
          }

But since I need to send a [...] Of 10 values each, I am not sure if it should even be in the scenario level. Help! :)


Solution

  • I couldn't figure out how to go about this within the session so I took it outside to a function and here is the solution:

     .exec(session => {session.set("idSeqList", convertFileIdSeqToFileIdSeqList(session("idsSeq").as[Seq[String]]))})
    
     def convertFileIdSeqToFileIdSeqList(idSeq: Seq[String]): Seq[Seq[String]] = {
        idSeq.grouped(10).toList
      }
    

    Note that when placing your list within a JSON body, you will need to use .jsonStringify() to format it correctly in the JSON context like so:

      "ids": ${ids.jsonStringify()},