【问题标题】:How to read data from json array and pass it to Gatling on run time in loop如何从 json 数组中读取数据并在循环运行时将其传递给 Gatling
【发布时间】:2019-06-28 01:39:47
【问题描述】:

我是 gatling 和 scala 的新手。我正在尝试做以下事情:-

test.json:
[
  {
    "code": "AML",
    "data": "aaasdfgghh"
  },
  {
    "code": "ABC",
    "data": "aaasdfgghh"
  },
  {
    "code": "SDF",
    "data": "aaasdfgghh"
  }
]

现在我想在循环中读取这个 json 键/值。目前我正在这样做:

请求正文和馈线:-

   val jsonFileFeederFundsDoc: SourceFeederBuilder[Any] = 
                  jsonFile("test.json").circular

   //Saves locally and to S3
   val upload_fund_s3: HttpRequestBuilder =
    http("upload Fund docs Locally")
   .post(onboarding_url_perf + "/document/passfort")
   .headers(basic_headers)
   .headers(auth_headers)
   .body(StringBody(
    """{
      "code":  "${code}",
      "data":  "${data}"
  }"""
  )).asJson
  .check(status.is(200))
  .check(jsonPath("$.id").saveAs("id"))

我的场景是:-

val newFundScenario: ScenarioBuilder = scenario("Company Fund Scenarios exists")
.exec(TokenScenario.getCompanyUsersGwtToken).exitHereIfFailed
.exec(CompanyProfileRequest.check_company_exists).exitHereIfFailed
.doIf("${COMPANY_NOT_FOUND}") {
  exec(CompanyProfileRequest.create_company_fund_profile).exitHereIfFailed
}
.feed(UploadFundDocsInPassfort.jsonFileFeederFundsDoc)
.exec(UploadFundDocsInPassfort.upload_fund_s3).exitHereIfFailed
.exec(UploadFundDocsInPassfort.upload_fund_passfort).exitHereIfFailed
.exec(OfficersRequest.create_director).exitHereIfFailed   

我的测试模拟是:

  class TestCompanySimulation extends Simulation {

   private val generalCompanyTestCases = GeneralCompanyScenarios
    .newFundScenario
    .inject(rampUsers(Integer.getInteger("users", 1)) during 
    (Integer.getInteger("ramp", 1) minutes))

    setUp(
      generalCompanyTestCases.protocols(httpConf),
    )
 }

现在的问题是,对于一个用户,我想在我的 newFundScenario 中循环读取数据,但在这种情况下,它只是从 json 文件中选择第一个值。

有点像

 foreach(read data from json file){
 //Pass Data in run time and execute
 .exec(UploadFundDocsInPassfort.upload_fund_s3).exitHereIfFailed
 .exec(UploadFundDocsInPassfort.upload_fund_passfort).exitHereIfFailed
}

非常感谢任何帮助。

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

【问题讨论】:

    标签: scala gatling scala-gatling


    【解决方案1】:

    如果您希望每个用户循环通过 feeder,您需要在循环中包含 feed 命令。例如,如果您想发出 5 个请求...

    val upload_fund_s3: ChainBuilder =
    repeat(5) {
      feed(jsonFileFeederFundsDoc)
      .exec(
        http("upload Fund docs Locally")
        .post(onboarding_url_perf + "/document/passfort")
        .headers(basic_headers)
        .headers(auth_headers)
        .body(StringBody("""{"code":  "${code}", "data":  "${data}"}""")).asJson
        .check(status.is(200))
        .check(jsonPath("$.id").saveAs("id"))
      )
    } //include all the actions you want in the loop
    

    并从您的场景定义中删除提要。当您的 feeder 和使用它的操作被一起定义时,这也更好地阅读。

    请注意,如果您开始在模拟中运行多个用户,他们都会从同一个馈线中提取值。您已指定 .circular,因此它不会用完值,但每个用户可能会发出不同的请求(这可能没问题,具体取决于您的建模内容)

    如果您的列表很小,您也可以考虑只使用一个 Map 并将其推送到会话中以使用 .foreach 进行迭代

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-08
      • 2016-10-02
      • 2018-07-23
      • 1970-01-01
      • 1970-01-01
      • 2013-05-29
      相关资源
      最近更新 更多