Cof*_*Pro 3 scala jar gradle gatling
我创建了一个 Scala 和 Gadling 项目,我想用它来执行一些负载测试。我的想法是构建一个可执行 jar,然后只需执行该 jar 即可运行所述负载测试。
\n到目前为止,我在 IDE 中运行负载测试没有任何问题。但是,当我尝试将其组装为:
\ngradle assemble\nRun Code Online (Sandbox Code Playgroud)\n该罐子是空的,即包含我的实用程序类src/main/scala,但没有任何内容src/gatling/* (see below for my setup).
我已经尝试过胖罐子,但该任务给了我错误:
\nExecution failed for task ':fatJar'.\n> Cannot expand ZIP 'D:\\projects\\load-test\\build\\classes\\java\\main' as it does not exist.\nRun Code Online (Sandbox Code Playgroud)\n我不知道为什么它首先需要 java 。
\n我的项目结构如下:
\nload-test/\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 src/\n\xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 gatling/\n\xe2\x94\x82 \xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 resources\n\xe2\x94\x82 \xe2\x94\x82 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 scala\n\xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 main/\n\xe2\x94\x82 \xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 resources\n\xe2\x94\x82 \xe2\x94\x82 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 scala\n\xe2\x94\x82 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 test/\n\xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 resources\n\xe2\x94\x82 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 scala\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 ...\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 build.gradle\nRun Code Online (Sandbox Code Playgroud)\n我发现 Gattle 插件非常顽固,它只包含gatlingCompileClasspath. 因此,为了能够编写负载测试,我需要有一个gatling源集。dirmain/scala用于实现一些实用功能,例如创建自定义时间戳等。test/scala tests those.
我的build.gradle的如下:
plugins {\n id "com.github.maiflai.scalatest" version "0.25"\n id 'io.gatling.gradle' version "3.4.0"\n id "scala"\n}\n\napply plugin: "scala"\napply plugin: "com.github.maiflai.scalatest"\n\ncompileScala.targetCompatibility = 1.8\nScalaCompileOptions.metaClass.useAnt = false\n\nrepositories {\n jcenter()\n maven {\n url "https://plugins.gradle.org/m2/"\n }\n}\n\nsourceSets {\n gatling {\n scala.srcDirs = ["src/gatling/scala"]\n resources.srcDirs = ["src/gatling/resources"]\n }\n\n test {\n scala.srcDirs = ["src/test/scala"]\n resources.srcDirs = ["src/test/resources"]\n }\n\n}\n\ndependencies {\n testCompile "gradle.plugin.com.github.maiflai:gradle-scalatest:0.25"\n testCompile "org.scala-lang:scala-library:2.12.12"\n testCompile 'org.scalatest:scalatest_2.12:3.0.0'\n testRuntime 'org.pegdown:pegdown:1.4.2'\n}\n\n\ngatling {\n toolVersion = '3.4.0'\n scalaVersion = '2.12.12'\n simulations = {\n include "**/*Simulation.scala"\n }\n systemProperties = ['file.encoding': 'UTF-8']\n}\n\n\ntask fatJar(type: Jar) {\n manifest {\n attributes 'Implementation-Version': project.version,\n 'Main-Class': 'io.gatling.app.Gatling'\n }\n setArchiveBaseName project.name + '-all'\n from { \n configurations.gatlingRuntimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }\n } {\n exclude 'META-INF/MANIFEST.MF'\n exclude 'META-INF/*.SF'\n exclude 'META-INF/*.DSA'\n exclude 'META-INF/*.RSA'\n\n }\n with jar\n}\nRun Code Online (Sandbox Code Playgroud)\n
经过大量实验和数小时在文档中搜索提示后,这是实现了这一点的构建 gradle 片段:
configurations {
fatJarDependencies.extendsFrom gatling
}
task fatJar(type: Jar, dependsOn: [':gatlingClasses', ':processResources']) {
manifest {
attributes 'Implementation-Title': 'Preparing test',
'Implementation-Version': '',
'Main-Class': 'io.gatling.app.Gatling'
}
classifier = 'all'
from files(sourceSets.main.output.classesDirs)
from files(sourceSets.gatling.output)
from { configurations.fatJarDependencies.collect { it.isDirectory() ? it : zipTree(it) } } {
exclude 'META-INF/MANIFEST.MF'
exclude 'META-INF/*.SF'
exclude 'META-INF/*.DSA'
exclude 'META-INF/*.RSA'
}
with jar
}
Run Code Online (Sandbox Code Playgroud)
关键有以下几点:
1. 配置
fatJarDependencies.extendsFrom gatling
Run Code Online (Sandbox Code Playgroud)
您需要扩展gatling,因为类似configurations.gatlingRuntimeClasspath.collect或configurations.gatling.collect在 fatJar 任务中什么也不做,即使这些配置存在(至少后者)。为了将依赖项放入你的 jar 中(gatrin 本身、scala 等等),这是必要的。
2. 取决于
准确地说:
(type: Jar, dependsOn: [':gatlingClasses', ':processResources'])
Run Code Online (Sandbox Code Playgroud)
特别是,我通过扫描gatting gradle 插件gatlingClasses的文档页面发现了这一点。这将添加下面的代码src/gatling/*
我可以通过执行以下命令来运行模拟(-DConfig可选):
java -Dconfig.resource=myConf.conf -jar the-jar-i-created.jar -s org.comp.pckg.DemoSimulation
Run Code Online (Sandbox Code Playgroud)
正如George Leung所指出的,如果您按照上面的方式开始评论(我错误地忘记了我添加了该选项-nr),也可以在 jar 之外生成报告。为了自定义文件夹,我添加了一个具有以下值的文件gatling.conf夹:src/gatling/resources
gatling {
core {
directory {
results = "/gatling/reports"
}
}
}
Run Code Online (Sandbox Code Playgroud)
这会覆盖您可以在此处找到的默认值。