java 为 Gatling 负载测试构建可执行 JAR
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27893423/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Build executable JAR for Gatling load test
提问by stackoverflower
I am new to Gatling (2.1.2) and want to do a small prototype project to show to my colleagues.
我是 Gatling (2.1.2) 的新手,想做一个小的原型项目来展示给我的同事。
According to the quick startpage, there are several ways I can run a simulation with Gatling:
根据快速入门页面,我可以通过多种方式使用 Gatling 运行模拟:
- decompress the Gatling bundle into a folder and drop my simulation files into user-files/simulations folder. bin/gatling.sh will compile and run the simulation files.
- use the
gatling-maven-plugin
maven plugin to execute the simulation. - create a project with
gatling-highcharts-maven-archetype
, and run the Engine class.
- 将 Gatling 包解压到一个文件夹中,然后将我的模拟文件放到 user-files/simulations 文件夹中。bin/gatling.sh 将编译并运行模拟文件。
- 使用
gatling-maven-plugin
maven 插件来执行模拟。 - 创建一个项目
gatling-highcharts-maven-archetype
,并运行 Engine 类。
and I found those problems
我发现了这些问题
For 1, it is hard to add dependencies for simulation classes. I have to figure out what the jars are needed and drop them to the lib folder.
对于 1,很难为模拟类添加依赖项。我必须弄清楚需要什么罐子并将它们放到 lib 文件夹中。
For 2, it requires maven to be installed.
对于2,它需要安装maven。
For 3, it only runs from an IDE
对于 3,它只能从 IDE 运行
I just want a simple executable JAR file with all the dependencies bundled together (my simulation, Gatling and third party), and run it from any machine (like EC2 instances).
我只想要一个简单的可执行 JAR 文件,将所有依赖项捆绑在一起(我的模拟、Gatling 和第三方),并从任何机器(如 EC2 实例)运行它。
Is there a way to achieve this?
有没有办法实现这一目标?
Update 1:
更新 1:
I tried method 3, but moving all the project files from test
folder to main
, and used maven-assembly-plugin
to build a jar with dependencies. When I tried to run the file, I got the following error:
我尝试了方法 3,但将所有项目文件从test
文件夹移动到main
, 并用于maven-assembly-plugin
构建具有依赖项的 jar。当我尝试运行该文件时,出现以下错误:
Exception in thread "main" java.lang.ExceptionInInitializerError
at Engine$.delayedEndpoint$Engine(Engine.scala:7)
at Engine$delayedInit$body.apply(Engine.scala:4)
at scala.Function0$class.apply$mcV$sp(Function0.scala:40)
at scala.runtime.AbstractFunction0.apply$mcV$sp(AbstractFunction0.scala:12)
at scala.App$$anonfun$main.apply(App.scala:76)
at scala.App$$anonfun$main.apply(App.scala:76)
at scala.collection.immutable.List.foreach(List.scala:381)
at scala.collection.generic.TraversableForwarder$class.foreach(TraversableForwarder.scala:35)
at scala.App$class.main(App.scala:76)
at Engine$.main(Engine.scala:4)
at Engine.main(Engine.scala)
Caused by: java.nio.file.FileSystemNotFoundException
at com.sun.nio.zipfs.ZipFileSystemProvider.getFileSystem(ZipFileSystemProvider.java:171)
at com.sun.nio.zipfs.ZipFileSystemProvider.getPath(ZipFileSystemProvider.java:157)
at java.nio.file.Paths.get(Paths.java:143)
at io.gatling.core.util.PathHelper$.uri2path(PathHelper.scala:32)
at IDEPathHelper$.<init>(IDEPathHelper.scala:7)
at IDEPathHelper$.<clinit>(IDEPathHelper.scala)
... 11 more
I guess this is something to do with Gatling configuration, but don't know what has gone wrong.
我想这与 Gatling 配置有关,但不知道出了什么问题。
采纳答案by voho
I tried to do something similar. I could notuse Maven as well. I will try to remember how I did this.
我试图做类似的事情。我也不能使用 Maven。我会努力记住我是如何做到的。
1) I have configured maven-assembly-pluginto generate single JAR with dependencies like this:
1)我已经配置了maven-assembly-plugin来生成具有如下依赖项的单个 JAR:
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
You need to ensure all required libraries (gatling, scala runtime, zinc compiler) are present on your resulting classpath.
您需要确保生成的类路径中存在所有必需的库(gatling、scala 运行时、锌编译器)。
2) Check the scope of your dependencies as Maven packs only classes defined with scope=compileby default. The most simple way is probably to use no test dependencies.
2) 检查您的依赖项的范围,因为 Maven 仅打包默认使用scope=compile定义的类。最简单的方法可能是不使用测试依赖项。
3) Create a launch script, e.g. launch.sh. It should contain something like this:
3) 创建一个启动脚本,例如launch.sh。它应该包含如下内容:
#!/bin/sh
USER_ARGS="-Dsomething="
COMPILATION_CLASSPATH=`find -L ./target -maxdepth 1 -name "*.jar" -type f -exec printf :{} ';'`
JAVA_OPTS="-server -XX:+UseThreadPriorities -XX:ThreadPriorityPolicy=42 -Xms512M -Xmx2048M -XX:+HeapDumpOnOutOfMemoryError -XX:+AggressiveOpts -XX:+OptimizeStringConcat -XX:+UseFastAccessorMethods -XX:+UseParNewGC -XX:+UseConcMarkSweepGC -XX:+CMSParallelRemarkEnabled -Djava.net.preferIPv4Stack=true -Djava.net.preferIPv6Addresses=false ${JAVA_OPTS}"
java $JAVA_OPTS $USER_ARGS -cp $COMPILATION_CLASSPATH io.gatling.app.Gatling -s your.simulation.FullClassName
To explain, I took gatling`s own launch script for inspiration. Note mainly the presence of targetdirectory in classpath parameter definition.
为了解释,我以 gatling 自己的启动脚本为灵感。主要注意类路径参数定义中目标目录的存在。
4) Compile your compiled targetdirectory and launch.shto a single directory and distribute this (e.g. as archive). Then you can the scenarios by executing ./launch.sh.
4) 将编译后的目标目录和launch.sh编译到单个目录并分发它(例如作为存档)。然后你可以通过执行./launch.sh来创建场景。
I know this is nota standard solution, but it worked for me. Hopefully it will help you too. If you have any problems or tips to improve, please share with us.
我知道这不是标准解决方案,但它对我有用。希望它也能帮助你。如果您有任何问题或改进建议,请与我们分享。
回答by Pata
You can always create a simple Java class that starts Gatling with the Gatling.fromArgs. With this setup you can have all in just one happy executable jar. Let this class be the jar mainClass instead of "io.gatling.app.Gatling". This example is for a scala simulation class "my.package.MySimulation".
您始终可以创建一个简单的 Java 类,该类使用 Gatling.fromArgs 启动 Gatling。通过这种设置,您可以在一个愉快的可执行 jar 中拥有所有内容。让这个类成为 jar mainClass 而不是“io.gatling.app.Gatling”。此示例适用于 Scala 模拟类“my.package.MySimulation”。
import scala.Option;
import io.gatling.app.Gatling;
import io.gatling.core.scenario.Simulation;
public class StartSimulation {
public static void main(String[] args) {
Gatling.fromArgs(new String[]{}, new Option<Class<Simulation>>() {
private static final long serialVersionUID = 1L;
@Override
public int productArity() {
return 0;
}
@Override
public Object productElement(int arg0) {
return null;
}
@SuppressWarnings("unchecked")
@Override
public Class<Simulation> get() {
try {
return (Class<Simulation>) Class.forName("my.package.MySimulation");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return null;
}
@Override
public boolean isEmpty() {
return false;
}
@Override
public boolean canEqual(Object o) {
return false;
}
});
}
}
回答by kszosze
I think is a bit late for that but I face kinda the same problem related here, but instead to use maven I used gradle. Guess that the approach it's the same, a bit mix of the first solution and something or my own.
我认为这有点晚了,但我面临着与此处相关的相同问题,但为了使用 maven,我使用了 gradle。猜猜它的方法是一样的,有点混合第一个解决方案和我自己的东西。
First, define a gradle build file with gatling dependencies and a task to build a fatjar
首先,定义一个带有gatling依赖项的gradle构建文件和一个构建fatjar的任务
apply plugin: 'scala'
version 0.1
dependencies {
compile group: 'io.gatling', name: 'gatling-test-framework', version: '2.1.7'
compile group: 'com.typesafe.akka', name: 'akka-actor_2.11', version: '2.4.7'
compile group: 'org.scala-lang', name: 'scala-library', version: '2.11.7'
}
repositories{
mavenCentral()
mavenLocal()
}
task fatJar(type: Jar) {
manifest {
attributes 'Implementation-Title': 'Preparing test',
'Implementation-Version': version,
'Main-Class': 'io.gatling.app.Gatling'
}
baseName = project.name + '-all'
from { configurations.compile.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
}
That task executed as
该任务执行为
gradle clean build fatJar
will generate a self contained jar which will run the Gatling main class as default. So tell it witch test you want to run is made with the standard '-s' parameter.
将生成一个自包含的 jar,它将默认运行 Gatling 主类。因此,告诉它您要运行的女巫测试是使用标准的“-s”参数进行的。
So last step is create, if you want, a script to run it. I will "steal" the script for the first comment and change a bit
所以最后一步是创建(如果需要)一个脚本来运行它。我将“窃取”第一条评论的脚本并稍作更改
#!/bin/sh
if [ -z "" ];
then
echo "Test config tool"
echo
echo "Running Parameters : "
echo
echo " <Config file> : Test definition file. Required"
echo
exit 0;
fi
USER_ARGS="-DCONFIG_FILE="
JAVA_OPTS="-server -XX:+UseThreadPriorities -XX:ThreadPriorityPolicy=42 -Xms512M -Xmx2048M -XX:+HeapDumpOnOutOfMemoryError -XX:+AggressiveOpts -XX:+OptimizeStringConcat -XX:+UseFastAccessorMethods -XX:+UseParNewGC -XX:+UseConcMarkSweepGC -XX:+CMSParallelRemarkEnabled -Djava.net.preferIPv4Stack=true -Djava.net.preferIPv6Addresses=false ${JAVA_OPTS}"
java $JAVA_OPTS $USER_ARGS -jar test-project-all-0.1.jar -s FunctionalTestSimulation -nr
In my case I will run the same test with different, easy to configure, parameters so my Simulation is always the same. All my scala files are compiled by gradle and package in the jar that's mean they are in the classpath, changing the "FunctionalTestSimulation" name for a Script variable make easy adapt this script for something more generic.
就我而言,我将使用不同的、易于配置的参数运行相同的测试,因此我的模拟始终相同。我所有的 scala 文件都由 jar 中的 gradle 和包编译,这意味着它们在类路径中,更改脚本变量的“FunctionalTestSimulation”名称可以轻松地将此脚本调整为更通用的内容。
Guess that make a Maven version will be easy.
猜猜制作 Maven 版本会很容易。
Hope that help somebody.
希望对某人有所帮助。
Update with folder structureAfter a request will add an small draft of the folder structure for the project:
使用文件夹结构更新请求后将为项目添加文件夹结构的小草稿:
test-project
|_ build.gradle
|_ src
|_ main
|_ scala
|_ resources
|_ runSimulation.sh
|_ configFile.conf
When have time will provide a link to my github with a working one. Cheers
什么时候有时间会提供一个链接到我的 github 和一个可用的。干杯
回答by Saman
I had a similar issue, I fixed it as following:
我有一个类似的问题,我修复它如下:
Inside Gatling package there is bin/
and take a look at gatling.sh
. You see that it simply adds certain configurations into classpath and then runs io.gatling.app.Gatling
class in gatling-compiler-<version_number>.jar
. So, all you need to do is to make a jar that includes compiler, add configurations and tests to classpath and run io.gatling.app.Gatling
.
steps:
加特林包里面有bin/
,看看gatling.sh
。您会看到它只是将某些配置添加到类路径中,然后io.gatling.app.Gatling
在gatling-compiler-<version_number>.jar
. 因此,您需要做的就是制作一个包含编译器的 jar,将配置和测试添加到类路径并运行io.gatling.app.Gatling
。脚步:
add compiler dependency:
添加编译器依赖:
<dependency>
<groupId>io.gatling</groupId>
<artifactId>gatling-compiler</artifactId>
<version>${gatling.version}</version>
</dependency
create jar with dependencies:
创建具有依赖项的 jar:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4.1</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<finalName>${project.build.finalName}</finalName>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
create test jar (this includes your gatling tests)
创建测试 jar(这包括您的 gatling 测试)
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<goals>
<goal>test-jar</goal>
</goals>
<configuration>
<excludes>
<exclude>src/test/resources/*</exclude>
</excludes>
<finalName>${project.build.finalName}</finalName>
</configuration>
</execution>
</executions>
</plugin>
create a package out of your configuration. You can use maven assembly for that. What I usually do, is to create a separate module that handles creating the package for different environments. This package contains your gatling.conf
, logback.xml
and all the other resources you application wants including test data.
Now you basically have three packages: application.jar
, application-tests.jar
and application-conf.zip
.
Unzip application-conf.zip
, copy application.jar
and application-tests.jar
in the same folder.
根据您的配置创建一个包。您可以为此使用 Maven 程序集。我通常做的是创建一个单独的模块来处理为不同环境创建包。此包包含你gatling.conf
,logback.xml
以及所有其他资源你应用程序需要包括测试数据。现在您基本上拥有三个包:application.jar
,application-tests.jar
和application-conf.zip
. 解压 application-conf.zip
,复制 application.jar
并application-tests.jar
在同一个文件夹中。
In this folder, You need to create target/test-classes/
folder, just
leave it empty. In my case, it was required. I think you can some how
change that in gatling.conf
. But I am not sure how.
在此文件夹中,您需要创建target/test-classes/
文件夹,将其留空即可。就我而言,这是必需的。我认为你可以在gatling.conf
. 但我不确定如何。
Run
跑
java -cp ".:application-test.jar:application.jar" io.gatling.app.Gatling
回答by Alferd Nobel
I use IntelliJ Idea and I got this fixed by right clicking on the scala folder > Mark Directory as > Test Sources Root . Now Execute "Engine" and you will be all good !
我使用 IntelliJ Idea 并通过右键单击 scala 文件夹> Mark Directory as > Test Sources Root 解决了这个问题。现在执行“引擎”,你会一切都好!
回答by jamietanna
I've recently blogged about this Creating a versionable, self-contained (fat-/uber-) JAR for Gatling tests, the source of which can be found in jamietanna/fat-gatling-jar.
我最近写了一篇关于为 Gatling 测试创建一个可版本化的、自包含的(fat-/uber-)JAR 的博客,其来源可以在jamietanna/fat-gatling-jar 中找到。
For a Maven project, the steps would be as follows.
对于 Maven 项目,步骤如下。
The main things you need are to add the dependency on gatling-charts-highcharts
:
您需要做的主要事情是添加对 的依赖gatling-charts-highcharts
:
<project>
<!-- ... -->
<dependencies>
<dependency>
<groupId>io.gatling.highcharts</groupId>
<artifactId>gatling-charts-highcharts</artifactId>
<version>${gatling.version}</version>
</dependency>
</dependencies>
</project>
Next, you need to make sure your Gatling scenarios/simulations are in src/main
instead of src/test
.
接下来,您需要确保您的 Gatling 场景/模拟在src/main
而不是src/test
.
Finally, you can use the maven-shade-plugin
to build an executable JAR which uses Gatling's CLI runner as the mainClass
:
最后,您可以使用maven-shade-plugin
构建一个可执行 JAR,它使用 Gatling 的 CLI 运行器作为mainClass
:
<project>
<!-- ... -->
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.1.1</version>
<configuration>
<filters>
<!-- https://stackoverflow.com/a/6743609 -->
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>io.gatling.app.Gatling</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>