在场景范围外加特林执行-不调用POST请求

时间:2020-08-12 23:58:10

标签: gatling scala-gatling

我正在尝试使用Gatling Simulation的 before after 块来编写gatling性能测试,以针对该服务发出一次HTTP发布请求。

 class MyTest extends Simulation {
      
      // Some code here
      // and definitions
      
      val myScenario = scenario("Vary number of ...")
        .exec(PublishMessageRoundRobin(pConfigTest, testTitle + "-" + numX, numY))
    
      // extract the nodes  
      val nodes : Array[String]  = endpoints.split(endpointDelimiter)  
      
      //
      // create consumers with desired configurations at endpoint prior to scenario run
      // then start them
      //
      before {
          var endpoint = ""
                
          //
          // TODO: based on run parameter, decide if we should pre-run producers
          // 
          for( elt <- 1 to numX ) {
            endpoint = "http://" + nodes(elt-1) + cEndpoint + setConfig      
            CallSet( myobj, endpoint )
            endpoint = "http://" + nodes(elt-1) + cEndpoint + start  
            CallStart( myobj, endpoint )        
          }
      }
      
      if (testMode == "debug") {
        setUp(
          myScenario.inject(
            atOnceUsers(1)
          )
        ).protocols(httpConf)    
      } else if (testMode == "open") {
        setUp(
          myScenario.inject(       
            rampConcurrentUsers(20) to (200) during (durationInMinutes minutes),
          )
        ).protocols(httpConf)
      }
       
       // stop all consumers
       after {
           var endpoint = ""
           for( elt <- 1 to numX ) {
               endpoint = "http://" + nodes(elt-1) + cEndpoint + stop  
               CallStop(myobj, endpoint)
           }
       }
    
    }

由于某些原因,CallStart和CallStop和CallSet没有发出POST请求。 唯一调用的POST请求是在场景PublishMessageRoundRobin中定义的POST请求,该请求调用exec并针对端点创建发布。

它们的定义非常相似,这里是其中之一

def CallStop(consumerConfig : ConsumerConfig, stopEndpoint : String ) = { 
      
      val jsonBody = consumerConfig.asJson
      val valuedJsonBody = Printer.noSpaces.copy(dropNullValues = true).print(jsonBody)
      println(valuedJsonBody)        
      println("stopEndpoint-" + stopEndpoint) 

      exec(http("StopConsumer-" + stopEndpoint)    
      .post(stopEndpoint)    
      .header(HttpHeaderNames.ContentType, HttpHeaderValues.ApplicationJson)
      .body(StringBody(valuedJsonBody))
      .check(status.is(200))
      .check(bodyString.saveAs("serverResponse"))
      )    
    .exec { session =>
      println("server_response: " + session("serverResponse").as[String])
      session
    }
  }

我看到了上面的println语句,但是没有POST请求。有人可以帮忙解释发生了什么事吗?

编辑 我是Gatling和Scala的新手,所以不确定如何调试或有断点。似乎它无声地失败了,这与我有关。

1 个答案:

答案 0 :(得分:0)

基于this-加特林DSL在挂钩内不起作用。我希望有一个警告,或者不要浪费时间。

我必须执行实际的POST请求,而不使用如下所示的Gatling DSL。

 def CallStop(consumerConfig : ConsumerConfig, stopEndpoint : String ) = { 
          
          val jsonBody = consumerConfig.asJson
          val valuedJsonBody = Printer.noSpaces.copy(dropNullValues = true).print(jsonBody)
          println(valuedJsonBody)        
          println("stopEndpoint:" + stopEndpoint) 
    
          val post = new HttpPost(stopEndpoint)
          post.setHeader("Content-type", "application/json")
          post.setEntity(new StringEntity(valuedJsonBody))
          // send the post request
          val client = new DefaultHttpClient
          val response = client.execute(post)
    
          println("Response:" + response)
      }