【问题标题】:Why I am getting Value is Null while executing two scenarios in Gatling?为什么我在 Gatling 中执行两个场景时得到 Value is Null?
【发布时间】:2017-05-17 22:28:53
【问题描述】:

我的脚本中有两个场景第一个“getAssets”场景将获取所有资产ID并将其保存在一个列表中,第2个场景“fetchMetadata”将迭代这些ID。

我只需要执行一次“getAssets”场景来获取所有 ID,然后“fetchMetadata”场景将执行到给定的持续时间。

这是“/api/assets;limit=$limit”请求的Json响应(我们使用$.assets[*].id从这里获取id),

{
  "assets": [
    {
      "id": 3010411,
      "name": "Asset 2016-11-22 20:06:07",
      ....
      ....
    },
    {
      "id": 3010231,
      "name": "Asset 2016-11-22 20:07:07",
      ....
      ....
    }, and so on..

这里是代码

import java.util.concurrent.ThreadLocalRandom
import scala.concurrent.duration._
import io.gatling.core.Predef._
import io.gatling.http.Predef._

class getAssetsMetadata extends Simulation {
    val getAssetURL = System.getProperty("getAssetURL", "https://performancetesting.net")
    val username = System.getProperty("username", "performanceuser")
    val password = System.getProperty("password", "performanceuser")
    val limit = Integer.getInteger("limit", 1000).toInt
    val userCount = Integer.getInteger("userCount", 100).toInt
    val duration = Integer.getInteger("duration",1).toInt //in minutes

    var IdList: Seq[String] = _   

    val httpProtocol = http
        .basicAuth(username, password)
        .baseURL(getAssetURL)
        .contentTypeHeader("""application/vnd.v1+json""")

    // Scenario 1 get assets
    val getAssets = scenario("Get Assets")
            .exec(http("List of Assets")
            .get(s"""/api/assets;limit=$limit""")
            .check(jsonPath("$.assets[*].id").findAll.transform {v => IdList = v; v }.saveAs("IdList"))
            )

    // Scenario 2 Fetch Metadata
    val fetchMetadata = scenario("Fetch Metadata")
        .exec(_.set("IdList", IdList))
        .exec(http("Metadata Request")
            .get("""/api/assets/${IdList.random()}/metadata""")
            )

    val scn = List(getAssets.inject(atOnceUsers(1)), fetchMetadata.inject(constantUsersPerSec(userCount) during(duration minutes)))
    setUp(scn).protocols(httpProtocol) 
}   

:::ERROR:::

它会抛出“Value is null”(虽然我们这里有 1000 万个资产 ID)。这是加特林日志

14883 [GatlingSystem-akka.actor.default-dispatcher-4] INFO  io.gatling.http.config.HttpProtocol - Warm up done
14907 [GatlingSystem-akka.actor.default-dispatcher-4] INFO  io.gatling.http.config.HttpProtocol - Start warm up
14909 [GatlingSystem-akka.actor.default-dispatcher-4] INFO  io.gatling.http.config.HttpProtocol - Warm up done
14911 [GatlingSystem-akka.actor.default-dispatcher-4] INFO  i.g.core.controller.Controller - Total number of users : 6001
14918 [GatlingSystem-akka.actor.default-dispatcher-6] INFO  i.g.c.r.writer.ConsoleDataWriter - Initializing
14918 [GatlingSystem-akka.actor.default-dispatcher-3] INFO  i.g.c.result.writer.FileDataWriter - Initializing
14923 [GatlingSystem-akka.actor.default-dispatcher-6] INFO  i.g.c.r.writer.ConsoleDataWriter - Initialized
14928 [GatlingSystem-akka.actor.default-dispatcher-3] INFO  i.g.c.result.writer.FileDataWriter - Initialized
14931 [GatlingSystem-akka.actor.default-dispatcher-4] DEBUG i.g.core.controller.Controller - Launching All Scenarios
14947 [GatlingSystem-akka.actor.default-dispatcher-12] ERROR i.g.http.action.HttpRequestAction - 'httpRequest-2' failed to execute: Value is null
14954 [GatlingSystem-akka.actor.default-dispatcher-4] DEBUG i.g.core.controller.Controller - Finished Launching scenarios executions
14961 [GatlingSystem-akka.actor.default-dispatcher-4] DEBUG i.g.core.controller.Controller - Setting up max duration
14962 [GatlingSystem-akka.actor.default-dispatcher-4] INFO  i.g.core.controller.Controller - Start user #7187317726850756780-0
14963 [GatlingSystem-akka.actor.default-dispatcher-4] INFO  i.g.core.controller.Controller - Start user #7187317726850756780-1
14967 [GatlingSystem-akka.actor.default-dispatcher-4] INFO  i.g.core.controller.Controller - End user #7187317726850756780-1
14970 [GatlingSystem-akka.actor.default-dispatcher-4] INFO  i.g.core.controller.Controller - Start user #7187317726850756780-2
14970 [GatlingSystem-akka.actor.default-dispatcher-5] INFO  io.gatling.http.ahc.HttpEngine - Sending request=List of Assets uri=https://performancetesting.net/api/assets;limit=1000: scenario=Get Assets, userId=7187317726850756780-0
14970 [GatlingSystem-akka.actor.default-dispatcher-7] ERROR i.g.http.action.HttpRequestAction - 'httpRequest-2' failed to execute: Value is null
14972 [GatlingSystem-akka.actor.default-dispatcher-7] INFO  i.g.core.controller.Controller - End user #7187317726850756780-2
14980 [GatlingSystem-akka.actor.default-dispatcher-7] ERROR i.g.http.action.HttpRequestAction - 'httpRequest-2' failed to execute: Value is null
14980 [GatlingSystem-akka.actor.default-dispatcher-4] INFO  i.g.core.controller.Controller - Start user #7187317726850756780-3
14984 [GatlingSystem-akka.actor.default-dispatcher-4] INFO  i.g.core.controller.Controller - End user #7187317726850756780-3
.....
.....
.....
61211 [GatlingSystem-akka.actor.default-dispatcher-12] INFO  i.g.core.controller.Controller - Start user #7187317726850756780-4626
61211 [GatlingSystem-akka.actor.default-dispatcher-7] ERROR i.g.http.action.HttpRequestAction - 'httpRequest-2' failed to execute: Value is null
61211 [GatlingSystem-akka.actor.default-dispatcher-7] INFO  i.g.core.controller.Controller - End user #7187317726850756780-4626
61224 [GatlingSystem-akka.actor.default-dispatcher-2] INFO  i.g.core.controller.Controller - Start user #7187317726850756780-4627
61225 [GatlingSystem-akka.actor.default-dispatcher-5] INFO  io.gatling.http.ahc.HttpEngine - Sending request=Metadata Request uri=https://performancetesting.net/api/assets/3010320/metadata: scenario=Fetch Metadata, userId=7187317726850756780-4627
61230 [GatlingSystem-akka.actor.default-dispatcher-12] INFO  i.g.core.controller.Controller - Start user #7187317726850756780-4628
61230 [GatlingSystem-akka.actor.default-dispatcher-7] INFO  io.gatling.http.ahc.HttpEngine - Sending request=Metadata Request uri=https://performancetesting.net/api/assets/3009939/metadata: scenario=Fetch Metadata, userId=7187317726850756780-4628
61233 [GatlingSystem-akka.actor.default-dispatcher-2] INFO  i.g.core.controller.Controller - End user #7187317726850756780-0
61233 [New I/O worker #12] DEBUG c.n.h.c.p.netty.handler.Processor - Channel Closed: [id: 0x8c94a1ae, /192.168.100.108:56739 :> performancetesting.net/10.20.14.176:443] with attribute INSTANCE

---- Requests ------------------------------------------------------------------
> Global                                                   (OK=261    KO=40    )
> Metadata Request                                         (OK=260    KO=40    )
> List of Assets                                           (OK=1      KO=0     )
---- Errors --------------------------------------------------------------------
> Value is null                                                      40 (100.0%)
================================================================================

谢谢。

【问题讨论】:

    标签: java scala gatling scala-gatling


    【解决方案1】:

    ID列表并没有传递给其他场景,因为它是由另一个“用户”(会话)获取的。

    您的模拟如下所示

    • 1 个用户正在获取 ID 列表(并为自己保留该列表)
    • 同时,n 个用户尝试使用(未定义的)ID 列表获取资产

    第一个用户不与其他用户交谈,两个场景注入相互独立。

    解决此问题的一种方法是将列表存储在共享容器(即 AtomicReference)中,并从第二种情况访问此容器。 为确保容器已填充,请在第二个场景开始时注入 nothingFor 步骤,以等待第一个场景完成。

    另一种方法是在第二个场景开始时获取 ID 列表 - 如果之前没有获取过(参见上面的容器)。

    我相信也有办法实现这一点(即在实际测试之前使用一些馈线并获取 id 列表)

    【讨论】:

    • 我在第二种情况下尝试了 .pause() 并且它有效。直到暂停时间第一个场景获取所有 ID,然后第二个场景开始。但是,如果我希望我的第二个请求在第一个完成之前不会开始,我该如何实施?如果可能,请举例说明。谢谢杰拉德。
    • 当您使用“constantUsersPerSec”进行模拟时,您可以使用doIfstatement 并检查第一个场景设置的条件。第一个到达的用户可能什么都不做,但之后的其他人都应该运行他们的序列。
    • val scn1 = scenario("scenario1") .exec(getAssets) val scn2 = scenario("scenario2") .exec(fetchMetadata) setUp(scn1.inject(atOnceUsers(1)), scn2. inject(constantUsersPerSec(userCount) during(duration minutes))).protocols(httpProtocol) 我试过了,但没有用。我想在链中执行这个场景。你能帮帮我吗?
    猜你喜欢
    • 1970-01-01
    • 2011-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-13
    • 1970-01-01
    相关资源
    最近更新 更多