从 SBT 执行 Gatling 负载测试

Executing Gatling load tests from SBT

我使用 Spray 框架编写了一个小型 Web 服务。我可以 运行 使用 sbt run 然后在浏览器中测试它。

现在我用Gatling框架写了一个负载测试

package com.abhi

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

class LoadTest extends Simulation {
   val httpConf = http
      .baseURL("http://localhost:8999")
      .acceptHeader("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 scn = scenario("BasicSimulation")
      .exec(http("request_1")
      .get("/path1"))
      .pause(5)

   setUp(
      scn.inject(atOnceUsers(10))
   ).protocols(httpConf)
}

测试在文件夹src/test/scala-2.11,喷码在src/main/scala-2.11

当我 运行 sbt test 它只是打印成功而没有 运行ning 我的网络服务器或 运行ning 上面定义的测试。它只是在 3 秒内打印成功并退出。

我怎么能

  1. 确保当我执行 sbt test 喷射 Web 应用程序时 开始
  2. galing 负载测试是 运行 针对已启动的服务器

编辑:我也试过 sbt test scenarios:BasicSimulation 但它抛出了错误

Not a valid key: BasicSimulation
[error] scenarios:BasicSimulation

您必须使用 Gatling sbt plugin to trigger from sbt. Beware that it doesn't run in the default configuration but in the gatling one: gatling:test.

Gatling 已作为 Scala 应用程序编写 - 它按照标准 JVM 应用程序启动 - 您可以在 startup script gatling.sh:

中看到这一点
# Run Gatling
java $JAVA_OPTS -cp "$GATLING_CLASSPATH" io.gatling.app.Gatling "$@"

SBT "know" 如何启动 Gatling Simulation 的方式与 "knows" 如何 运行 Specs2 Specification 的方式相同。幸运的是,似乎有一个可用的 Gatling SBT 插件可以完全满足您的需求 - 查看demo project on GitHub