【问题标题】:Refresh Bearer Token during Gatling Load Tests在 Gatling 负载测试期间刷新承载令牌
【发布时间】:2022-02-02 01:02:43
【问题描述】:

如何在 3 小时测试期间每 15 分钟在 gatling(用 scala 编写)中刷新我的不记名 (aouth2) 令牌?

我能够获得我的令牌,但我无法在场景中每 15 分钟应用一次。

  def getXYZ() = {
    exec(
      http("Get all xyz")
        .get("/xyz/v1/abc")
    )
  }

  val authTimeout = 20.seconds
  val safetyMargin = 5.seconds
  val executionTime = 2.hours

  val tokenTimeout: ChainBuilder = exec(session => session.set("timeout", authTimeout.fromNow))
  val printSession: ChainBuilder = exec { session => println(session)
    session
  }

  def refreshAccessToken(): ChainBuilder = {
    exec(tokenTimeout)
    doIf(session => {
      session("timeout").as[Deadline].timeLeft <= safetyMargin
    }) {
      exec(
        http("Refresh Access Token")
          .post(url)
          .formParam("grant_type", "client_credentials")
          .formParam("scope", scope)
          .header("Content-Type", "application/x-www-form-urlencoded")
          .header("Authorization", s"Basic $base64EncodedCredentials")
          .check(jsonPath("$.access_token").find.saveAs("accessToken"))
      )
        .exec(printSession)
    }
  }

  val scn = scenario("Scenario: Load Simulation With rampingUp Users")
    .exec(session => {
      val mytoken = session("accessToken")  // -->Trying the capture the token here
      println(mytoken.as[String])
      session
    })
    .exec(getXYZ())
    .pause(5)

setUp(
    scn.inject( ..... etc ...)

【问题讨论】:

    标签: testing oauth-2.0 load bearer-token scala-gatling


    【解决方案1】:

    伪代码(Java 中,自 Gatling 3.7 起支持):

    // global mutable reference to a shared token
    private volatile String token = null;
    
    // first scenario refreshes the token
    // must run with one single user 
    ScenarioBuilder refresh = scenario("refresh")
    .during(Duration.ofHours(3)).on(
      exec(http("refreshToken").get(???).check(saveToken))
      .exec(session ->
        {
          token = session.getString("token")
          return token;
        }
      ).pause(Duration.ofMinutes(15))
    );
    
    
    // second scenario uses the token,
    // must start after the first one
    ScenarioBuilder use = scenario("use")
      .exec(http("useToken").get(???).header("Bearer", session -> token))
    
    

    【讨论】:

    • 首先,感谢您对我的问题的回答。不幸的是,我仍在为 scala 代码而苦苦挣扎。我正在使用 asynchttpclient 来获取我的令牌。但我无法在场景中注入它,因为我收到以下错误:“scala gatling ambiguous reference to overloaded definition, both method exec in trait Execs of type”你有什么解决办法吗?
    • 如果您认为自己在使用 Scala 时遇到困难,因此您不是 Scala 开发人员,为什么要使用 Scala? Gatling 支持 Java(或 Kotlin)。
    • 我继续一个现有的项目,我超越了它。这里的问题不是 scala 本身,而是在性能测试过程中使用自定义请求提要。 def getXYZ() = { exec( http("获取所有功能") .get("/xyz/v1/abc") .check(status.is(200)) ) } val scn = scenario("场景:负载模拟With rampingUp Users") .exec(getXYZ()) .exec(session => { val mytoken = session("accessToken") println(mytoken.as[String]) session }) .pause(5)
    猜你喜欢
    • 1970-01-01
    • 2018-11-14
    • 1970-01-01
    • 2012-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多