Skip to content
This repository has been archived by the owner on Sep 30, 2020. It is now read-only.

Commit

Permalink
feat(refactor): unify simulations filtering
Browse files Browse the repository at this point in the history
BREAKING CHANGE: simulations can only be configured via PatternFilterable-like closure
  • Loading branch information
Evgeny Shepelyuk authored and eshepelyuk committed Sep 16, 2019
1 parent 66d9344 commit b9c59ee
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 94 deletions.
63 changes: 26 additions & 37 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,19 @@ Using `Gradle` API file locations can be customized.
sourceSets {
gatling {
scala.srcDir "folder1" <1>
// or
scala.srcDirs = ["folder1"] <2>
resources.srcDir "folder2" <3>
// or
resources.srcDirs = ["folder2"] <4>
}
}
----
<1> append folder1 as a extra folder for storing simulations
<2> set folder1 as a single source of simulations
<3> append folder2 as a extra folder for storing `Gatling` resources
<4> append folder2 as a single source of `Gatling` resources
<1> append `folder1` as an extra simulations folder.
<2> use `folder1` as a single source of simulations.
<3> append `folder2` as an extra `Gatling` resources folder.
<4> use `folder2` as a single source of `Gatling` resources.

== Plugin configuration

Expand Down Expand Up @@ -98,47 +100,36 @@ The plugin defines the following extension properties in the `gatling` closure
|Additional systems properties passed to JVM together with caller JVM system properties

|simulations
|Closure or Iterable<String>
|Closure
|[source,groovy]
----
{ include "**/*Simulation*.scala" }
----
| Simulations filter. +
If closure then https://docs.gradle.org/current/userguide/working_with_files.html[See Gradle docs] for details,
otherwise an Iterable of simulations fully qualified names.
| Simulations filter. https://docs.gradle.org/current/javadoc/org/gradle/api/tasks/util/PatternFilterable.html[See Gradle docs] for details.
|===

=== Examples

Overriding Gatling version and JVM arguments::
Overriding Gatling version, JVM arguments and system properties::
+
[source,groovy,subs="attributes"]
----
gatling {
toolVersion = '{gatlingToolVersion}'
jvmArgs = [ '-server', '-Xms512M', '-Xmx512M' ]
toolVersion = '{gatlingToolVersion}'
jvmArgs = [ '-server', '-Xms512M', '-Xmx512M' ]
systemProperties = ['file.encoding': 'UTF-8']
}
----

Filtering simulations using FQN list::
Filtering simulations::
+
[source,groovy]
----
gatling {
simulations = [ 'com.package1.MySimu', 'com.package2.advanced.MySimulation' ] <1>
}
----
<1> only execute `com.package1.MySimu` and `com.package2.advanced.MySimulation` simulations.

Filtering simulations using Groovy closure::
+
[source,groovy]
----
gatling {
simulations = {
include "**/package1/*Simu.scala" <1>
include "**/package2/*Simulation.scala" <2>
}
simulations = {
include "**/package1/*Simu.scala" <1>
include "**/package2/*Simulation.scala" <2>
}
}
----
<1> all `Scala` files from plugin simulation dir subfolder `package1` ending with `Simu`.
Expand Down Expand Up @@ -192,10 +183,10 @@ Example::
[source,groovy]
----
gatling {
// do not include classes and resources from src/main
includeMainOutput = false
// do not include classes and resources from src/test
includeTestOutput = false
// do not include classes and resources from src/main
includeMainOutput = false
// do not include classes and resources from src/test
includeTestOutput = false
}
----

