Search code examples
scalagatling

Value for a session is replicated between sessions


I have a load test where a session value is set depending on the URL that I request and can be one of two options which I choose randomly.

When I execute the test for one user, the value in the session is set successfully with a random value.

When I add more users the value would be set randomly but also the same for all the sessions of all users (so all users have the same session value)

My load test looks like this

class test extends Simulation {
  val customer_types = Array("P", "B")

  val httpProtocol = http
    .baseUrl("https://www.test.com")
    .inferHtmlResources()
    .acceptHeader("*/*")
    .acceptEncodingHeader("gzip, deflate")
    .acceptLanguageHeader("en-US,en;q=0.5")
    .userAgentHeader("Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:67.0) Gecko/20100101 Firefox/67.0")

  val homepage = exec(http("homepage")
    .get("/?customer_type=" + Random.shuffle(customer_types.toList).head.toString))
    .exec { session =>
      println(session)
      session
    }

  val home_page_scenario = scenario("PDP").exec(homepage)

  setUp(
    home_page_scenario.inject(
      rampUsers(10) during (5 seconds),
    ).protocols(httpProtocol),
  )
}

After running the test with one user, I get the following session values with customer_type equals either P or B

Session(PDP,10,1565009885331,Map(gatling.http.cache.baseUrl -> https://test.test.com, gatling.http.cache.dns -> io.gatling.http.cache.DnsCacheSupport$$anon$1@13cea563, gatling.http.cache.contentCache -> io.gatling.core.util.cache.Cache@1acc69e3, gatling.http.ssl.sslContexts -> SslContexts(io.netty.handler.ssl.OpenSslClientContext@534581dd,None), gatling.http.referer -> https://test.test.com/?customer_type=B, gatling.http.cookies -> CookieJar(Map(CookieKey(customer_type,www.test.com,/) -> StoredCookie(customer_type=B, path=/, secure,true,false,1565009892233), CookieKey(test_session,test.com328110,/) -> StoredCookie(test_session=OS96ekJ4Nk0zRkJBak5obDdES0RZaW1Qb1hHS1U1VG5YcndGbmxKT1hrV3p4WVpCZElSUXJISVlZRlZtNjRmazd4QVlYTHhlWHFyUjJIU2VLZUh1Q083NjFmVlFVdzNLMmVwckh5Z0JuOWJLaW1ab2FIbU13Qnl0UVdZblFSOHlrVXJWYUZTQ3dYL1ZOV1dZM2Z0MWxGK1piN1lpbGdTRUdZeXBGQXllaHBPcW83eW0zTStuc1huelJOZzRPNkFBN2RKN281Y2FvSUU0V01BTVk5RmtWQT09LS1nbEMxV1FId3MvT0ZaVFBFV2YwVGZnPT0%3D--5a89e73be05416d96f3acf2e3f927495caf3d491, domain=.test,cin, path=/, secure, HTTPOnly,false,false,1565009892233), CookieKey(user_group,www.test.com,/) -> StoredCookie(user_group=n, path=/,true,false,1565009892233)))),0,OK,List(),io.gatling.core.protocol.ProtocolComponentsRegistry$$Lambda$531/0x0000000840511040@6360231f)

After running it with the 10 users however, i get all the sessions with either P or B for customer type but it's the same for all users.


Solution

  • The gatling DSL specifices builders that are built ONCE at the start of a run - these are then used to create the users that execute scenarios.

    Because of this, your Random.shuffle(customer_types.toList).head.toString is only executed once and all the users pick up this value.

    To have a random customer_type selected by each user, you can create a custom feeder.

    private val customerTypes = Iterator.continually(
      Map("customerType" -> Random.shuffle(List("P","B")).head)
    )
    
    ...
    
    val homepage = exec(http("homepage")
      .get("/?customer_type=${customerType}")
      .exec { session =>
        println(session)
        session
      }
    )
    
    val home_page_scenario = scenario("PDP").feed(customerTypes).exec(homepage)
    

    so now each user will get the next value from the customerTypes feeder (which has more or less the same randomising function as your original example)