简体   繁体   English

Gatling JSON Feeder 独特的 POST 机构

[英]Gatling JSON Feeder Unique POST Bodies

I have a JSON file that contains a JSON Array我有一个包含 JSON 数组的 JSON 文件

test.json测试文件

[
  { "Name": "Bob" },
  { "Age": "37" },
  { "DOB": "12/01/1985"}
]

I would like to test each respective element in the JSON array against an endpoint to observe the performance of the system against unique payloads我想针对端点测试 JSON 数组中的每个相应元素,以观察系统针对唯一有效负载的性能

currently I have目前我有

testService.scala testService.scala

val payload = jsonFile("test.json").circular
val httpProtocol = http
    .baseURL("http://test.com")
    .headers(Map("Content-Type" -> "application/json"))

val scn = scenario("Test Service")
    .feed(payload)
    .exec(http("test_request")
        .post("/v1/test")
        .queryParam("key", "123")
        .body()

I am not able to pass each respective child from the payload in the .body() as a JSON我无法将.body()的有效负载中的每个子项作为 JSON .body()

The Gatling Docs say that the JSON Feeder loads the each element of the Array into a record collection Gatling Docs 说 JSON Feeder 将 Array 的每个元素加载到记录集合中

https://gatling.io/docs/2.3/session/feeder/ https://gatling.io/docs/2.3/session/feeder/

ie: IE:

record1: Map("id" -> 19434, "foo" -> 1)
record2: Map("id" -> 19435, "foo" -> 2)

and set the body to .body(StringBody("""[{"id": ${id}}]"""))并将正文设置为.body(StringBody("""[{"id": ${id}}]"""))

The issue is I have different keys (Name,Age,DOB) and I'd like each one to be a different request sent.问题是我有不同的键(姓名、年龄、出生日期),我希望每个键都发送不同的请求。

.body(StringBody("""[{"KEY_NAME_HERE": ${KEY_NAME_HERE}}]"""))

How do I achieve this?我如何实现这一目标?

This is how i am doing:-这就是我的做法:-

company_users.json.json company_users.json.json

[
  {
    "env":"dev",
    "userName": "a@test.com",
    "password": "Qwerty!12345678"
  },
  {
    "env":"sit",
    "userName": "b@test.com",
    "password": "Qwerty!12345678"
  },
  {
    "env":"uat",
    "userName": "c@test.com",
    "password": "Qwerty!12345678"
  },
  {
    "env":"prod",
    "userName": "d@test.com",
    "password": "Qwerty!12345678"
  }
]

Working Code Snippet:工作代码片段:

val jsonFileFeederCompany = jsonFile("data/company_users.json").circular

val get_company_user_token = http("Get Company Tokens")
.post(gwt_token_url)
.header("Content-Type", "application/json")
.header("Accept", "application/json")
.body(StringBody(
  """{
      "env":  "${env}",
      "userName":  "${userName}",
      "password":  "${password}"
  }"""
)).asJson
.check(status.is(200))
.check(jsonPath("$.jwtToken").saveAs("jwtToken"))

val getCompanyUsersGwtToken = scenario("Create Company GWT token Scenario")
.feed(GetTokenRequest.jsonFileFeederCompany)
.exec(GetTokenRequest.get_company_user_token).exitHereIfFailed

This will read each array[position] from json and replace the values in request, to fetch security tokens from different env.这将从 json 读取每个 array[position] 并替换请求中的值,以从不同的环境中获取安全令牌。

Hope this helps.希望这可以帮助。

Regards, Vikram Pathania问候, 维克拉姆·帕塔尼亚

In your case JSONs from that array are loaded one by one, and since each first level key from that JSON will be saved as session attribute then users in your simulation end up with just 1 of 3 attributes depending which JSON was used.在您的情况下,来自该数组的 JSON 被一个一个加载,并且由于来自该 JSON 的每个第一级键都将保存为会话属性,因此您模拟中的用户最终只会获得 3 个属性中的一个,具体取决于使用的 JSON。 This way you can't (or to be precise can't easily) build body string.这样你就不能(或者准确地说不能很容易)建立身体字符串。 In that simple case it would be better to have JSONs with same fields, so you can rely on them when building request payload.在这种简单的情况下,最好使用具有相同字段的 JSON,这样您就可以在构建请求有效负载时依赖它们。 Fe.铁。 you can place payload key and value in separate fields:您可以将有效负载键和值放在单独的字段中:

[
  {
    "key":"Name",
    "value":"Bob"
  },
  {
    "key":"Age",
    "value":"37"
  },
  {
    "key":"DOB",
    "value":"12/01/1985"
  },
]

This way for each user in simulation you will have two attributes key and value so you will be able to construct payload like:通过这种方式,对于模拟中的每个用户,您将拥有两个属性keyvalue因此您将能够构建如下有效负载:

.body(StringBody("""{"${key}": "${value}"}"""))

Of course this will work only in that simple case you described and with string-only values in JSONs.当然,这仅适用于您描述的简单情况以及 JSON 中的纯字符串值。 If your final goal is to make something more complex please provide real-life example.如果您的最终目标是让事情变得更复杂,请提供现实生活中的例子。

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

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