Gatling (performance test):how to perform task in background every x-minutes

本小妞迷上赌 提交于 2019-12-11 18:10:00

问题


I would like to know how to perform a task in the background every x-minutes with Gatling.

Background: I want to write a couple of scenario's on our backend. Our backend will be called by another backend in this case. A usual scenario:

  • client (backend) to server authentication (optional)
  • client requests X
  • client requests Y
  • client requests Z

I want a scenario where requests 'X, Y and Z' are performed.

The problem: The backend(client) to backend (server) authentication is performed with accessTokens. This accesToken will expire a couple of times within a total simulation, however I dont want to do this authentication every scenario. (since that might than be the bottleneck). For example : the token expires every 10 minutes, a scenario takes 5 seconds, and the total simulation will be 2 hours.

Question: How can I create a simulation that will refresh the accesstoken on the background every 10 minutes. In a real-life scenario the backend(client) will just have a background process that updates the accesToken (in memory, or shared, state) every 10 mins. Once again: I don't want to re-authenticate every scenario (call X, call Y, call Z).


回答1:


if it suits to consider the looping a part of the scenario, then you should be able to achieve what you want by using a duration / deadline and conditional execution.

eg

import scala.concurrent.duration._

val executionTime = 2 hours
val authTimeout = 10 minutes 
val safetyMargin = 30 seconds

val authenticate : ChainBuilder = exec(login)
  .exec(session => session.set("timeout", authTimeout.fromNow))

scn = scenario("scenarioXYZ")
  .exec(authenticate)      
  .during(executionTime) {
    doIf(session => {session.get("timeout").as[Deadline].timeleft <= safteyMargin}) {
      exec(authenticate)
    }
    .exec(requestX)
    .exec(requestY)
    .exec(requestZ)
  }
}

so login (setting your token) and set a Deadline for when the token will expire

then loop for as long as you need to, and on each loop if the authentication has less that some specified amount remaining then authenticate again and set a new expected expiry



来源:https://stackoverflow.com/questions/48975160/gatling-performance-testhow-to-perform-task-in-background-every-x-minutes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!