creating a cache of responses in gatling simulation

49 Views Asked by At

Here is my basic setup:

running 2 scenarios in the simulation below in sequence

  • the first scenario is trying to populate a cache based on some http responses
  • the second is running test queries that use the cache populated in the first scenario

My problem is that the first scenario only reads one item from the feeder defined for it. I can't get it to read all the items defined in the CSV file. Is what I'm trying to do here possible?

Here's the code. Sorry for the length. I tried to distill it down to its basic essence. I'm using gatling 3.4.2

class ExampleSimulation extends Simulation {
  var DEFAULT_PAGE_SIZE = 100
  var numUsersByAccount = scala.collection.mutable.Map[String, Int]()

  // calculate middle page given total number of items and page size
  def getMiddlePage(numItems: Int, pageSize: Int)  { 50 }

  val cacheLoad = {
    feed(csv("file.csv").eager.queue)
      // following gets the number of users and saves it in session
      .exec(http("cacheWarmupQuery")
        .get("/accounts/${accountId}")
        .check(status.is(200))
        .check(jsonPath("$.numUsers").saveAs("numUsers")))

      // following caches numUsers
      .exec { session => {
        val numPrincipals = session("numPrincipals").as[Int]
        val accountId = session("accountId").as[String]
        numUsersByAccount.put(accountId, numPrincipals)
        println("put numUsersByAccount " + accountId + " = " + numPrincipals)
        session
      }}
  }

  val searches = {
    feed(csv("file.csv").circular)
      .exec { session => {
        val accountId = session("accountId").as[String]
        val numUsers = numUsersByAccount(accountId)
        println ("got " + numUsers + " users for account "+ accountId)
        val middlePage = getMiddlePage(numUsers, DEFAULT_PAGE_SIZE)
        val newSession = session.set("middlePage", middlePage)
        newSession
      }}

      .exec(http("testQuery")
        .get("/accounts/${accountId}")
        .queryParam("page", "${middlePage}")
        .check(status.is(200)))
  }

  setUp(
    scenario("cacheWarmup").exec(cacheLoad).inject(atOnceUsers(1))
      .andThen(
        scenario("test")
          .exec(searches)
          .inject(constantUsersPerSec(10) during Duration(10, "seconds")))
  )
}
0

There are 0 best solutions below