为Gatling负载测试构建可执行JAR

为Gatling负载测试构建可执行JAR

问题描述:

我是Gatling(2.1.2)的新手,想要做一个小型原型项目给同事们展示。

I am new to Gatling (2.1.2) and want to do a small prototype project to show to my colleagues.

根据快速入门页面,有几种方法可以用Gatling运行模拟:

According to the quick start page, there are several ways I can run a simulation with Gatling:


  1. 将Gatling包解压缩到一个文件夹中,然后将我的模拟文件放到用户文件/模拟文件夹中。 bin / gatling.sh将编译并运行模拟文件。

  2. 使用 gatling-maven-plugin maven插件执行模拟。

  3. gatling-highcharts-maven-archetype 创建一个项目,然后运行Engine类。

  1. 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.
  2. use the gatling-maven-plugin maven plugin to execute the simulation.
  3. create a project with gatling-highcharts-maven-archetype, and run the Engine class.

我发现了这些问题

对于1,很难为模拟类添加依赖项。我必须弄清楚需要什么罐子并将它们放到lib文件夹中。

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.

对于2,它需要安装maven。

For 2, it requires maven to be installed.

对于3,它只从IDE运行

For 3, it only runs from an IDE

我只想要一个简单的可执行JAR文件,其中所有依赖项捆绑在一起(我的模拟, Gatling和第三方),并从任何机器(如EC2实例)运行。

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).

有没有办法实现这个目标?

Is there a way to achieve this?

更新1:

我尝试了方法3,但是从测试中移动了所有项目文件文件夹到 main ,并使用 maven-assembly-plugin 构建一个带有依赖项的jar 。当我尝试运行该文件时,出现以下错误:

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:

Exception in thread "main" java.lang.ExceptionInInitializerError
    at Engine$.delayedEndpoint$Engine$1(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$1.apply(App.scala:76)
    at scala.App$$anonfun$main$1.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

我想这与Gatling配置有关,但不要我不知道出了什么问题。

I guess this is something to do with Gatling configuration, but don't know what has gone wrong.

我试着做类似的事。我也不能使用Maven。我会尝试记住我是如何做到的。

I tried to do something similar. I could not use Maven as well. I will try to remember how I did this.

1)我已经配置 maven-assembly-plugin 来生成具有这样依赖关系的单个JAR :

1) I have configured maven-assembly-plugin to generate single JAR with dependencies like this:

<plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <configuration>
        <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
    </configuration>
</plugin>

您需要确保所得到的所有库(gatling,scala运行时,锌编译器)都存在于您的结果中classpath。

You need to ensure all required libraries (gatling, scala runtime, zinc compiler) are present on your resulting classpath.

2)检查依赖关系的范围,因为Maven默认只包含用 scope = compile 定义的类。最简单的方法可能是不使用测试依赖项。

2) Check the scope of your dependencies as Maven packs only classes defined with scope=compile by default. The most simple way is probably to use no test dependencies.

3)创建一个启动脚本,例如的 launch.sh 即可。它应包含以下内容:

3) Create a launch script, e.g. launch.sh. It should contain something like this:

#!/bin/sh
USER_ARGS="-Dsomething=$1"
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 target directory in classpath parameter definition.

4)编译已编译的目标目录并 launch.sh 到一个目录并分发它(例如作为存档)。然后你可以通过执行 ./ launch.sh 来实现这些方案。

4) Compile your compiled target directory and launch.sh to a single directory and distribute this (e.g. as archive). Then you can the scenarios by executing ./launch.sh.

我知道这是不是标准解决方案,但它对我有用。希望它也会对你有所帮助。如果您有任何问题或提示需要改进,请与我们分享。

I know this is not a 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.