Gradle + Gatling

Hi, I am using Gradle for building my tests with gatling. In my build.gradle file I have the following task: task runLoadTest(type: JavaExec) {

classpath = sourceSets.main.runtimeClasspath

// Gatling application

main = “io.gatling.app.Gatling”

// Specify the simulation to run

args = me("[’-s’, ‘loadTests.LoadTest’]") //Gatling command } I need to pass JAVA_OPTS before running the gatling command. How can I do this?

You can use the ‘JavaExec#environment’ property (see Gradle Build Language Reference for details). However, ‘JAVA_OPTS’ is typically read by the script that starts the Java app, not by the Java app itself (or the JVM). You may want to set JVM arguments with ‘JavaExec#jvmArgs’ instead.

Ok, the solution was to add to this task line: systemProperties System.getProperties()

And then when I run it from terminal: gradle runLoadTest -Dusers=xxx -Dramp=yyy

Now what I need is to specify these parameters and predefine the task. For example gradle runSmallLoad //Runs gradle runLoadTest -Dusers=10 -Dramp=10 gradle runMediumLoad //Runs gradle runLoadTest -Dusers 100 -Dramp=100 etc.

How can I do this?

You can do this by declaring and configuring multiple ‘JavaExec’ tasks.

I started using gradle 1 week ago and writing tests 2 weeks ago. I am a newbie so could you be more specific please?

You already declared a JavaExec task in the code above. Now you need to declare a second ‘JavaExec’ task with a different name and different value for ‘systemProperties’ (e.g. ‘systemProperties = [users: “xxx”, ramp: “yyy”]’).

I actually managed to do it the other way: task runSmallLoad(type: Exec) {

commandLine ‘gradle’, ‘runLoadTest’, ‘-Dusers=5’, ‘-Dramp=5’ } Is it messy and it would be better to redo the way you told me?

Yes.

Thanks to you now I got this working. However, it looks messy as I have like 4 almost-the-same tasks. How can I refactor them? For example: task runLoad10(type: JavaExec) {

classpath = pathToClass

systemProperties = [users: 1, ramp: 9, rampTime: 3]

systemProperties System.getProperties()

main = pathToMain

args = argument }

to task runLoad10(type: JavaExec) {

runLoad(1,9,3) } def runLoad(a:Int, b:Int, c:Int) = {

classpath = pathToClass

systemProperties = [users: a, ramp: b, rampTime: c]

systemProperties System.getProperties()

main = pathToMain

args = argument }

You can create the tasks in a loop, or configure their commonalities in one place (e.g. ‘tasks.withType(JavaExec) { classpath = pathToClass; … }’). See Gradle User Guide for details.