如何在PUT请求中设置计数器?

时间:2016-11-15 06:18:06

标签: scala gatling

我有两个URL,第一个是BaseURL创建用户,第二个用于为asLongAs循环中的每个用户设置密码。 我在第二个url请求中设置“counter”是“Change_Password”时遇到问题。它会抛出错误,

  

9425 [GatlingSystem-akka.actor.default-dispatcher-11]错误   i.g.http.action.HttpRequestAction - 'httpRequest-4'无法执行:   没有定义名为'counter'的属性

我无法在这里设置计数器,

.exec(http("Change_Password")
            .put(setPasswordURL + "/api/authentication/accounts/" + accountName + "/users/" + unm + "${counter}/actions/setPassword")

如果我在.put请求中放置静态值,它可以正常工作。

这是代码,

import scala.concurrent.duration._
import io.gatling.core.Predef._
import io.gatling.http.Predef._
import io.gatling.jdbc.Predef._

class setPass extends Simulation {
    val testServerUrl = System.getProperty("testServerUrl", "https://abcde.net")
    val username = System.getProperty("username", "ma")
    val password = System.getProperty("password", "ma")

    val accntID = Integer.getInteger("accountID", 27280asd5).toInt
    val rolID = Integer.getInteger("roleId", 272812506).toInt
    val startNum = Integer.getInteger("startNum", 2).toInt
    val endNum = Integer.getInteger("EndNum", 2).toInt
    val userCount = Integer.getInteger("userCount", 1).toInt
    val unm = System.getProperty("CreateUserName", "TestUser")
    val FirstName = System.getProperty("FirstName", "Test")
    val LastName = System.getProperty("LastName", "Yo")
    val emailDomain = System.getProperty("EmailDomain", "@testdomain.com")
    val accountName = System.getProperty("AccountName", "info")

    val counter = new java.util.concurrent.atomic.AtomicInteger(startNum)

    val setPasswordURL = "http://XYZ-differentURL.net:18101"

    val httpProtocol = http
        .baseURL(testServerUrl)
        .basicAuth(username, password)
        .inferHtmlResources()
        .acceptHeader("""*/*""")
        .acceptEncodingHeader("""gzip, deflate""")
        .acceptLanguageHeader("""en-US,en;q=0.8""")
        .contentTypeHeader( """application/json""")
        .userAgentHeader("""Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.94 Safari/537.36""")

    val headers_0 = Map(
        """Cache-Control""" -> """no-cache""",
        """Origin""" -> """chrome-extension://fhbjgbifasdsgdshghfdhdcbncdddomop""")

    val headers_1 = Map(
        """Accept""" -> """application/json""",
        """Content-Type""" -> """application/json""")   

    val scn = scenario("sc1")
        .asLongAs(session => counter.getAndIncrement() < endNum)
        {
        exec(http("req1")
            .post("""/api/user""")
            .headers(headers_0)
            .body(StringBody(session =>s"""{"name": "$unm${counter.get()}" ,"roleId": $rolID, "accountId": $accntID, "firstName": "$FirstName${counter.get()}", "lastName": "$LastName${counter.get()}", "email": "$unm${counter.get()}$emailDomain"}"""))
            .check(jsonPath("$.id").saveAs("UserID"))
            )

        .exec(http("Change_Password")
            .put(setPasswordURL + "/api/authentication/accounts/" + accountName + "/users/" + unm + "${counter}/actions/setPassword")
            .headers(headers_1)
            .body(StringBody("""{"newPassword":"password"}"""))
            )   
        }

    .exec(session => session.set("count", counter.getAndIncrement))
    setUp(scn.inject(atOnceUsers(userCount))).protocols(httpProtocol)
    }

1 个答案:

答案 0 :(得分:0)

您可以以不雅的方式使用Gatling请求ID。

.exec{session => session.set("number",session.userId.split("-").last.toInt)}
.exec{session => session("number").as[Int] }

我用它来每10个请求和每100个请求调用一个请求。

.exec{session => session.set("number",session.userId.split("-").last.toInt)}
.exec(request1)
.doIf(session => session("number").as[Int] % 10 == 0) {
   exec(request2)
   .doIf(session => session("number").as[Int] % 100 == 0) {
      exec(pause(10 seconds))
      .exec(request3)
      .exec(pause(4 seconds))
      .exec(request4)
   }
}