加特林的步骤是否被缓存并且仅执行一次?

问题描述

我有一个模拟步骤,可以将其发布到不同的端点。

class MySimulation extends Simulation {
  
  // some init code 

  var testTitle = this.getClass.getSimpleName
  
  val myscenario = scenario("Scn Description")
    .exec(PublishMessageRandom(pConfigTest,testTitle + "-" + numProducers,numProducers))   
  
  if (testMode == "debug") {
    setUp(
      myscenario.inject(
        atOnceUsers(1)
      )
    ).protocols(httpConf)    
  } else if (testMode == "open") {
    setUp(
      myscenario.inject(       
        rampConcurrentUsers(concurrentUserMin) to (concurrentUserMax) during (durationInMinutes minutes),)
    ).protocols(httpConf)
  }
}

在这是我的PublishMessageRandom定义

def PublishMessageRandom(producerConfig : ProducerConfig,testTitle : String,numberOfProducers : Int ) = { 
      
      val jsonBody = producerConfig.asJson
      val valuedJsonBody = Printer.noSpaces.copy(dropNullValues = true).print(jsonBody)
      println(valuedJsonBody)
        
      val nodes : Array[String]  = endpoints.split(endpointDelimiter)
      val rnd = scala.util.Random
      val rndindex = rnd.nextInt(numberOfProducers)      
      var endpoint = "http://" + nodes(rndindex) + perfEndpoint      
      println("endpoint:" + endpoint)

      exec(http(testTitle)    
      .post(endpoint)    
      .header(HttpHeaderNames.ContentType,HttpHeaderValues.Applicationjson)
      .body(StringBody(valuedJsonBody))
      .check(status.is(200))
      .check(bodyString.saveAs("serverResponse"))
    )
    // the below is only useful in debug mode. Comment it out for longer tests
    /*.exec { session =>
      println("server_response: " + session("serverResponse").as[String])
      println("endpoint:" + endpoint)
      session */
    }
  }
如您所见,

只是端点的循环。不幸的是,我一次看到了上面的println("endpoint:" + endpoint),看起来它随机地选择了一个端点并一直击中该端点,而不是随机击中端点的预期目的。

有人可以解释这种行为吗?加特林是在缓存该步骤还是该如何解决

解决方法

引用官方documentation

警告

加特林DSL组件是不可变的ActionBuilder,必须 完全链接在一起,并且仅在启动时构建一次。结果是 工作流的一系列动作。这些建设者什么都不做 本身,它们不会触发任何副作用,它们只是 定义。结果,在运行时创建此类DSL组件 功能是完全没有意义的。

,

我不得不使用馈线解决馈线取随机端点的问题。

  // feeder is random endpoint as per number of producers
  val endpointFeeder = GetEndpoints(numProducers).random
  
  val myscenario = scenario("Vary number of producers hitting Kafka cluster")
    .feed(endpointFeeder)
    .exec(PublishMessageRandom(pConfigTest,testTitle + "-" + numProducers))  

和“随机发布消息”如下所示:

def PublishMessageRandom(producerConfig : ProducerConfig,testTitle : String ) = { 
      
      val jsonBody = producerConfig.asJson
      val valuedJsonBody = Printer.noSpaces.copy(dropNullValues = true).print(jsonBody)
      println(valuedJsonBody)        
     
      exec(http(testTitle)    
      .post("${endpoint}")
      .header(HttpHeaderNames.ContentType,HttpHeaderValues.ApplicationJson)
      .body(StringBody(valuedJsonBody))
      .check(status.is(200))
      .check(bodyString.saveAs("serverResponse"))
    )

  }

您看到 .post("${endpoint}")上方的那行最终会碰到来自馈线的端点。

馈线函数GetEndpoints定义如下 我们在其中创建一个映射数组,每个“端点”都是一个值,这是关键。

 def GetEndpoints(numberOfProducers : Int ) : Array[Map[String,String]] = {  
      val nodes : Array[String]  = endpoints.split(endpointDelimiter)        
      var result : Array[Map[String,String]] = Array()
      for( elt <- 1 to numberOfProducers ) {   
        var endpoint = "http://" + nodes(elt-1) + perfEndpoint      
        var m : Map[String,String] = Map()
        m += ("endpoint" -> endpoint )
        result = result :+ m  
        println("map:" + m)    
      }
      result
  }