首页 > 解决方案 > Jmeter with maven and using external groovy scripts issues with groovy imports

问题描述

I'd like to execute jmeter tests from maven using groovy scripts, but I got the error below. For setting up jmeter and maven I did what is described here.

Shall I package my groovy functions and entities into a jar and copy into jmeter's lib directory and only put those groovy scripts next to the jmx file which contains the sampler code?

2019-06-22 17:40:17,714 INFO o.a.j.s.SampleResult: sampleresult.useNanoTime=true
2019-06-22 17:40:17,714 INFO o.a.j.s.SampleResult: sampleresult.nanoThreadSleep=5000
2019-06-22 17:40:17,744 ERROR o.a.j.p.j.s.JSR223Sampler: Problem in JSR223 script CreateUsers, message: javax.script.ScriptException: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
Script4.groovy: 4: unable to resolve class com.google.gson.Gson
 @ line 4, column 1.
   import com.google.gson.Gson;
   ^

标签: mavengroovyjmetergson

解决方案


我找到了答案:

  • 我需要将我想在测试中使用的库打包到 jar 中并将它们放入jmeter/lib目录中
  • 我必须对齐 Sampler 脚本路径,以便 jmeter 可以使用它们

第一个解决方案来自jmeter-maven-plugin doc:

<project>
    [...]
        <build>
            <plugins>
                <plugin>
                    <groupId>com.lazerycode.jmeter</groupId>
                    <artifactId>jmeter-maven-plugin</artifactId>
                    <version>USE LAST VERSION</version>
                    <executions>
                        <execution>
                            <id>jmeter-tests</id>
                            <goals>
                                <goal>jmeter</goal>
                            </goals>
                        </execution>
                    </executions>
                    <configuration>
                        <testPlanLibraries>
                             <artifact>org.apache.activemq:activemq-spring:5.15.2</artifact>
                 <artifact>org.apache.activemq:activemq-client:5.15.2</artifact>
                 <artifact>org.apache.activemq:activemq-broker:5.15.2</artifact>
                </testPlanLibraries>
                        <excludedArtifacts>
                             <exclusion>com.sun.jdmk:jmxtools</exclusion>
                             <exclusion>com.sun.jmx:jmxri</exclusion>
            </excludedArtifacts>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    [...]
</project>

第二个解决方案是我需要在 jmx 文件旁边复制 Sampler 脚本。Maven 可以使用maven-resource-plugin轻松做到这一点。


推荐阅读