预期输出应该是 3 次类似的调用,
- https://someurl.net;limit=5;offset=0 (第一次调用将每次从 0 偏移量开始 - 必须)
- https://someurl.net;limit=5;offset=5(第二次调用,limit + offsetvalue 0 + 5 = 5)
- 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)
谢谢你。