Expand All @@ -205,9 +196,9 @@ Example::
[source,groovy]
----
dependencies {
gatling 'com.google.code.gson:gson:2.8.0' <1>
gatlingCompile 'org.apache.commons:commons-lang3:3.4' <2>
gatlingRuntime 'cglib:cglib-nodep:3.2.0' <3>
gatling 'com.google.code.gson:gson:2.8.0' <1>
gatlingCompile 'org.apache.commons:commons-lang3:3.4' <2>
gatlingRuntime 'cglib:cglib-nodep:3.2.0' <3>
}
----
<1> adding `gson` library, available both in compile and runtime classpath.
Expand Down Expand Up @@ -237,11 +228,9 @@ Options are used in a fallback manner, i.e. if option is not set the value from
|Additional systems properties passed to JVM together with caller JVM system properties

|simulations
|Closure or Iterable<String>
|Closure
|null
|Simulations filter. +
If closure then https://docs.gradle.org/current/userguide/working_with_files.html[See Gradle docs] for details,
otherwise an Iterable of simulations fully qualified names.
|Simulations filter. https://docs.gradle.org/current/javadoc/org/gradle/api/tasks/util/PatternFilterable.html[See Gradle docs] for details.
|===

=== Default tasks
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,22 @@ class GatlingPlugin implements Plugin<Project> {
project.tasks.addRule("Pattern: $GATLING_RUN_TASK_NAME-<SimulationClass>: Executes single Gatling simulation.") {
String taskName ->
if (taskName.startsWith(GATLING_TASK_NAME_PREFIX)) {
createGatlingTask(taskName, [(taskName - GATLING_TASK_NAME_PREFIX)])
createGatlingTask(taskName, (taskName - GATLING_TASK_NAME_PREFIX))
}
}
}

