1

我正在使用 JSON feeder 来比较 Web 服务的 JSON 输出,如下所示,

 val jsonFileFeeder = jsonFile("test_data.json")

    val strategy = (value: Option[String], session: Session) => value.map { jsonFileFeeder =>
      val result = JSONCompare.compareJSON("expectedStr", "actualStr", JSONCompareMode.STRICT)
      if (result.failed) Failure(result.getMessage)
      else Success(value)
      }.getOrElse(Failure("Missing body"))

      val login = exec(http("Login")
       .get("/login"))
      .pause(1)
      .feed(feeder)
      .exec(http("authorization")
        .post("/auth")
        .headers(headers_10)
        .queryParam("""email""", "${email}")
        .queryParam("""password""", "${password}")
        .check(status.is(200))
        .check(bodyString.matchWith(strategy)))
      .pause(1)

但它会引发错误

value matchWith is not a member of io.gatling.core.check.DefaultFindChe
ckBuilder[io.gatling.http.check.HttpCheck,io.gatling.http.response.Response,String,String]
15:10:01.963 [ERROR] i.g.a.ZincCompiler$ -         .check(bodyString.matchWith(jsonFileFeeder)))

s\lib\Login.scala:18: not found: value JSONCompare
15:10:05.224 [ERROR] i.g.a.ZincCompiler$ -       val result = JSONCompare.compareJSON(jsonFileFeeder, j
sonFileFeeder, JSONCompareMode.STRICT)
               ^
15:10:05.631 [ERROR] i.g.a.ZincCompiler$ - two errors found
Compilation failed
4

1 回答 1

2

这是一个示例脚本,它在语义上将 JSON 响应与预期输出进行比较:

import io.gatling.core.Predef._
import io.gatling.http.Predef._
import io.gatling.core.json.Jackson
import java.nio.charset.StandardCharsets.UTF_8
import scala.concurrent.duration._

class BasicSimulation extends Simulation {
  lazy val expectedJson = Jackson.parse(
      getClass.getResourceAsStream("/output.json"),
      UTF_8
    )

  val scn = scenario("Scenario Name")
    .exec(http("request_1")
      .get("http://localhost:8000/output.json")
      .check(bodyString.transform(Jackson.parse).is(expectedJson))
    )

  setUp(scn.inject(atOnceUsers(1)))
}

它假定目录中有一个文件(output.jsonresources目录也包含您的dataand request-bodies)。

但是,我认为您应该仔细考虑此解决方案是否适合您的需求。它的扩展性不如 JSONPath 或正则表达式检查(尤其是对于大型 JSON 文件),它不灵活,而且看起来更像是一项功能测试任务而不是性能任务。我怀疑如果您尝试以这种方式比较 JSON 文件,那么您可能正在尝试解决错误的问题。

请注意,它不使用jsonFile,因为jsonFile它是为用作馈送器而设计的,而我怀疑您想将单个请求与硬编码响应进行比较。但是,jsonFile如果您将使用不同的参数发出许多不同的请求并期望不同的(已知)响应,这可能会很有用。这是一个采用这种方法的示例脚本:

import io.gatling.core.Predef._
import io.gatling.http.Predef._
import io.gatling.core.json.Jackson
import scala.concurrent.duration._

class BasicSimulation extends Simulation {
  val myFeed = jsonFile("json_data.json").circular

  val scn = scenario("Scenario Name")
    .feed(myFeed)
    .exec(http("request_1")
      .get("${targetUrl}")
      .check(bodyString.transform(Jackson.parse).is("${expectedResponse}"))
    )

  setUp(scn.inject(atOnceUsers(2)))
}

它假定 中有一个 json 资源data/json_data.json,如下所示:

[
  {
    "targetUrl":"http://localhost:8000/failure.json",
    "expectedResponse":
      {
        "success": false,
        "message": "Request Failed"
      }
  },
  {
    "targetUrl":"http://localhost:8000/success.json",
    "expectedResponse":
      {
        "success": true,
        "message": "Request Succeeded"
      }
  }
]

expectedResponse应该是您希望从服务器返回的确切 JSON 。当然,您不仅需要参数化targetUrl,还可以通过这种方式参数化任何您想要的东西。

顺便说一句,您可能还想知道 Gatling 2.1 有望允许将响应与文件进行比较,而无需使用此类黑客(尽管当前的开发版本仅支持逐字节比较,而不是作为 json 比较)。

于 2014-09-10T14:25:38.440 回答