Skip to content

Commit

Permalink
feat: add new recorder task for Gatling 3.11
Browse files Browse the repository at this point in the history
  • Loading branch information
slandelle committed Feb 20, 2024
1 parent 29717b7 commit 6914346
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 15 deletions.
7 changes: 7 additions & 0 deletions src/main/groovy/io/gatling/gradle/GatlingPlugin.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ final class GatlingPlugin implements Plugin<Project> {

public static def GATLING_RUN_TASK_NAME = 'gatlingRun'

public static def GATLING_RECORDER_TASK_NAME = 'gatlingRecorder'

static String GATLING_TASK_NAME_PREFIX = "$GATLING_RUN_TASK_NAME-"

public static def ENTERPRISE_PACKAGE_TASK_NAME = "gatlingEnterprisePackage"
Expand Down Expand Up @@ -47,6 +49,11 @@ final class GatlingPlugin implements Plugin<Project> {
group = "Gatling"
}

project.tasks.register(GATLING_RECORDER_TASK_NAME, GatlingRecorderTask.class) {
description = "Launch recorder"
group = "Gatling"
}

registerGatlingTask(project, GATLING_RUN_TASK_NAME, null)

project.tasks.addRule("Pattern: $GATLING_RUN_TASK_NAME-<SimulationClass>: Executes single Gatling simulation.") { String taskName ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,8 @@ class GatlingPluginExtension {

static final String GATLING_MAIN_CLASS = 'io.gatling.app.Gatling'

static final String GATLING_RECORDER_CLASS = 'io.gatling.recorder.GatlingRecorder'

static final String JAVA_SIMULATIONS_DIR = "src/gatling/java"

static final String SCALA_SIMULATIONS_DIR = "src/gatling/scala"
Expand All @@ -299,7 +301,7 @@ class GatlingPluginExtension {

static final String RESOURCES_DIR = "src/gatling/resources"

static final String GATLING_VERSION = '3.10.3'
static final String GATLING_VERSION = '3.10.4'

static final String SCALA_VERSION = '2.13.12'

Expand Down
93 changes: 93 additions & 0 deletions src/main/groovy/io/gatling/gradle/GatlingRecorderTask.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package io.gatling.gradle

import org.gradle.api.Action
import org.gradle.api.DefaultTask
import org.gradle.api.tasks.*
import org.gradle.process.JavaExecSpec
import org.gradle.process.internal.ExecException

class GatlingRecorderTask extends DefaultTask {

@Input
@Optional
String simulationClass

@Input
@Optional
String simulationPackage

@OutputDirectory
File gatlingReportDir = project.file("${project.reportsDir}/gatling")

GatlingRecorderTask() {
outputs.upToDateWhen { false }
}

List<String> createRecorderArgs() {
def gatling = project.sourceSets.gatling

File javaSrcDir = gatling.hasProperty("java") ? gatling.java.srcDirs?.find() : null
File scalaSrcDir = gatling.hasProperty("scala") ? gatling.scala.srcDirs?.find() : null
File kotlinSrcDir = gatling.hasProperty("kotlin") ? gatling.kotlin.srcDirs?.find() : null
File resourcesDir = gatling.resources.srcDirs?.find()

List<String> args
if (scalaSrcDir != null && scalaSrcDir.exists()) {
args = [
"-sf", scalaSrcDir.getAbsolutePath(),
"-fmt", "scala"
]
} else if (kotlinSrcDir != null && kotlinSrcDir.exists()) {
args = [
"-sf", kotlinSrcDir.getAbsolutePath(),
"-fmt", "kotlin"
]
} else if (javaSrcDir != null && javaSrcDir.exists()) {
args = [
"-sf", javaSrcDir.getAbsolutePath()
// let the Recorder pick a default Java format based on Java version
]
} else {
throw new IllegalStateException("None of the scala/kotlin/java src dir exist")
}

args += [ "-rf", resourcesDir.getAbsolutePath() ]

if (simulationPackage != null) {
args += [ "-pkg", simulationPackage ]
}

if (simulationClass != null) {
args += [ "-cn", simulationClass ]
}

return args
}

@TaskAction
void gatlingRecorder() {
def gatlingExt = project.extensions.getByType(GatlingPluginExtension)

def result = project.javaexec({ JavaExecSpec exec ->
exec.mainClass.set(GatlingPluginExtension.GATLING_RECORDER_CLASS)
exec.classpath = project.configurations.gatlingRuntimeClasspath

def logbackFile = LogbackConfigTask.logbackFile(project.buildDir)
if (logbackFile.exists()) {
exec.systemProperty("logback.configurationFile", logbackFile.absolutePath)
}

exec.args this.createRecorderArgs()

exec.standardInput = System.in

exec.ignoreExitValue = true
} as Action<JavaExecSpec>)

try {
result.rethrowFailure()
} catch (ExecException e) {
throw new TaskExecutionException(this, new RuntimeException("Failed to launch recorder", e))
}
}
}
30 changes: 16 additions & 14 deletions src/main/groovy/io/gatling/gradle/GatlingRunTask.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,6 @@ class GatlingRunTask extends DefaultTask {
outputs.upToDateWhen { false }
}

List<String> createGatlingArgs(String gatlingVersion) {
def gatlingVersionComponents = gatlingVersion.split("\\.")
int gatlingMajorVersion = Integer.valueOf(gatlingVersionComponents[0])
int gatlingMinorVersion = Integer.valueOf(gatlingVersionComponents[1])

def baseArgs = ["-rf", gatlingReportDir.absolutePath]

return (gatlingMajorVersion == 3 && gatlingMinorVersion >= 8) || gatlingMajorVersion >= 4 ?
baseArgs + ["-l", "gradle", "-btv", GradleVersion.current().version] :
baseArgs
}

@TaskAction
void gatlingRun() {
def gatlingExt = project.extensions.getByType(GatlingPluginExtension)
Expand All @@ -68,8 +56,7 @@ class GatlingRunTask extends DefaultTask {
exec.systemProperty("logback.configurationFile", logbackFile.absolutePath)
}

exec.args this.createGatlingArgs(gatlingExt.gatlingVersion)
exec.args "-s", simulationClass
exec.args this.createGatlingArgs(simulationClass, gatlingExt.gatlingVersion)

exec.standardInput = System.in

Expand All @@ -91,4 +78,19 @@ class GatlingRunTask extends DefaultTask {

return SimulationFilesUtils.resolveSimulations(classpath, includes, excludes)
}

List<String> createGatlingArgs(String simulationClass, String gatlingVersion) {
def baseArgs = [
"-s", simulationClass,
"-rf", gatlingReportDir.absolutePath
]

def gatlingVersionComponents = gatlingVersion.split("\\.")
int gatlingMajorVersion = Integer.valueOf(gatlingVersionComponents[0])
int gatlingMinorVersion = Integer.valueOf(gatlingVersionComponents[1])

return (gatlingMajorVersion == 3 && gatlingMinorVersion >= 8) || gatlingMajorVersion >= 4 ?
baseArgs + ["-l", "gradle", "-btv", GradleVersion.current().version] :
baseArgs
}
}

0 comments on commit 6914346

Please sign in to comment.