Scala 会话的值在会话之间复制

Scala 会话的值在会话之间复制,scala,gatling,Scala,Gatling,我有一个负载测试,其中会话值是根据我请求的URL设置的,可以是我随机选择的两个选项之一 当我为一个用户执行测试时,会话中的值成功设置为随机值 当我添加更多用户时,该值将随机设置,但对于所有用户的所有会话也是相同的(因此所有用户都具有相同的会话值) 我的负载测试是这样的 class test extends Simulation { val customer_types = Array("P", "B") val httpProtocol = http .baseUrl("htt

我有一个负载测试,其中会话值是根据我请求的URL设置的,可以是我随机选择的两个选项之一

当我为一个用户执行测试时,会话中的值成功设置为随机值

当我添加更多用户时,该值将随机设置,但对于所有用户的所有会话也是相同的(因此所有用户都具有相同的会话值)

我的负载测试是这样的

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),
  )
}
在使用一个用户运行测试之后,我得到以下会话值 客户_类型等于P或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)

然而,在使用10个用户运行它之后,我获得了所有客户类型的p或B会话,但所有用户的会话都是相同的。

gatling DSL指定了在运行开始时构建一次的构建器-然后使用这些构建器创建执行场景的用户

因此,您的
Random.shuffle(customer\u types.toList).head.toString
只执行一次,所有用户都会获取该值

要让每个用户选择一个随机的客户类型,您可以创建一个自定义进料器

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)
因此,现在每个用户都将从customerTypes feeder(它与原始示例或多或少具有相同的随机化功能)获得下一个值。

如前所述,在设置测试时,代码中执行
Random.shuffle
只运行一次。事实上,这是新用户对加特林的普遍误解

对于动态行为,在大多数情况下,该参数应足够

如果只需要一个随机元素,只需使用
.random()
EL函数即可。洗牌整个名单的代价太高了

“${foo.random()}”//如果`foo`指向索引集合,则返回`foo`的随机元素

但是要使用EL,必须将变量放入会话属性中。这可以通过使用



谢谢你,这对我想做的事情非常有效
.exec { session => session.set("customerTypes", customerTypes)}
.exec(http("homepage")
  .get("/")
  .queryParam("customer_type", "${customerTypes.random()}"))