会话的值在会话之间复制
Value for a session is replicated between sessions
我有一个负载测试,会话值是根据我请求的 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),
)
}
在 运行 与一个用户的测试之后,我得到以下会话值
customer_type 等于 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@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$$Lambda1/0x0000000840511040@6360231f)
然而,在 运行 与 10 个用户进行之后,我获得了针对客户类型的 P 或 B 的所有会话,但它对所有用户都是相同的。
gatling DSL 指定在 运行 开始时构建一次的构建器 - 这些构建器随后用于创建执行场景的用户。
因此,您的 Random.shuffle(customer_types.toList).head.toString
仅执行一次,所有用户都将获取此值。
要让每位用户随机选择 customer_type,您可以创建自定义供稿器。
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 馈送器中获得下一个值(它或多或少具有与原始示例相同的随机化功能)
正如 所提到的,您的代码中 Random.shuffle
的执行只发生 运行 一次,因为正在设置测试。其实这是加特林新用户的一个普遍误区。
对于动态行为,Expression Language (EL) of Gatling 对于大多数情况应该足够了。
如果你只是想要一个随机元素,你可以简单地使用.random()
EL函数。打乱整个列表的代价太大了。
"${foo.random()}" // returns a random element of `foo` if `foo` points to an indexed collection
但是要使用EL,必须将变量放入会话属性中。这可以通过使用 exec block.
来完成
.exec { session => session.set("customerTypes", customerTypes)}
.exec(http("homepage")
.get("/")
.queryParam("customer_type", "${customerTypes.random()}"))
我有一个负载测试,会话值是根据我请求的 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),
)
}
在 运行 与一个用户的测试之后,我得到以下会话值 customer_type 等于 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@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$$Lambda1/0x0000000840511040@6360231f)
然而,在 运行 与 10 个用户进行之后,我获得了针对客户类型的 P 或 B 的所有会话,但它对所有用户都是相同的。
gatling DSL 指定在 运行 开始时构建一次的构建器 - 这些构建器随后用于创建执行场景的用户。
因此,您的 Random.shuffle(customer_types.toList).head.toString
仅执行一次,所有用户都将获取此值。
要让每位用户随机选择 customer_type,您可以创建自定义供稿器。
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 馈送器中获得下一个值(它或多或少具有与原始示例相同的随机化功能)
正如 Random.shuffle
的执行只发生 运行 一次,因为正在设置测试。其实这是加特林新用户的一个普遍误区。
对于动态行为,Expression Language (EL) of Gatling 对于大多数情况应该足够了。
如果你只是想要一个随机元素,你可以简单地使用.random()
EL函数。打乱整个列表的代价太大了。
"${foo.random()}" // returns a random element of `foo` if `foo` points to an indexed collection
但是要使用EL,必须将变量放入会话属性中。这可以通过使用 exec block.
来完成.exec { session => session.set("customerTypes", customerTypes)}
.exec(http("homepage")
.get("/")
.queryParam("customer_type", "${customerTypes.random()}"))