DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • How to Build Your Own AI SRE From Scratch
  • Stop Preparing for Audits — Build the Pipeline That Audits Itself
  • Multi-Agent Software Engineering: Can AI Teams Build Production Systems?
  • Arm64 Is No Longer the Edge Case

Trending

  • Why Your AI Agent Works in the Demo and Fails in Production
  • Designing Frontend Applications for Failure, Not Just Success
  • 10 Best Practices for Managing Kubernetes at Scale
  • Introducing Graph Concepts in Java With Eclipse JNoSQL, Part 2: Understanding Neo4j

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Software Design and Architecture
  3. Performance
  4. Integrating Gatling Into a Gradle Build: Understanding SourceSets and Configuration

Integrating Gatling Into a Gradle Build: Understanding SourceSets and Configuration

Gradle sourceSets are logical groupings of related files. Configurations are groupings of related dependencies. Using both, we can create some neat build configurations.

By 
Biju Kunjummen
·
May. 08, 17 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
14.8K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

I recently worked on a project where we had to integrate the excellent load testing tool Gatling into a Gradle-based build. There are Gradle plugins available which make this easy, two of them being this and this, however for most needs, a simple execution of the command line tool itself suffices, so this post will go into some details of how Gatling can be hooked up into a Gradle build and in the process understand some good gradle concepts.

SourceSets and Configuration

To execute the Gatling CLI, I need a location for the source code and related content of the Gatling simulations, and I need a way to get the Gatling libraries. This is where two concepts of Gradle (sourceSets and Configuration) come into play.

Let us start with the first one - sourceSets.

sourceSets

sourceSets are simply a logical grouping of related files and are best demonstrated with an example. If I were to add a "java" plugin to a Gradle build:

apply plugin: 'java'

The sourceSets property would now show up with two values: "main" and "test," and if I wanted to find details of the these sourceSets, a Gradle task can be used for printing the details:

task sourceSetDetails {
    doLast {
        sourceSets {
            main {
                println java.properties
                println resources.properties
            }

            test {
                println java.properties
                println resources.properties
            }
        }
    }
}

Coming back to Gatling, I can essentially create a new sourceSet to hold the Gatling simulations:

sourceSets {
    simulations
}

This would now expect the Gatling simulations to reside in "src/simulations/java" and the resources related to it in "src/simulations/resources" folders, which is okay, but ideally I would want to keep it totally separate from the project sources. I would want my folder structure to be with load simulations in "simulations/load" and resources in "simulations/resources" folder. This can be tweaked by first applying the "scala" plugin, which brings in Scala compilation support to the project and then modifying the "simulations" sourceSet along these lines:

apply plugin: 'scala'

sourceSets {
    simulations {
        scala {
            srcDirs = ['simulations/load']
        }
        resources {
            srcDirs = ['simulations/resources']
        }
    }
}

With these set of changes, I can now put my simulations in the right place, but the dependency of Gatling and scala has not been pulled in, this is where the "configuration" feature of Gradle comes in.

Configuration

Gradle Configuration is a way of grouping related dependencies together. If I were to print the existing set of configurations using a task:

task showConfigurations  {
    doLast {
        configurations.all { conf -> println(conf) }
    }
}

These show up:

configuration ':archives'
configuration ':compile'
configuration ':compileClasspath'
configuration ':compileOnly'
configuration ':default'
configuration ':runtime'
configuration ':simulationsCompile'
configuration ':simulationsCompileClasspath'
configuration ':simulationsCompileOnly'
configuration ':simulationsRuntime'
configuration ':testCompile'
configuration ':testCompileClasspath'
configuration ':testCompileOnly'
configuration ':testRuntime'
configuration ':zinc'

"compile" and "testCompile" should be familiar ones; that is where a normal source dependency and a test dependency are typically declared like this:

dependencies {
    compile 'org.slf4j:slf4j-api:1.7.21'
    testCompile 'junit:junit:4.12'  
}

However, it also looks like there is now configuration for "simulations" sourceSet available - "simulationsCompile" and "simulationsRuntime," etc, so with this I can declare the dependencies required for my Gatling simulations using these configurations. However, my intention is to declare a custom configuration just to go over the concept a little more, so let us explicitly declare one:

configurations {
    gatling
}

And use this configuration for declaring the dependencies of Gatling:

dependencies {
    gatling 'org.scala-lang:scala-library:2.11.8'
    gatling 'io.gatling.highcharts:gatling-charts-highcharts:2.2.5'
}

Almost there. Now, how do we tell the sources in simulations source set to use dependency from Gatling configuration? By tweaking the sourceSet a little:

sourceSets {
    simulations {
        scala {
            srcDirs = ['simulations/load']
        }
        resources {
            srcDirs = ['simulations/resources']
        }

        compileClasspath += configurations.gatling
    }
}

Running a Gatling Scenario

With the sourceSets and the configuration defined, all we need to do is to write a task to run a Gatling simulation, which can be along these lines:

task gatlingRun(type: JavaExec) {
    description = 'Run gatling tests'
    new File("${buildDir}/reports/gatling").mkdirs()

    classpath = sourceSets.simulations.runtimeClasspath + configurations.gatling

    main = "io.gatling.app.Gatling"
    args = ['-s', 'simulations.SimpleSimulation',
            '-sf', 'simulations/resources',
            '-df', 'simulations/resources',
            '-rf', "${buildDir}/reports/gatling"
    ]
}

See how the compiled sources of the simulations and the dependencies from the Gatling configuration is being set as the classpath for the "JavaExec" task.

A good way to review this would be to look at a complete working sample that I have here in my github repo.

Gatling (software) Gradle Build (game engine)

Published at DZone with permission of Biju Kunjummen. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • How to Build Your Own AI SRE From Scratch
  • Stop Preparing for Audits — Build the Pipeline That Audits Itself
  • Multi-Agent Software Engineering: Can AI Teams Build Production Systems?
  • Arm64 Is No Longer the Edge Case

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook