3
votes

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

1 Answers

3
votes

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