在场景范围外加特林执行-不调用POST请求 [英] Gatling exec outside scenario scope - POST requests are not called

查看:75
本文介绍了在场景范围外加特林执行-不调用POST请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to write gatling performance test where I am using the before and after blocks of the Gatling Simulation to make one-time HTTP post requests against the service.

 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并针对端点创建发布.

CallStart and CallStop and CallSet are not making POST request for some reason. The only POST request called is the one defined within the scenario PublishMessageRoundRobin which calls exec and creates post against endpoint.

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

they are defined very similar way here is one of them

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请求.有人可以帮忙解释发生了什么事吗?

I see the println statements above but there is no POST request. Can someone help explain what is going on?

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

EDIT I am new to Gatling and Scala so I am not sure how to debug or have breakpoints. It seems that it fails silently which is concerning to me.

推荐答案

基于

Based on this - Gatling DSL doesn't work inside hooks. I wish there was a warning or something to not waste time on that.

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

I had to perform the actual POST request without using Gatling DSL as below.

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

这篇关于在场景范围外加特林执行-不调用POST请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