Created
October 10, 2013 01:01
-
-
Save thirsch81/6911338 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package de.otto.lhotse.xlt.gradle | |
import groovy.json.StringEscapeUtils | |
import org.gradle.internal.reflect.Instantiator | |
import org.gradle.api.Plugin | |
import org.gradle.api.Project | |
import org.gradle.api.tasks.compile.JavaCompile | |
import org.gradle.api.tasks.Copy | |
import org.gradle.api.tasks.Sync | |
import org.gradle.api.tasks.JavaExec | |
import de.otto.lhotse.xlt.gradle.task.CleanXltResult | |
import de.otto.lhotse.xlt.gradle.XltUtilitiesExtension | |
import de.otto.lhotse.xlt.gradle.domain.XltPluginExtension | |
import de.otto.lhotse.xlt.gradle.domain.XltInstallation | |
import de.otto.lhotse.xlt.gradle.domain.XltMasterController | |
import de.otto.lhotse.xlt.gradle.domain.XltReportGenerator | |
import de.otto.lhotse.xlt.gradle.domain.XltTestsuite | |
import de.otto.lhotse.xlt.gradle.domain.XltTest | |
import de.otto.lhotse.xlt.gradle.domain.XltTransaction | |
import de.otto.lhotse.xlt.gradle.domain.XltConfiguration | |
/** | |
* @author Thomas | |
*/ | |
class XltPlugin implements Plugin<Project> { | |
void apply(Project project) { | |
def instantiateXltConfiguration = { String name -> | |
project.gradle.services.get(Instantiator).newInstance(XltConfiguration, name, project) | |
} | |
def instantiateXltTransaction = { String name -> | |
project.gradle.services.get(Instantiator).newInstance(XltTransaction, name, project) | |
} | |
project.with { | |
// ******** Utility methods ******** | |
ext { | |
getParam = { name, defaultValue = null -> (project.hasProperty(name) && project.getProperty(name).trim() != "") ? project.getProperty(name) : defaultValue } | |
hasParam = { name -> project.hasProperty(name) } | |
} | |
// ***** set up DSL extensions ***** | |
extensions.create "xlt", XltPluginExtension | |
extensions.create "xltUtils", XltUtilitiesExtension, project | |
xlt { | |
extensions.create "installation", XltInstallation, project | |
extensions.create "mastercontroller", XltMasterController, project | |
extensions.create "reportgenerator", XltReportGenerator, project | |
extensions.create "testsuite", XltTestsuite, project | |
extensions.create "test", XltTest, project | |
extensions.configurations = container XltConfiguration, instantiateXltConfiguration | |
extensions.configRepo = container XltConfiguration, instantiateXltConfiguration | |
mastercontroller.extensions.create "configuration", XltConfiguration, "configuration", project | |
reportgenerator.extensions.create "configuration", XltConfiguration, "configuration", project | |
extensions.agents = container XltConfiguration, instantiateXltConfiguration | |
test.extensions.transactions = container XltTransaction, instantiateXltTransaction | |
} | |
// *** default XLT configuration *** | |
xlt { | |
installation { | |
if(!System.env["XLT_HOME"]) { | |
throw new RuntimeException("Please install XLT from http://www.xceptance.com/products/xlt/downloads.html and set XLT_HOME environment variable.") | |
} | |
homeDir = file System.env["XLT_HOME"] | |
} | |
mastercontroller { | |
configuration { fromFile xlt.mastercontroller.propertiesFile } | |
} | |
reportgenerator { | |
configuration { fromFile xlt.reportgenerator.propertiesFile } | |
reportFrom = getParam "from" | |
reportTo = getParam "to" | |
mergeRules = getParam "mergeRules" | |
} | |
def hostname = InetAddress.localHost.hostName | |
agents { "devAgents" { fromFile "${xlt.testsuite.configSrcDir}/dev/${hostname}-agents.properties" } } | |
configurations { | |
"dev" { | |
fromFile "${xlt.testsuite.configSrcDir}/dev.properties" | |
merge "${xlt.testsuite.configSrcDir}/dev/${hostname}-test.properties" | |
def outputFile = "${xlt.testsuite.configSrcDir}/test.properties" | |
println "\tWriting development configuration for $hostname to $outputFile" | |
toFile file: outputFile, headline: "Auto-generated development configuration for ${hostname}" | |
} | |
"defaultProperties" { fromFile "${xlt.testsuite.configSrcDir}/default.properties" } | |
"projectProperties" { fromFile "${xlt.testsuite.configSrcDir}/project.properties" } | |
} | |
test { | |
config = getParam "config", "dev" | |
agents = getParam "agents", "devAgents" | |
result = getParam "result", new Date().format(xlt.dateFormat) | |
report = getParam "report" | |
runEmbedded = hasParam "embedded" | |
} | |
} | |
// *********** Java stuff ********** | |
apply plugin: "java" | |
sourceSets { report { java.srcDir "src/report/java" } } | |
configurations { reportCompile.extendsFrom compile } | |
repositories { | |
flatDir name: "distribution", dirs: xlt.installation.libDir | |
} | |
dependencies { | |
reportCompile fileTree(xlt.installation.libDir) | |
} | |
sourceSets.report.output.classesDir = file "$buildDir/reportgenerator-classes" | |
tasks.withType(JavaCompile) { options.encoding = "UTF-8" } | |
xlt.testsuite { uploadDir file("${buildDir}/testsuite") } | |
// ************* Tasks ************* | |
task("copyTestsuiteDefaultConfig", group: "Build", type: Copy) { | |
description = "Copies default, JVM and logging configuration from config to upload directory." | |
project.afterEvaluate { | |
from xlt.testsuite.configSrcDir | |
include "default.properties", "project.properties", "log4j.properties", "jvmargs*" | |
into xlt.testsuite.configDir | |
} | |
} | |
task("writeTestProperties", group: "Build") { | |
description = "Applies the provided test configuration and writes test.properties to upload directory." | |
dependsOn copyTestsuiteDefaultConfig | |
ext { headline = null } | |
doLast { | |
def testConfig = xlt.test.currentConfiguration() | |
testConfig.toFile file: file("${xlt.testsuite.configDir}/test.properties"), headline: headline | |
println testConfig.prettyPrint("\t") | |
} | |
} | |
task("copyLibs", group: "Build", type: Sync) { | |
dependsOn compileJava | |
project.afterEvaluate { | |
from sourceSets.main.runtimeClasspath | |
include "**/*.jar" | |
into xlt.testsuite.libDir | |
} | |
} | |
task("copyTestData", group: "Build", type: Copy) { | |
project.afterEvaluate { | |
from xlt.testsuite.dataSrcDir | |
into xlt.testsuite.dataDir | |
} | |
} | |
task("buildTestsuite", group: "Build") { | |
dependsOn copyTestsuiteDefaultConfig, writeTestProperties, copyLibs, copyTestData | |
description = "Prepares testsuite for use with XLT. Aggregate task." | |
doLast { println "\tDone." } | |
} | |
task("configureMastercontroller", group: "XLT") { | |
description = "Merges source config, agents and testsuite location and writes into mastercontroller.properties." | |
ext { | |
baseConfig = xlt.mastercontroller.configuration | |
headline = null | |
} | |
doLast { | |
def config = baseConfig | |
config.set "com.xceptance.xlt.mastercontroller.testSuitePath", StringEscapeUtils.escapeJava(xlt.testsuite.uploadDir.path) | |
config.include xlt.test.currentAgents() | |
config.toFile file: xlt.mastercontroller.propertiesFile, headline: headline | |
println config.prettyPrint("\t") | |
} | |
} | |
task("configureReportgenerator", group: "XLT") { | |
description = "Writes new configuration to reportgenerator.properties." | |
ext { | |
config = xlt.reportgenerator.configuration | |
headline = null | |
} | |
doLast { | |
config.toFile file: xlt.reportgenerator.propertiesFile, headline: headline | |
println config.prettyPrint("\t") | |
} | |
} | |
task("executeMastercontroller", group: "XLT", type: JavaExec) { | |
description = "Runs the configured XLT Mastercontroller in -auto mode." | |
dependsOn configureMastercontroller, buildTestsuite | |
ext { log4jConfigFile = null } | |
main = "com.xceptance.xlt.mastercontroller.Main" | |
project.afterEvaluate { | |
log4jConfigFile = xlt.mastercontroller.propertiesFile | |
args = xlt.mastercontroller.buildArguments xlt.test.resultDir.path, xlt.test.runEmbedded | |
classpath fileTree(xlt.installation.libDir) | |
jvmArgs "-Xmx2048m", | |
"-Dcom.xceptance.xlt.home=${xlt.installation.homeDir}", | |
"-Djava.awt.headless=true", | |
"-Dlog4j.configuration=file:${log4jConfigFile}" | |
} | |
doFirst { | |
println "\tUsing arguments: ${args.join(' ')}" | |
println "\tUsing log4j config file: ${log4jConfigFile}" | |
} | |
} | |
task("loadtest", group: "XLT") { | |
dependsOn executeMastercontroller | |
description = "Aggregate task, builds the testsuite, configures XLT and runs the loadtest." | |
doLast { | |
println "\tResult: ${xlt.test.resultDir}" | |
def timestamps = xltUtils.getTimestampsFromResult xlt.test.resultDir | |
xlt.test.startTime = timestamps.startTime | |
xlt.test.endTime = timestamps.endTime | |
file("${xlt.test.resultDir}/loadtest-time-window.csv").text = "${xlt.test.startTime},${xlt.test.endTime}" | |
} | |
} | |
task("createReport", group: "XLT", type: JavaExec) { | |
dependsOn configureReportgenerator, compileReportJava | |
description = "Runs the configured XLT Reportgenerator." | |
ext.log4jConfigFile = null | |
main = "com.xceptance.xlt.report.ReportGeneratorMain" | |
project.afterEvaluate { | |
log4jConfigFile = xlt.reportgenerator.propertiesFile | |
args = xlt.reportgenerator.buildArguments xlt.test.resultDir.path, xlt.test.reportDir.path | |
classpath sourceSets.report.runtimeClasspath | |
maxHeapSize = "2048m" | |
jvmArgs "-Dcom.xceptance.xlt.home=${xlt.installation.homeDir}", | |
"-Djava.awt.headless=true", | |
"-Dlog4j.configuration=file:${log4jConfigFile}" | |
} | |
doFirst { | |
println "\tUsing arguments: ${args.join(' ')}" | |
println "\tUsing log4j config file: ${log4jConfigFile}" | |
println "\tUsing jvm args:\n\t\t" + allJvmArgs.join("\n\t\t") | |
} | |
} | |
task("cleanResult", group: "XLT", type: CleanXltResult) { | |
description = "Cleans a loadtest result from unused XLT result browsers." | |
mustRunAfter createReport | |
project.afterEvaluate { | |
resultDir = xlt.test.resultDir | |
reportDir = xlt.test.reportDir | |
} | |
} | |
task("showXltConfig", group: "Help") { | |
doLast { | |
xlt.with { println """ | |
\tXLT home directory: ${installation.homeDir} | |
\tConfigured agents "${agents}": | |
${currentAgents().prettyPrint("\t\t")} | |
\tMastercontroller configuration (without agents) --> ${mastercontroller.propertiesFile}: | |
${mastercontroller.configuration.prettyPrint("\t\t")} | |
\tReportgenerator configuration --> ${reportgenerator.propertiesFile}: | |
${reportgenerator.configuration.prettyPrint("\t\t")} | |
""" } | |
} | |
} | |
task("showTestConfig", group: "Help") { | |
doLast { | |
xlt.test.with { println """ | |
\tConfigured agents "${agents}": | |
${currentAgents().prettyPrint("\t\t")} | |
\tCurrent default.properties: | |
${configurations.defaultProperties.prettyPrint("\t\t")} | |
\tCurrent project.properties: | |
${configurations.projectProperties.prettyPrint("\t\t")} | |
\tCurrent test.properties for selected configuration "${config}": | |
${currentConfiguration().prettyPrint("\t\t")} | |
""" } | |
} | |
} | |
task("showConfigRepo", group: "Help") { | |
doLast { | |
xlt.configRepo.each { | |
println "\tName: ${it.name}" | |
println it.prettyPrint("\t") | |
} | |
} | |
} | |
task("showConfigurations", group: "Help") { | |
doLast { | |
xlt.configurations.each { | |
println "\tName: ${it.name}" | |
println it.prettyPrint("\t") | |
} | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment