如何在 Gatling 脚本中添加条件计数 > 8?
How to add a condition count > 8 in Gatling script?
我想在 http 请求中添加以下条件,如果该条件匹配,它会给我 KO 状态。
我的条件是WorkflowFailed = True OR count > 8
然后状态会失败。
对于上述情况之一 WorkflowFailed = True
我添加了下面的代码并且它工作正常,但是对于 count > 8
它不起作用。
.check(jsonPath("$.failed").transform(status => status == "true").is(false))
我也尝试过使用此代码,但没有成功并引发错误。
.check(jsonPath("$.failed").transform(status => status == "true" || count > 8).is(false))
19:00:52.143 [ERROR] i.g.c.ZincCompiler$ -
D:...\gatling-charts-highcharts-bundle-2.1.7\user-file
s\simulations\LaunchResources.scala:83: value > is not a member of
java.util.concurrent.atomic.AtomicInteger 19:00:52.144 [ERROR]
i.g.c.ZincCompiler$ -
.check(jsonPath("$.failed"). transform(status => status == "true" ||
count > 8).is(false)) 19:00:52.144 [ERROR] i.g.c.ZincCompiler$ -
^ 19:00:52.222 [ERROR] i.g.c.ZincCompiler$ - one error found
这是代码,
class LaunchResources extends Simulation {
val scenarioRepeatCount = Integer.getInteger("scenarioRepeatCount", 1).toInt
val userCount = Integer.getInteger("userCount", 1).toInt
val UUID = System.getProperty("UUID", "24d0e03")
val username = System.getProperty("username", "p1")
val password = System.getProperty("password", "P12")
val testServerUrl = System.getProperty("testServerUrl", "https://someurl.net")
val count = new java.util.concurrent.atomic.AtomicInteger(0)
val httpProtocol = http
.baseURL(testServerUrl)
.basicAuth(username, password)
.connection("""keep-alive""")
.contentTypeHeader("""application/vnd+json""")
val headers_0 = Map(
"""Cache-Control""" -> """no-cache""",
"""Origin""" -> """chrome-extension://fdmmgasdw1dojojpjoooidkmcomcm""")
val scn = scenario("LaunchAction")
.repeat (scenarioRepeatCount) {
exec(http("LaunchAResources")
.post( """/api/actions""")
.headers(headers_0)
.body(StringBody(s"""{"UUID": "$UUID", "stringVariables" : {"externalFilePath" : "/Test.mp4"}}"""))
.check(jsonPath("$.id").saveAs("WorkflowID")))
.exec(http("SaveWorkflowStatus")
.get("""/api/actions/{$WorkflowID}""")
.headers(headers_0)
.check(jsonPath("$.status").saveAs("WorkflowStatus")))
}
.asLongAs(session => session.attributes("WorkflowStatus") != "false" && count.getAndIncrement() < 8) {
doIf(session => session("WorkflowFailed").validate[String].map(WorkflowFailed => !WorkflowFailed.contains("true")).recover(true))
{
pause(pauseTime)
.exec(http("SaveWorkflowStatus")
.get("""/api/actions/${WorkflowID}""")
.headers(headers_0)
.check(jsonPath("$.running").saveAs("WorkflowStatus"))
.check(jsonPath("$.failed").saveAs("WorkflowFailed"))
.check(jsonPath("$.failed").transform(status => status == "true").is(false)) // Added this line to fail 1st condition which is (WorkflowFailed = True) then mark as KO. Works fine.
)
.exec(session => {
val wflowStatus1 = session.get("WorkflowStatus").asOption[String]
val wflowFailed1 = session.get("WorkflowFailed").asOption[String]
println("Inner Loop Workflow Status: ========>>>>>>>> " + wflowStatus1.getOrElse("COULD NOT FIND STATUS"))
println("Inner Loop Workflow Failed?? ========>>>>>>>> " + wflowFailed1.getOrElse("COULD NOT FIND STATUS"))
println("Count =====>> " + count)
session})
}
}
setUp(scn.inject(atOnceUsers(userCount))).protocols(httpProtocol)
}
这个错误很容易解释?
value > is not a member of java.util.concurrent.atomic.AtomicInteger
并且您的代码中已经有一个修复程序;)所以我想您应该使用
count.get > 8
而不是count > 8
我想在 http 请求中添加以下条件,如果该条件匹配,它会给我 KO 状态。
我的条件是WorkflowFailed = True OR count > 8
然后状态会失败。
对于上述情况之一 WorkflowFailed = True
我添加了下面的代码并且它工作正常,但是对于 count > 8
它不起作用。
.check(jsonPath("$.failed").transform(status => status == "true").is(false))
我也尝试过使用此代码,但没有成功并引发错误。
.check(jsonPath("$.failed").transform(status => status == "true" || count > 8).is(false))
19:00:52.143 [ERROR] i.g.c.ZincCompiler$ - D:...\gatling-charts-highcharts-bundle-2.1.7\user-file s\simulations\LaunchResources.scala:83: value > is not a member of java.util.concurrent.atomic.AtomicInteger 19:00:52.144 [ERROR] i.g.c.ZincCompiler$ -
.check(jsonPath("$.failed"). transform(status => status == "true" || count > 8).is(false)) 19:00:52.144 [ERROR] i.g.c.ZincCompiler$ - ^ 19:00:52.222 [ERROR] i.g.c.ZincCompiler$ - one error found
这是代码,
class LaunchResources extends Simulation {
val scenarioRepeatCount = Integer.getInteger("scenarioRepeatCount", 1).toInt
val userCount = Integer.getInteger("userCount", 1).toInt
val UUID = System.getProperty("UUID", "24d0e03")
val username = System.getProperty("username", "p1")
val password = System.getProperty("password", "P12")
val testServerUrl = System.getProperty("testServerUrl", "https://someurl.net")
val count = new java.util.concurrent.atomic.AtomicInteger(0)
val httpProtocol = http
.baseURL(testServerUrl)
.basicAuth(username, password)
.connection("""keep-alive""")
.contentTypeHeader("""application/vnd+json""")
val headers_0 = Map(
"""Cache-Control""" -> """no-cache""",
"""Origin""" -> """chrome-extension://fdmmgasdw1dojojpjoooidkmcomcm""")
val scn = scenario("LaunchAction")
.repeat (scenarioRepeatCount) {
exec(http("LaunchAResources")
.post( """/api/actions""")
.headers(headers_0)
.body(StringBody(s"""{"UUID": "$UUID", "stringVariables" : {"externalFilePath" : "/Test.mp4"}}"""))
.check(jsonPath("$.id").saveAs("WorkflowID")))
.exec(http("SaveWorkflowStatus")
.get("""/api/actions/{$WorkflowID}""")
.headers(headers_0)
.check(jsonPath("$.status").saveAs("WorkflowStatus")))
}
.asLongAs(session => session.attributes("WorkflowStatus") != "false" && count.getAndIncrement() < 8) {
doIf(session => session("WorkflowFailed").validate[String].map(WorkflowFailed => !WorkflowFailed.contains("true")).recover(true))
{
pause(pauseTime)
.exec(http("SaveWorkflowStatus")
.get("""/api/actions/${WorkflowID}""")
.headers(headers_0)
.check(jsonPath("$.running").saveAs("WorkflowStatus"))
.check(jsonPath("$.failed").saveAs("WorkflowFailed"))
.check(jsonPath("$.failed").transform(status => status == "true").is(false)) // Added this line to fail 1st condition which is (WorkflowFailed = True) then mark as KO. Works fine.
)
.exec(session => {
val wflowStatus1 = session.get("WorkflowStatus").asOption[String]
val wflowFailed1 = session.get("WorkflowFailed").asOption[String]
println("Inner Loop Workflow Status: ========>>>>>>>> " + wflowStatus1.getOrElse("COULD NOT FIND STATUS"))
println("Inner Loop Workflow Failed?? ========>>>>>>>> " + wflowFailed1.getOrElse("COULD NOT FIND STATUS"))
println("Count =====>> " + count)
session})
}
}
setUp(scn.inject(atOnceUsers(userCount))).protocols(httpProtocol)
}
这个错误很容易解释?
value > is not a member of java.util.concurrent.atomic.AtomicInteger
并且您的代码中已经有一个修复程序;)所以我想您应该使用
count.get > 8
而不是count > 8