如何使用 gatling 将来自 feeder 的 JSON 注入体内

问题描述 投票:0回答:0

我有一个加特林机场景

  1. 创建一个 Http 对象
  2. 创建一个带有正文的帖子请求
  3. 设置场景

我可以从包含以下内容的 JSON 文件中注入正文:

[{"id":"1"}]

此时我没有问题。我的问题是关于第二点。我希望用户使用不同的 ID 来获取他们的正文。所以我认为我必须使用馈线才能从 JSON 数组为每个用户设置正文。我用作 feeder 的 JSON 文件是这样的:

[{
    "id":"1"},
    {"id":"2"},
    {"id":"3"},
    {"id":"4"},
    {"id":"5"},
    {"id":"6"},
    {"id":"7"},
    {"id":"8"},
    {"id":"9"},
    {"id":"10"},
    {"id":"11"},
    {"id":"12"},
    {"id":"13"}
]

我不知道在来自 feeder 的 body 值中注入的语法。我在下面的代码中有一个假的身体线,你敢打赌它不起作用,但我不知道应该用什么来代替这条线。该行有评论:

import io.gatling.core.Predef._
import io.gatling.http.Predef._
import io.gatling.jdbc.Predef._
import io.gatling.http.HeaderNames._
import io.gatling.http.request._

import scala.concurrent.duration._
import scala.concurrent.duration._


class BasicSimulation extends Simulation {

  val httpConf = http
    .baseURL("http://localhost:8099")
    .headers(Map("Content-Type" -> "application/json"))
    .doNotTrackHeader("1")
    .acceptLanguageHeader("en-US,en;q=0.5")
    .acceptEncodingHeader("gzip, deflate")
    .userAgentHeader("Mozilla/5.0 (Windows NT 5.1; rv:31.0) Gecko/20100101 Firefox/31.0")

  val feeder = jsonFile("src/test/resources/json/gatling/test.json")

  val scnPost = scenario("BasicSimulation")
    .feed(feeder)
    .exec(http("request_1")
      .post("/spring-test")
      //I have to replace this line...
      .body(RawFileBody("${.}"))
      .check(status.is(200)))

  setUp(
    scnPost.inject(constantUsersPerSec(3) during(3 seconds))
  ).protocols(httpConf)
}

你知道如何从 feeder 中将 JSON 注入 body 吗?

scala gatling
© www.soinside.com 2019 - 2024. All rights reserved.