[英]Gatling update file used to feed simulation within feed() before call
我需要更新 .csv 文件,其中包含加特林模擬中使用的一些 ID,因為我需要事先創建數據。 我試圖在 before() 調用中更新該文件,但它不起作用。 惰性評估也不起作用。
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}"))...
我如何解決該問題以使用刷新的 ID 進行模擬?
我遇到了類似的問題,然后我決定不使用 CSV,而是使用隱式饋送器並創建一種方法來構建我的饋送器。 我無法通過使用 before 鈎子來解決這個問題,所以為我的饋線創建了一個構建器方法。 關於它如何工作的一個例子。
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
}
}
只需使用初始化方法來創建文件,它將在所有內容之前處理並且應該可以工作
/**
* customize before
*/
{
Helper.CreateDocuments()
}
您應該能夠在before
步驟中修改 csv 文件。 例如:
before {
val pw = new PrintWriter(new File("data.csv"))
for (i <- 0 to 10) {
pw.write(i + "\n")
}
pw.close
}
記得導入庫import java.io._
然后在調用文件的模擬中使用它:
val orderId = csv("data.csv").queue
但是,這可能是多余的,因為您可以使用saveAs
步驟創建值,如Gatling Session API 文檔中所述。 例如,您可以執行以下操作:
val someHttpCall = http("Create data for the feeder").get("/my/resource").saveAs("data")
然后在喂料器中使用:
val scn = feed(data).exec(somethingElse)
如果這還不夠,請記住,您還可以在模擬課程的任何時候創建自己的饋線。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.