2017-02-21 21 views
0

如何根據JSON文件編寫自定義Gatling進紙器,該JSON文件具有某些殘存值且需要在發送前需要替換的值?例如從帶有殘值的JSON編寫Gatling自定義進紙器

{"payloads":[ 
    {"groupId":"<GUID>", "epoch":<TIME>, "report":"somethingInteresting1"}, 
    {"groupId":"<GUID>", "epoch":<TIME>, "report":"somethingInteresting2"}, 
    {"groupId":"<GUID>", "epoch":<TIME>, "report":"somethingInteresting3"} 
]} 

jsonFile("/opt/gatling/user-files/simulation/cannedPayloads.json") 

將無法​​正常工作,我認爲,因爲它不是真正有效的JSON的文件中。我已經試過:

val jsonFileContents = Source.fromFile("/opt/gatling/user-files/simulation/cannedPayloads.json").getLines.mkString 
.replaceAll("<GUID>", java.util.UUID.randomUUID().toString()) 
.replaceAll("<TIME>", Instant.now().toEpochMilli().toString()) 

val feeder = JsonPath.query("$.payloads[*]", jsonFileContents).right.get.toArray.circular 

val scn1 = scenario("CannedTestSimulation").exec(feed(feeder).exec(
    http("to ingestion").post(url).body(StringBody("$")).asJSON 
) 

回答

0

我繞行它在文件中讀取,執行我的內容替換,編寫到一個臨時文件,並在jsonFile使用構建從加特林。該JSON基地成爲

[ 
    {"groupId":"<GUID>", "epoch":<TIME>, "report":"somethingInteresting1"}, 
    {"groupId":"<GUID>", "epoch":<TIME>, "report":"somethingInteresting2"}, 
    {"groupId":"<GUID>", "epoch":<TIME>, "report":"somethingInteresting3"} 
] 

的情況下變得

val scn1 = scenario("CannedTestSimulation").feed(jsonFile(CannedRequests.createTempJsonFile())).exec(
    http("send data").post(url).body(StringBody("$")).asJSON 
) 

和CannedRequests成爲

object CannedRequests { 
val jsonFile = "/opt/gatling/user-files/simulations/stubbed_data.json" 


def jsonFileContents(testSessionId: String): String = 
Source.fromFile(jsonFile).getLines.mkString 
.replaceAll("<GUID>", "gatling_"+testSessionId) 
.replaceAll("<TIME>", Instant.now().toEpochMilli().toString()) 

def createTempFile(contents: String, testSessionId: String): File = { 
val tempFile = File.createTempFile(testSessionId, ".json") 
val bw = new BufferedWriter(new FileWriter(tempFile)) 
bw.write(contents) 
bw.close 

tempFile 
} 

def createTempJsonFile():String = { 
val tempSessionId = java.util.UUID.randomUUID().toString() 
val tempContents = jsonFileContents(tempSessionId) 
val tempFile = createTempFile(tempContents, tempSessionId) 

tempFile.getAbsolutePath 
} 
}