1

预期输出应该是 3 次类似的调用,

  1. https://someurl.net;limit=5;offset=0 (第一次调用将每次从 0 偏移量开始 - 必须)
  2. https://someurl.net;limit=5;offset=5(第二次调用,limit + offsetvalue 0 + 5 = 5)
  3. https://someurl.net;limit=5;offset=10(第三次调用,limit + offsetvalue 5 + 5 = 10)

这是代码,

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

class metadataAn extends Simulation {
    val getAssetURL = System.getProperty("getAssetURL", "https://someurl")
    val username = System.getProperty("username", "user")
    val password = System.getProperty("password", "user")

    val limit = Integer.getInteger("limit", 5).toInt
    val counter = 0

    val httpProtocol = http
        .basicAuth(username, password)
        .baseURL(getAssetURL)

    // Step-1 get total count
    val scn = scenario("Get Total assets")
            .exec(http("Number of Assets")
            .get(s"""/api/xyz;limit=1;offset=0""")
            .check(jsonPath("$.totalCount").findAll.saveAs("total"))
            )

    //.asLongAs(session => counter.getAndIncrement().equals("${total}/$limit")) // Throws error mentioned below
    .asLongAs(session => session.get("${counter}").equals("10"))
    {
        exec(http("List of Assets")
            .get(session =>s"""/api/xyz;limit=$limit;offset=${counter}""")
            .check(jsonPath("$.assets[*].id").findAll.saveAs("IdList"))
            )

        .foreach("${IdList}", "idlist") {
            exec(http("Add data")
                .post("""/api/xyz/${idlist}/data""")
                .body(StringBody(session =>s"""{some data....}"""))
                )
            }
        .exec(session => {
                val cnt = session("counter").as[String].toInt
                val increaseCounter = cnt + limit
                session.set("counter", increaseCounter)
                println("********COUNTER************: ====>>> " + increaseCounter)
                session})

    }
    setUp(scn.inject(atOnceUsers(1))).protocols(httpProtocol)
}       

输出:

它编译但无法进入循环。

---- Get Total assets ----------------------------------------------------------
[##########################################################################]100%
          waiting: 0      / active: 0      / done:1
---- Requests ------------------------------------------------------------------
> Global                                                   (OK=1      KO=0     )
> Number of Assets                                         (OK=1      KO=0     )
================================================================================

===============================

如果我用以下条件编译它

.asLongAs(session => counter.getAndIncrement().equals("${total}/$limit"))

引发错误:

17:34:10.300 [ERROR] i.g.c.ZincCompiler$ - scala:34: value getAndIncrement is not a member of Int
17:34:10.301 [ERROR] i.g.c.ZincCompiler$ -      .asLongAs(session => counter.getAndIncrement().equals("${total}/$limit"))
17:34:10.486 [ERROR] i.g.c.ZincCompiler$ - one error found
17:34:10.487 [DEBUG] i.g.c.ZincCompiler$ - Compilation failed (CompilerInterface)

谢谢你。

4

1 回答 1

0

问题是在顶部你定义了一个变量“计数器”,在这里

.asLongAs(session => session.get("${counter}").equals("10"))

您使用会话中的变量。我之前看不到您将变量添加到会话的位置。这意味着这在访问或 null 时会给出异常。一种解决方案是

.asLongAs(session => session.("$counter").asOption[String].equals("10"))

你可以加一个!在反转结果之前执行循环,直到计数器为 10。

PS:我确定它与您不再相关,但问题仍然出现在谷歌上,因此它可能对某人有所帮助。

于 2021-06-02T12:50:52.057 回答