当前位置: 首页 > 知识库问答 >
问题:

使用存根值从JSON编写Gatling定制feeder

廖君昊
2023-03-14

如何根据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
)

共有1个答案

龙隐水
2023-03-14

我通过读取文件、执行替换、将其写入临时文件,并使用Gatling的内置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
)

然后罐头变成了

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
}
}
 类似资料:
  • 我无法将Gatling的数据实时发送到influxDB。 我在Windows10上。加特林版本:2.3.0(最后一个)。InflxDB版本:1.3.5(最后一个是1.3.6)。 我的Gatling.conf: 我连接到inflxdb 我连接到我的数据库了。然后: 和 安东尼

  • 虽然 Glide 内置了大部分常用模型(URL, Uri, 文件路径等)的支持,你还是可能偶尔会遇到一种 Glide 不支持的类型。你也可能会遇到需要定制或调整 Glide 默认行为的情况。你甚至可能会想要集成一种新的拉取图片的方法,或更换 Glide 目前支持的 集成库 之外的网络库。 好在 Glide 是可扩展的。要添加对一种新的模型(Model)类型的支持,你需要按照以下步骤来执行: 实现一

  • 问题内容: 我正在尝试编写XSLT以将特定的网页转换为JSON。以下代码演示了Ruby如何进行此转换,但是XSLT不会生成有效的JSON(数组中有太多逗号)-有人知道如何编写XSLT来生成有效的JSON吗? 问题答案: 从中的行省略逗号,然后添加: 这将为除最后一项以外的每一项添加逗号。

  • 我正在进行性能测试,为此我有以下加特林脚本- 在中,我有用户电子邮件和密码。我必须为feeder中的每封电子邮件生成一个令牌,并将其添加到中的header中。 有人能指导我如何发送相同的

  • 我试图使用csv文件中的数据构建随机json请求。我已经定义了我的目标。场景功能如下 但是当我将日志级别设置为跟踪时,我看到所有请求都具有相同的值。我错过了什么吗?

  • 问题内容: 我有一个传递参数的url使用json_encode每个值,如下所示: 返回的结果为: 我可以用来解析每个值以进行进一步的数据处理吗? 谢谢。 问题答案: 如果第二个值为true,将返回一个对象或数组: