添加每个http请求增加的计数器

时间:2016-10-05 06:16:01

标签: java scala performance-testing gatling

我想在下面的场景中添加一个条件 我想退出方案if(counter=8 or WorkflowStatus=true)

有没有人知道如何添加一个计数器,该计数器在每次请求时增加8次并在8之后退出,如果请求获得WorkflowStatus = true则退出上述条件,然后退出以下情况?

如果您需要更多说明,请与我们联系。 感谢。

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 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")))

        }

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

2 个答案:

答案 0 :(得分:1)

我个人使用这个技巧在每次请求时都有一个计数器增量

val scn = scenario("Scenario Conversion")
.exec{session => session.set("number",session.userId.split("-").last.toInt)}

您可以在其他会话值

中重复使用此功能
val scn = scenario("Scenario Conversion")
.exec{session => session.set("number",session.userId.split("-").last.toInt)}
.exec{session => session.set("timestamp", nextDay(session("number").as[Int]/1000))}

答案 1 :(得分:0)

您可以使用Redis存储您的计数,每次请求时都会控制Redis编号。 我使用Redis在3分钟内计算我的http帖子计数,如果计数在3分钟内超过10次,我将禁用此帖子IP地址,并且此ip将在未来3分钟内获得403禁止错误。