本文介绍了加特林(性能测试):如何每隔X分钟在后台执行任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何使用Gatling每隔X分钟在后台执行一项任务.

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:

  • 客户端(后端)到服务器的身份验证(可选)
  • 客户请求X
  • 客户请求Y
  • 客户请求Z

我想要执行请求'X,Y和Z'的情况.

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

问题:后端(客户端)到后端(服务器)的身份验证是使用accessTokens执行的.整个模拟过程中,此accesToken将失效两次,但是我不想在每种情况下都进行此身份验证. (因为这可能是瓶颈).例如:令牌每10分钟失效一次,一个场景需要5秒钟,而整个模拟过程将是2个小时.

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.

问题:如何创建一个仿真,每10分钟刷新一次后台的访问令牌.在实际情况中,后端(客户端)将仅具有一个后台进程,该进程每10分钟更新一次accesToken(在内存或共享状态下).再说一次:我不想对每种情况都进行身份验证(呼叫X,呼叫Y,呼叫Z).

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

推荐答案

如果适合考虑循环场景的一部分,那么您应该能够通过使用持续时间/截止日期和条件执行来实现所需的目标.

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.

例如

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

这篇关于加特林(性能测试):如何每隔X分钟在后台执行任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-15 20:46