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

In project/plugins.sbt, add the Gatling plugin dependency:

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

In build.sbt, add the Gatling library dependencies and enable the Gatling plugin:

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"

Please check our official sample project for sbt and Scala on GitHub.

‘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

Working with Gatling Enterprise Cloud

Package

You can directly package your simulations for Gatling Enterprise Cloud:

sbt Gatling/enterprisePackage

This will generate the target/gatling/<artifactId>-gatling-enterprise-<version>.jar package which you can then upload to the Cloud.

To package simulations from the it configuration, GatlingIt/enterprisePackage will generate the target/gatling-it/<artifactId>-gatling-enterprise-<version>.jar package.

Package and upload

You can also create and upload the package in a single command. You must already have configured a package (copy the package ID from the Packages table). You will also need an API token with appropriate permissions to upload a package.

Configure the package ID (and possibly the API token, but see below for other options) on the plugin:

Gatling / enterprisePackageId := "YOUR_PACKAGE_ID"
// omit enterpriseApiToken when using environment variable or Java System property instead
Gatling / enterpriseApiToken := "YOUR_API_TOKEN"

Since you probably don’t want to include you secret token in your source code, you can instead configure it using either:

  • the GATLING_ENTERPRISE_API_TOKEN environment variable
  • the gatling.enterprise.apiToken Java System property

Then package and upload your simulation to gatling Enterprise Cloud:

sbt Gatling/enterpriseUpload

To package and upload simulations from the it configuration, simply replace Gatling/ with GatlingIt/ in the configuration and command.

Working with 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