使用Gradle为Gatling构建可执行的Jar文件

3

问题

我创建了一个Scala和Gatling项目,想要执行一些负载测试。我的想法是构建可执行的jar文件,然后简单地执行它以运行所述的负载测试。

到目前为止,在我的IDE中运行负载测试没有任何问题。但是,当我尝试使用以下命令进行组装时:

gradle assemble

这个jar包是空的,即只包含我从src/main/scala中提取的实用类,而没有src/gatling/*中的内容(有关我的设置,请参见下文)。

我尝试使用fat jar,但该任务给出了以下错误:

Execution failed for task ':fatJar'.
> Cannot expand ZIP 'D:\projects\load-test\build\classes\java\main' as it does not exist.

我不知道为什么它首先期望Java。
我的设置
我的项目结构如下:
load-test/
├── src/
│   ├── gatling/
│   │   ├── resources
│   │   └── scala
│   ├── main/
│   │   ├── resources
│   │   └── scala
│   └── test/
│       ├── resources
│       └── scala
├── ...
└── build.gradle

我发现 Gatling 插件非常顽固,它只在 gatlingCompileClasspath 中包含 Gatling 依赖项。因此,为了能够编写负载测试,我需要有一个 gatling 源集。 main/scala 目录用于实现一些实用程序功能,例如创建自定义时间戳等。 test/scala 用于测试这些功能。
我的 build.gradle 如下:
plugins {
    id "com.github.maiflai.scalatest" version "0.25"
    id 'io.gatling.gradle' version "3.4.0"
    id "scala"
}

apply plugin: "scala"
apply plugin: "com.github.maiflai.scalatest"

compileScala.targetCompatibility = 1.8
ScalaCompileOptions.metaClass.useAnt = false

repositories {
    jcenter()
    maven {
        url "https://plugins.gradle.org/m2/"
    }
}

sourceSets {
    gatling {
        scala.srcDirs = ["src/gatling/scala"]
        resources.srcDirs = ["src/gatling/resources"]
    }

    test {
        scala.srcDirs = ["src/test/scala"]
        resources.srcDirs = ["src/test/resources"]
    }

}

dependencies {
    testCompile "gradle.plugin.com.github.maiflai:gradle-scalatest:0.25"
    testCompile "org.scala-lang:scala-library:2.12.12"
    testCompile 'org.scalatest:scalatest_2.12:3.0.0'
    testRuntime 'org.pegdown:pegdown:1.4.2'
}


gatling {
    toolVersion = '3.4.0'
    scalaVersion = '2.12.12'
    simulations = {
        include "**/*Simulation.scala"
    }
    systemProperties = ['file.encoding': 'UTF-8']
}


task fatJar(type: Jar) {
    manifest {
        attributes 'Implementation-Version': project.version,
                'Main-Class': 'io.gatling.app.Gatling'
    }
    setArchiveBaseName project.name + '-all'
    from { 
        configurations.gatlingRuntimeClasspath.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
}

可能是重复的。https://dev59.com/414c5IYBdhLWcg3wdKKT - George Leung
很遗憾,那没有帮助到我 :( - Younes El Ouarti
1
Gatling 不喜欢以非官方的方式启动。话虽如此,你可以尝试一些 Gradle 技巧。 - George Leung
你可能是对的。这很令人沮丧,但似乎只能通过gradle任务来运行模拟。 - Younes El Ouarti
1个回答

5
经过大量实验和数小时在文档中搜索提示,以下是可行的build.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
}

重点在于以下内容: 1. 配置
fatJarDependencies.extendsFrom gatling

你需要扩展gatling,因为像configurations.gatlingRuntimeClasspath.collectconfigurations.gatling.collect这样的内容在你的fatJar任务中没有任何作用,尽管这些配置存在(至少后者)。这是为了将依赖项(gatling本身、scala等)放入您的jar包中所必需的。 2. dependsOn 具体来说:
(type: Jar, dependsOn: [':gatlingClasses', ':processResources'])

特别是 gatlingClasses,我是通过扫描gatling gradle插件的文档页面找到的。这将在src/gatling/*下添加代码。

我可以通过执行以下命令来运行模拟(-DConfig是可选项):

java -Dconfig.resource=myConf.conf -jar the-jar-i-created.jar -s org.comp.pckg.DemoSimulation

正如George Leung所指出的那样,如果您按照上述方式启动注释(我错误地忘记了添加选项-nr),也可以在jar文件外部生成报告。为了自定义文件夹,我添加了一个gatling.conf文件,并设置如下值:src/gatling/resources
gatling {
  core {
    directory {
      results = "/gatling/reports"
    }
  }
}

这将覆盖默认值,您可以在这里找到默认值。

1
难道没有报告的原因不是-nr标志吗? - George Leung
@GeorgeLeung 我已经编辑了我的答案以反映你的提示。再次感谢! - Younes El Ouarti
2
非常有帮助!谢谢! - Lucy

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接