【问题标题】:Gatling exec outside scenario scope - POST requests are not calledGatling exec 超出场景范围 - 不调用 POST 请求
【发布时间】:2020-08-12 23:58:10
【问题描述】:

我正在尝试编写 gatling 性能测试,其中我使用 Gatling Simulation 的 beforeafter 块对服务发出一次性 HTTP post 请求。

 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 中定义的请求,它调用 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 的新手,所以我不确定如何调试或设置断点。似乎它默默地失败了,这与我有关。

【问题讨论】:

标签: gatling scala-gatling


【解决方案1】:

基于this - Gatling DSL 在钩子中不起作用。我希望有一个警告或其他东西不要浪费时间。

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

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

【讨论】:

    猜你喜欢
    • 2020-08-27
    • 1970-01-01
    • 1970-01-01
    • 2013-12-14
    • 2017-03-17
    • 1970-01-01
    • 2018-05-23
    • 1970-01-01
    • 2022-01-18
    相关资源
    最近更新 更多