2017-02-21 45 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 
} 
} 
相关问题