Gatling sbt Plugin

The sbt plugin allows you to run Gatling tests from the command line, without the bundle, as well as to package your simulations for Gatling Enterprise

This sbt plugin integrates Gatling with sbt, allowing to use Gatling as a testing framework. It can also be used to package your Gatling project to run it on Gatling Enterprise.

Versions

Check out available versions on Maven Central.

Beware that milestones (M versions) are not documented for OSS users and are only released for Gatling Enterprise customers.

Setup

If you prefer to manually configure your sbt project rather than clone our sample, you need to add the Gatling plugin dependency to your project/plugins.sbt:

addSbtPlugin("io.gatling" % "gatling-sbt" % "MANUALLY_REPLACE_WITH_LATEST_VERSION")

And then add the Gatling library dependencies and enable the Gatling plugin in your build.sbt:

enablePlugins(GatlingPlugin)
libraryDependencies += "io.gatling.highcharts" % "gatling-charts-highcharts" % "MANUALLY_REPLACE_WITH_LATEST_VERSION" % "test"
libraryDependencies += "io.gatling"            % "gatling-test-framework"    % "MANUALLY_REPLACE_WITH_LATEST_VERSION" % "test"

‘Test’ vs ‘Integration Tests’ configurations

This plugin offers two different custom sbt configurations, named Gatling and GatlingIt. They are tied to different source directories (see next section for more details) and therefore allow to separate your simulations according to your needs, should you desire it.

Ideally:

  • Your simulations with low injection profiles, which may serve as functional tests, should live in src/test (the default source directory for the Gatling configuration), and run along your unit tests, since they would complete quickly
  • Longer, more complex simulations with high injection profiles, should live in src/it (the default source directory for the GatlingIt configuration) and be run on an as-needed basis.

Also, since they’re tied to separate sbt configurations, your sbt settings can then be customized per configuration. You can expect a relatively short simulation to run easily with the default JVM settings, but, for example, simulations with much higher load could require an increase of the max heap memory.

Default settings

For the Gatling configuration:

  • By default, Gatling simulations must be in src/test/scala, configurable using the Gatling / scalaSource setting.
  • By default, Gatling reports are written to target/gatling, configurable using the Gatling / target setting.

For the GatlingIt configuration:

  • By default, Gatling simulations must be in src/it/scala, configurable using the GatlingIt / scalaSource setting.
  • By default, Gatling reports are written to target/gatling-it, configurable using the GatlingIt / target setting.

If you override the default settings, you need to reset them on the project, eg:

Gatling / scalaSource := sourceDirectory.value / "gatling" / "scala"
lazy val root = (project in file(".")).settings(inConfig(Gatling)(Defaults.testSettings): _*)

Multi-project support

If you have a multi-project build, make sure to only configure the subprojects which contain Gatling Simulations with the Gatling plugin and dependencies as described above. Your Gatling subproject can, however, depend on other subprojects.

Usage

Running your simulations

As with any sbt testing framework, you’ll be able to run Gatling simulations using sbt standard test, testOnly, testQuick, etc… tasks. However, since the sbt Plugin introduces many customizations that we don’t want to interfere with unit tests, those commands are integrated into custom configurations, meaning you’ll need to prefix them with Gatling/ or GatlingIt/.

For example, run all Gatling simulations from the test configuration:

sbt Gatling/test

Or run a single simulation, by its FQN (fully qualified class name), from the it configuration:

sbt 'GatlingIt/testOnly com.project.simu.MySimulation'

Running your simulations on Gatling Enterprise Self-Hosted

Build from sources

Once you have configured the sbt plugin on your project, Gatling Enterprise Self-Hosted can build it from sources without additional configuration. Add your source repository and configure your simulation to build from sources using sbt.

To make sure your setup is correct, you can run the packaging command and check that you get a jar containing all the classes and extra dependencies of your project in target/gatling/<artifactId>-gatling-enterprise-<version>.jar:

sbt Gatling/enterprisePackage

Publish to a binary repository

Alternatively, you can package your simulations and publish them to a binary repository (JFrog Artifactory, Sonatype Nexus or AWS S3).

Enable publishing the Gatling test artifact, then define the repository:

Gatling / publishArtifact := true
publishTo := (
  if (isSnapshot.value)
    Some("private repo" at "REPLACE_WITH_YOUR_SNAPSHOTS_REPOSITORY_URL")
  else
    Some("private repo" at "REPLACE_WITH_YOUR_RELEASES_REPOSITORY_URL")
)

The packaged artifact will be automatically attached to your project and deployed with the tests classifier when you publish it:

sbt publish

You can also set:

  • GatlingIt / publishArtifact := true to publish Gatling simulations from the it configuration, this artifact will be published with the it qualifier
  • Compile / publishArtifact := false e.g. if your project only contains Gatling simulations and you don’t need to publish code from src/main.

Additional tasks

Gatling’s sbt plugin also offers four additional tasks:

  • Gatling/startRecorder: starts the Recorder, configured to save recorded simulations to the location specified by Gatling/scalaSource (by default, src/test/scala).
  • Gatling/generateReport: generates reports for a specified report folder.
  • Gatling/lastReport: opens by the last generated report in your web browser. A simulation name can be specified to open the last report for that simulation.
  • Gatling/copyConfigFiles: copies Gatling’s configuration files (gatling.conf & recorder.conf) from the bundle into your project resources if they’re missing.
  • Gatling/copyLogbackXml: copies Gatling’s default logback.xml.

Overriding JVM options

Gatling’s sbt plugin uses the same default JVM options as the bundle launchers or the Maven plugin, which should be sufficient for most simulations. However, should you need to tweak them, you can use overrideDefaultJavaOptions to only override those default options, without replacing them completely.

E.g., if you want to tweak Xms/Xmx to give more memory to Gatling

Gatling / javaOptions := overrideDefaultJavaOptions("-Xms1024m", "-Xmx2048m")

Sources

If you’re interested in contributing, you can find the gatling-sbt plugin sources on GitHub.

Edit this page on GitHub