void createGatlingTask(String taskName, Iterable<String> predefinedSimulations) {
void createGatlingTask(String taskName, String simulationFQN = null) {
def task = project.tasks.create(name: taskName,
dependsOn: project.tasks.gatlingClasses, type: GatlingRunTask,
description: "Execute Gatling simulation", group: "Gatling")

if (predefinedSimulations) {
task.configure { simulations = predefinedSimulations }
if (simulationFQN) {
task.configure {
simulations = {
include "${simulationFQN.replace('.', '/')}.scala"
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ package com.github.lkishalmi.gradle.gatling

import org.gradle.api.Project

import java.nio.file.Path
import java.nio.file.Paths

class GatlingPluginExtension {

static final String GATLING_MAIN_CLASS = 'io.gatling.app.Gatling'
Expand Down Expand Up @@ -33,9 +30,7 @@ class GatlingPluginExtension {

static final Map DEFAULT_SYSTEM_PROPS = ["java.net.preferIPv4Stack": true, "java.net.preferIPv6Addresses": false]

static final Closure DEFAULT_SIMULATIONS = {
include "**/*Simulation*.scala"
}
static final def DEFAULT_SIMULATIONS = { include("**/*Simulation*.scala") }

def toolVersion = GATLING_TOOL_VERSION

Expand All @@ -45,7 +40,7 @@ class GatlingPluginExtension {

def systemProperties = DEFAULT_SYSTEM_PROPS

def simulations = DEFAULT_SIMULATIONS
Closure simulations = DEFAULT_SIMULATIONS

def includeMainOutput = true
def includeTestOutput = true
Expand All @@ -57,13 +52,4 @@ class GatlingPluginExtension {
GatlingPluginExtension(Project project) {
this.project = project
}

Iterable<String> resolveSimulations(Closure simulationFilter) {
def scalaDirs = project.sourceSets.gatling.scala.srcDirs.collect { Paths.get(it.absolutePath) }
def scalaFiles = project.sourceSets.gatling.scala.matching(simulationFilter).collect { Paths.get(it.absolutePath) }

scalaFiles.collect { Path simu ->
scalaDirs.find { simu.startsWith(it) }.relativize(simu).join(".") - ".scala"
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.github.lkishalmi.gradle.gatling


import org.gradle.api.DefaultTask
import org.gradle.api.file.FileTree
import org.gradle.api.tasks.InputFiles
Expand All @@ -18,24 +17,15 @@ class GatlingRunTask extends DefaultTask {

def systemProperties

def simulations
Closure simulations

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

@InputFiles
FileTree getSimulationSources() {
def simulationFilter = this.simulations ?: project.gatling.simulations

if (simulationFilter != null && simulationFilter instanceof Closure) {
return project.sourceSets.gatling.scala.matching(simulationFilter)
} else if (simulationFilter != null && simulationFilter instanceof Iterable<String>) {
return project.sourceSets.gatling.scala.matching {
include simulationFilter.collect { "${it.replaceAll("\\.", "/")}.scala" }
}
}

throw new IllegalArgumentException("`simulations` property neither Closure nor Iterable<String>, simulations: $simulationFilter")
return project.sourceSets.gatling.scala.matching(simulationFilter)
}

List<String> createGatlingArgs() {
Expand All @@ -62,8 +52,8 @@ class GatlingRunTask extends DefaultTask {
def scalaSrcDirs = project.sourceSets.gatling.scala.srcDirs.collect { Paths.get(it.absolutePath) }
def scalaFiles = getSimulationSources().collect { Paths.get(it.absolutePath) }

return scalaFiles.collect { Path s ->
scalaSrcDirs.find { s.startsWith(it) }.relativize(s).join(".") - ".scala"
return scalaFiles.collect { Path srcFile ->
scalaSrcDirs.find { srcFile.startsWith(it) }.relativize(srcFile).join(".") - ".scala"
}
}

Expand Down
15 changes: 12 additions & 3 deletions src/test/groovy/functional/WhenRunFailingSimulationSpec.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ class WhenRunFailingSimulationSpec extends GatlingFuncSpec {
given:
new File(testProjectDir.root, "build.gradle") << """
gatling {
simulations = ['computerdatabase.FailedSimulation', 'computerdatabase.BasicSimulation']
simulations = {
include 'computerdatabase/FailedSimulation.scala'
include 'computerdatabase/BasicSimulation.scala'
}
}
"""
and: "add incorrect simulation"
Expand Down Expand Up @@ -51,7 +54,10 @@ class FailedSimulation extends Simulation {
given:
new File(testProjectDir.root, "build.gradle") << """
gatling {
simulations = ['computerdatabase.FailedSimulation', 'computerdatabase.BasicSimulation']
simulations = {
include 'computerdatabase/FailedSimulation.scala'
include 'computerdatabase/BasicSimulation.scala'
}
}
"""
and: "add incorrect simulation"
Expand Down Expand Up @@ -82,7 +88,10 @@ class FailedSimulation extends Simulation {
given:
new File(testProjectDir.root, "build.gradle") << """
gatling {
simulations = ['computerdatabase.FailedSimulation', 'computerdatabase.BasicSimulation']
simulations = {
include 'computerdatabase/FailedSimulation.scala'
include 'computerdatabase/BasicSimulation.scala'
}
}
"""
and: "add incorrect simulation"
Expand Down
4 changes: 2 additions & 2 deletions src/test/groovy/functional/WhenRunSimulationSpec.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class WhenRunSimulationSpec extends GatlingFuncSpec {
prepareTest()
when:
BuildResult result = executeGradle(GATLING_RUN_TASK_NAME)
then: "default tasks were executed succesfully"
then: "default tasks were executed successfully"
result.task(":$GATLING_RUN_TASK_NAME").outcome == SUCCESS
result.task(":gatlingClasses").outcome == SUCCESS
and: "all simulations were run"
Expand Down Expand Up @@ -80,7 +80,7 @@ gatling.charting.noReports = true
given:
prepareTest()
buildFile << """
gatling { simulations = ['computerdatabase.BasicSimulation'] }
gatling { simulations = { include 'computerdatabase/BasicSimulation.scala' } }
"""
when: '1st time'
BuildResult result = executeGradle("$GATLING_RUN_TASK_NAME")
Expand Down
Loading

0 comments on commit b9c59ee

Please sign in to comment.