简体   繁体   English

Gatling 更新文件用于在调用之前在 feed() 中提供模拟

[英]Gatling update file used to feed simulation within feed() before call

I need to update.csv file containing some ids used within Gatling simulation, as I need to create data beforehand.我需要更新 .csv 文件,其中包含加特林模拟中使用的一些 ID,因为我需要事先创建数据。 I tried to update that file within before() call however it won't work.我试图在 before() 调用中更新该文件,但它不起作用。 Lazy evaluation won't work either.惰性评估也不起作用。

  before {
    Helper.CreateDocuments()
  }

  lazy val documentIds = csv("data/documentIds.csv").circular

  val scn: ScenarioBuilder = scenario("PutFile")
    .feed(documentIds)
    .exec(http("Dynamic id")
      .put("files/${documentId}"))...

How can I solve that issue to feed simulation with refreshed ids?我如何解决该问题以使用刷新的 ID 进行模拟?

I've faced similar issue, and then I decided to not use CSV, but implicit feeders and to create a method to build my feeder.我遇到了类似的问题,然后我决定不使用 CSV,而是使用隐式馈送器并创建一种方法来构建我的馈送器。 I wasn't able to fix that by using the before hook, so created a builder method for my feeder.我无法通过使用 before 钩子来解决这个问题,所以为我的馈线创建了一个构建器方法。 An example on how it would work.关于它如何工作的一个例子。

import io.gatling.core.Predef._
import io.gatling.core.structure.ChainBuilder
import io.gatling.http.Predef._
import org.apache.http.client.methods.{HttpGet, HttpPost}
import org.apache.http.entity.StringEntity
import org.apache.http.impl.client.HttpClientBuilder
import org.apache.http.util.EntityUtils
import spray.json.DefaultJsonProtocol._
import spray.json._

object TestFacade {    
var test_feeder = getFeeder()
    
      val test_scenario: ChainBuilder = feed(test_feeder).group("TEST_GROUP") {
        exec(
          http("POST /test")
            .post("/test")
            .header("Content-Type", Configuration.APPLICATION_JSON)
            .body(ElFileBody("test.json"))
            .asJSON
            .check(status.find.in(200))
        )
      }
    
      def getFeeder() = {
        val result = getRestContent("http://www.randomnumberapi.com/api/v1.0/random?min=100&max=1000&count=5")
    
        val ids = result.parseJson.convertTo[Array[Int]]
        var coolVars = Array.empty[Map[String, String]]
    
        for (id <- ids) {
          println(id)
          coolVars = coolVars :+ Map("coolVar" -> id.toString)
        }
    
        coolVars.random
      }
      
      def getRestContent(url: String): String = {
        val httpClient = HttpClientBuilder.create().build()
        val httpResponse = httpClient.execute(new HttpGet(url))
        val entity = httpResponse.getEntity
        var content = ""
        if (entity != null) {
          content = EntityUtils.toString(entity, "UTF-8")
        }
        httpClient.getConnectionManager.shutdown()
    
        content
      }
}

just use initializing method to create the file, it will process before all stuff and should work只需使用初始化方法来创建文件,它将在所有内容之前处理并且应该可以工作

  /**
   * customize before
   */
  {
    Helper.CreateDocuments()
  }


You should be able to modify a csv file in the before step.您应该能够在before步骤中修改 csv 文件。 For example:例如:

  before {
    val pw = new PrintWriter(new File("data.csv"))
    for (i <- 0 to 10) {
      pw.write(i + "\n")
    }
    pw.close
  }

Remember to import the library import java.io._记得导入库import java.io._

And then use it in your simulation calling the file:然后在调用文件的模拟中使用它:

val orderId = csv("data.csv").queue

However this might be redundant as you can create the values an use the saveAs step, as mentioned in the Gatling Session API documentation .但是,这可能是多余的,因为您可以使用saveAs步骤创建值,如Gatling Session API 文档中所述 For example you can do something like:例如,您可以执行以下操作:

val someHttpCall = http("Create data for the feeder").get("/my/resource").saveAs("data")

And then use in the feeder:然后在喂料器中使用:

val scn = feed(data).exec(somethingElse)

If this is not enough, remember that you can also create you own feeder at any point in the simulation class.如果这还不够,请记住,您还可以在模拟课程的任何时候创建自己的馈线

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM